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

pull laanwj wants to merge 2 commits into bitcoin:master from laanwj:2016_02_cli_stdin2 changing 2 files +25 −9
  1. laanwj commented at 2:15 PM on February 17, 2016: member

    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: https://github.com/laanwj/bitcoin/commit/1f73b8e27b57c8561840cddc9f69a97475d06e85 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.

  2. laanwj added the label RPC on Feb 17, 2016
  3. paveljanik commented at 4:30 PM on February 17, 2016: contributor

    Brilliant idea!

    ACK https://github.com/laanwj/bitcoin/commit/c97198db769954c4ad2b57eaf7e5335578badc00

    pavel$ echo -e "getblockhash\n0" | bitcoin-7550/src/bitcoin-cli -stdin
    000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
    pavel$ 
    
  4. laanwj added the label Feature on Feb 17, 2016
  5. jonasschnelli commented at 2:35 PM on February 18, 2016: contributor

    Nice! Concept ACK. Plans to test this soon.

  6. in src/bitcoin-cli.cpp:None in c97198db76 outdated
     244 | -        UniValue params = RPCConvertValues(strMethod, strParams);
     245 | +        std::vector<std::string> args = std::vector<std::string>(&argv[1], &argv[argc]);
     246 | +        if (GetBoolArg("-stdin", false)) {
     247 | +            // Read one arg per line from stdin and append
     248 | +            std::string line;
     249 | +            while (std::getline(std::cin,line))
    


    jonasschnelli commented at 3:16 PM on February 19, 2016:

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


    laanwj commented at 4:47 PM on February 19, 2016:

    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.

    https://github.com/bitcoin/bitcoin/compare/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 commented at 8:11 AM on February 22, 2016:

    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 commented at 8:51 AM on February 24, 2016:

    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 commented at 8:53 AM on February 24, 2016:

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


    laanwj commented at 9:14 AM on February 24, 2016:

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

  7. jonasschnelli commented at 3:18 PM on February 19, 2016: 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).

  8. 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
    ```
    92bcca37ab
  9. laanwj force-pushed on Feb 24, 2016
  10. jonasschnelli commented at 9:15 AM on February 24, 2016: contributor

    Re-ACK 92bcca37ab0c52789b13ebee1f4659e30f05e2b6

  11. doc: mention bitcoin-cli -stdin in release notes f22f14c65b
  12. laanwj merged this on Feb 24, 2016
  13. laanwj closed this on Feb 24, 2016

  14. laanwj referenced this in commit 8b958ab15b on Feb 24, 2016
  15. luke-jr referenced this in commit f9bbb0eddc on Jun 28, 2016
  16. luke-jr referenced this in commit 3c2a190b7a on Jun 28, 2016
  17. laanwj referenced this in commit 645a7ecc0b on Sep 6, 2017
  18. codablock referenced this in commit 75720c6a59 on Dec 9, 2017
  19. codablock referenced this in commit f1da40c876 on Dec 9, 2017
  20. zkbot referenced this in commit 3b0a5bcd24 on Apr 13, 2018
  21. zkbot referenced this in commit 65a8f9f201 on Apr 13, 2018
  22. PastaPastaPasta referenced this in commit 74801efebf on Sep 23, 2019
  23. PastaPastaPasta referenced this in commit a1b2915923 on Sep 24, 2019
  24. PastaPastaPasta referenced this in commit 502b1c0387 on Nov 19, 2019
  25. PastaPastaPasta referenced this in commit 7329569cda on Nov 21, 2019
  26. PastaPastaPasta referenced this in commit 82035124bf on Dec 9, 2019
  27. PastaPastaPasta referenced this in commit e9355f9eec on Jan 1, 2020
  28. PastaPastaPasta referenced this in commit 871903f0d2 on Jan 2, 2020
  29. PastaPastaPasta referenced this in commit 946eb1599f on Jan 2, 2020
  30. PastaPastaPasta referenced this in commit 8aeb9e2f86 on Jan 2, 2020
  31. PastaPastaPasta referenced this in commit 9ae12e04e2 on Jan 2, 2020
  32. PastaPastaPasta referenced this in commit 39c2079743 on Jan 2, 2020
  33. PastaPastaPasta referenced this in commit eac4ea76e4 on Jan 2, 2020
  34. PastaPastaPasta referenced this in commit 7214f07e78 on Jan 3, 2020
  35. PastaPastaPasta referenced this in commit 3b1696a7e6 on Jan 4, 2020
  36. PastaPastaPasta referenced this in commit 09e4b00321 on Jan 12, 2020
  37. PastaPastaPasta referenced this in commit 1e1a79a68f on Jan 12, 2020
  38. PastaPastaPasta referenced this in commit 5de97013f2 on Jan 12, 2020
  39. ckti referenced this in commit 6ae78314c7 on Mar 28, 2021
  40. MarcoFalke locked this on Sep 8, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-13 15:15 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me