Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

rpc: Input-from-stdin mode for bitcoin-cli #7550

Merged
merged 2 commits into from Feb 24, 2016

Conversation

Projects
None yet
3 participants
Owner

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/bitcoin@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.

@laanwj laanwj added the RPC/REST/ZMQ label Feb 17, 2016

Contributor

paveljanik commented Feb 17, 2016

Brilliant idea!

ACK laanwj/bitcoin@c97198d

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

@laanwj laanwj added the Feature label Feb 17, 2016

Member

jonasschnelli commented Feb 18, 2016

Nice!
Concept ACK. Plans to test this soon.

@jonasschnelli jonasschnelli commented on the diff Feb 19, 2016

src/bitcoin-cli.cpp
@@ -232,15 +233,17 @@ int CommandLineRPC(int argc, char *argv[])
argc--;
argv++;
}
-
- // Method
- if (argc < 2)
- throw runtime_error("too few parameters");
- string strMethod = argv[1];
-
- // Parameters default to strings
- std::vector<std::string> strParams(&argv[2], &argv[argc]);
- UniValue params = RPCConvertValues(strMethod, strParams);
+ std::vector<std::string> args = std::vector<std::string>(&argv[1], &argv[argc]);
+ if (GetBoolArg("-stdin", false)) {
+ // Read one arg per line from stdin and append
+ std::string line;
+ while (std::getline(std::cin,line))
@jonasschnelli

jonasschnelli Feb 19, 2016

Member

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

@laanwj

laanwj Feb 19, 2016

Owner

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)

@jonasschnelli

jonasschnelli Feb 22, 2016

Member

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
@laanwj

laanwj Feb 24, 2016

Owner

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.

@jonasschnelli

jonasschnelli Feb 24, 2016

Member

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

@laanwj

laanwj Feb 24, 2016

Owner

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

Member

jonasschnelli commented Feb 19, 2016

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).

rpc: Input-from-stdin mode for bitcoin-cli
Implements #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
```
Member

jonasschnelli commented Feb 24, 2016

Re-ACK 92bcca3

@laanwj laanwj merged commit f22f14c into bitcoin:master Feb 24, 2016

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details

laanwj added a commit that referenced this pull request Feb 24, 2016

Merge #7550: rpc: Input-from-stdin mode for bitcoin-cli
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

Merge #11125: Add bitcoin-cli -stdin and -stdinrpcpass functional 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` (#7550) and `-stdinrpcpass` #10997.

Tree-SHA512: fd8133f44876f2b5b41dfd3762b1988598f6b7bf13fb2385ad95876825d9c0b2b896ce4ea6eeb21012158e1f276907f155d37bb967198b609d2d3dddbfa334c1

@codablock codablock referenced this pull request in dashpay/dash Oct 20, 2017

Open

[WIP] Update build system to Bitcoin 0.13.2 #1692

22 of 24 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment