Skip to content

Conversation

@laanwj
Copy link
Member

@laanwj laanwj commented Feb 17, 2016

Implements #7442 by adding an option -stdin which reads additional arguments from standard in, one per line.

For example

$ echo -e "mysecretcode\n120" | src/bitcoin-cli -stdin walletpassphrase
$ echo -e "walletpassphrase\nmysecretcode\n120" | src/bitcoin-cli -stdin
$ src/bitcoin-cli -stdin walletpassphrase
mysecretcode
120
^D

This is the simplest implementation and avoids escaping issues by using newline as separator instead of space, I first had another implementation: laanwj@1f73b8e that reuses parseCommandLine from the GUI debug console, but I think this is more useful in practice as most use of cli is probably script-driven.

@paveljanik
Copy link
Contributor

Brilliant idea!

ACK laanwj@c97198d

pavel$ echo -e "getblockhash\n0" | bitcoin-7550/src/bitcoin-cli -stdin
000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
pavel$ 

@laanwj laanwj added the Feature label Feb 17, 2016
@jonasschnelli
Copy link
Contributor

Nice!
Concept ACK. Plans to test this soon.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about adding a magic work as ^D alternative? Something like "end" or "quit"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've thought about that. The problem is that anything could be a valid argument. I've chosen the line-per-argument, until EOL to avoid that kind of escaping issues.
It's easy for scripting but yes for a user interface it's not very friendly.

master...laanwj:2016_02_cli_stdin is better in that regard, but what makes it an easier user interface is annoying/dangerous for scripting (as you'd have to quote ' "" etc to prevent one argument from spilling into the next)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe adding a little ">>>" prompt and or giving a one-line-help-instruction when using -stdin (something like "enter command, arguments and use Ctrl-D to quit/execute")?

Allow quitting over "quit()" and if "exit" or "quit" has been typed, show little help line?
Though, a simple one-line-help-message would probably do the job.

Python example:

:~ jonasschnelli$ python
Python 2.7.10 (default, Sep 23 2015, 04:34:14) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.72)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> quit
Use quit() or Ctrl-D (i.e. EOF) to exit

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be awesome for an "interactive mode" (see #3122), but -stdin is explicitly not meant to be that, it's just to have a way to input data (say, from scripts) that isn't exposed on the command line. Printing more text will make it harder to parse the output.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. Was nitpick territory anyway (does not affect my tested ACK I already gave).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, for now, I've added a EOF/Ctrl-D mention to the -help message at least.

@jonasschnelli
Copy link
Contributor

Tested ACK c97198db769954c4ad2b57eaf7e5335578badc00

Post-merge actions: add to the docs/release notes, maybe add a little test script (though, not sure if we have a test entry point for bitcoin-cli at all).

Implements bitcoin#7442 by adding an option `-stdin` which reads
additional arguments from stdin, one per line.

For example

```bash
echo -e "mysecretcode\n120" | src/bitcoin-cli -stdin walletpassphrase
echo -e "walletpassphrase\nmysecretcode\n120" | src/bitcoin-cli -stdin
```
@laanwj laanwj force-pushed the 2016_02_cli_stdin2 branch from c97198d to 92bcca3 Compare February 24, 2016 09:12
@jonasschnelli
Copy link
Contributor

Re-ACK 92bcca3

@laanwj laanwj merged commit f22f14c into bitcoin:master Feb 24, 2016
laanwj added a commit that referenced this pull request Feb 24, 2016
f22f14c doc: mention bitcoin-cli -stdin in release notes (Wladimir J. van der Laan)
92bcca3 rpc: Input-from-stdin mode for bitcoin-cli (Wladimir J. van der Laan)
luke-jr added a commit to bitcoinknots/bitcoin that referenced this pull request Jun 28, 2016
laanwj added a commit that referenced this pull request Sep 6, 2017
29e1dfb [test] Add bitcoin-cli -stdin and -stdinrpcpass functional tests (João Barbosa)
ce379b4 [test] Replace check_output with low level version (João Barbosa)
232e3e8 [test] Add assert_raises_process_error to assert process errors (João Barbosa)
5c18a84 [test] Add support for custom arguments to TestNodeCLI (João Barbosa)
e127494 [test] Improve assert_raises_jsonrpc docstring (João Barbosa)
7696841 Fix style in -stdin and -stdinrpcpass handling (João Barbosa)

Pull request description:

  This patch adds tests for `bitcoin-cli` options `-stdin` (#7550) and `-stdinrpcpass` #10997.

Tree-SHA512: fd8133f44876f2b5b41dfd3762b1988598f6b7bf13fb2385ad95876825d9c0b2b896ce4ea6eeb21012158e1f276907f155d37bb967198b609d2d3dddbfa334c1
codablock pushed a commit to codablock/dash that referenced this pull request Dec 9, 2017
f22f14c doc: mention bitcoin-cli -stdin in release notes (Wladimir J. van der Laan)
92bcca3 rpc: Input-from-stdin mode for bitcoin-cli (Wladimir J. van der Laan)
zkbot added a commit to zcash/zcash that referenced this pull request Apr 13, 2018
CLI binary improvements

Cherry-picked from the following upstream PRs:

- bitcoin/bitcoin#5936
- bitcoin/bitcoin#7550
- bitcoin/bitcoin#7989
- bitcoin/bitcoin#7957
- bitcoin/bitcoin#9067
- bitcoin/bitcoin#9220

Excludes any changes that affected the QT code.
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Dec 9, 2019
…nal tests

29e1dfb [test] Add bitcoin-cli -stdin and -stdinrpcpass functional tests (João Barbosa)
ce379b4 [test] Replace check_output with low level version (João Barbosa)
232e3e8 [test] Add assert_raises_process_error to assert process errors (João Barbosa)
5c18a84 [test] Add support for custom arguments to TestNodeCLI (João Barbosa)
e127494 [test] Improve assert_raises_jsonrpc docstring (João Barbosa)
7696841 Fix style in -stdin and -stdinrpcpass handling (João Barbosa)

Pull request description:

  This patch adds tests for `bitcoin-cli` options `-stdin` (bitcoin#7550) and `-stdinrpcpass` bitcoin#10997.

Tree-SHA512: fd8133f44876f2b5b41dfd3762b1988598f6b7bf13fb2385ad95876825d9c0b2b896ce4ea6eeb21012158e1f276907f155d37bb967198b609d2d3dddbfa334c1
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jan 3, 2020
…nal tests

29e1dfb [test] Add bitcoin-cli -stdin and -stdinrpcpass functional tests (João Barbosa)
ce379b4 [test] Replace check_output with low level version (João Barbosa)
232e3e8 [test] Add assert_raises_process_error to assert process errors (João Barbosa)
5c18a84 [test] Add support for custom arguments to TestNodeCLI (João Barbosa)
e127494 [test] Improve assert_raises_jsonrpc docstring (João Barbosa)
7696841 Fix style in -stdin and -stdinrpcpass handling (João Barbosa)

Pull request description:

  This patch adds tests for `bitcoin-cli` options `-stdin` (bitcoin#7550) and `-stdinrpcpass` bitcoin#10997.

Tree-SHA512: fd8133f44876f2b5b41dfd3762b1988598f6b7bf13fb2385ad95876825d9c0b2b896ce4ea6eeb21012158e1f276907f155d37bb967198b609d2d3dddbfa334c1

remove duplicate method

Signed-off-by: Pasta <[email protected]>
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants