|
22 | 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
23 | 23 | // SOFTWARE.
|
24 | 24 |
|
| 25 | +//! Command line interface |
| 26 | +//! |
| 27 | +//! This module provides a simple cli interface using [clap](https://docs.rs/clap/) to access |
| 28 | +//! basic wallet functionality. See the `repl.rs` example app for how to use this module to create |
| 29 | +//! a custom `bdk` based command line wallet application. |
| 30 | +//! |
| 31 | +//! # Example |
| 32 | +//! |
| 33 | +//! ```rust |
| 34 | +//! let app = cli::make_cli_subcommands(); |
| 35 | +//! let app = cli::add_global_flags(app); |
| 36 | +//! |
| 37 | +//! let matches = app.get_matches(); |
| 38 | +//! |
| 39 | +//! let network = match matches.value_of("network") { |
| 40 | +//! Some("regtest") => Network::Regtest, |
| 41 | +//! Some("testnet") | _ => Network::Testnet, |
| 42 | +//! }; |
| 43 | +//! |
| 44 | +//! // etc. |
| 45 | +//! ``` |
| 46 | +//! See [`make_cli_subcommands()`] for list of sub-commands, and [`add_global_flags()`] for list of global flags. |
| 47 | +
|
25 | 48 | use std::collections::BTreeMap;
|
26 | 49 | use std::str::FromStr;
|
27 | 50 |
|
@@ -70,6 +93,47 @@ fn outpoint_validator(s: String) -> Result<(), String> {
|
70 | 93 | parse_outpoint(&s).map(|_| ())
|
71 | 94 | }
|
72 | 95 |
|
| 96 | +/// Create new [clap](https://docs.rs/clap/) `App` with wallet sub-commands |
| 97 | +/// |
| 98 | +/// Configuration such as name, version, author, about can be overridden. Additional sub-commands |
| 99 | +/// sub-commands can also be added. See [clap](https://docs.rs/clap/) for more information on how |
| 100 | +/// to customize an `App`. |
| 101 | +/// |
| 102 | +/// # Wallet sub-commands |
| 103 | +/// |
| 104 | +/// | Sub-command | Description | |
| 105 | +/// |--------------------|----------------------------------------------- | |
| 106 | +/// | broadcast | Broadcasts a transaction to the network. Takes either a raw transaction or a PSBT to extract | |
| 107 | +/// | bump_fee | Bumps the fees of an RBF transaction | |
| 108 | +/// | combine_psbt | Combines multiple PSBTs into one | |
| 109 | +/// | create_tx | Creates a new unsigned tranasaction | |
| 110 | +/// | extract_psbt | Extracts a raw transaction from a PSBT | |
| 111 | +/// | finalize_psbt | Finalizes a psbt | |
| 112 | +/// | get_balance | Returns the current wallet balance | |
| 113 | +/// | get_new_address | Generates a new external address | |
| 114 | +/// | help | Prints this message or the help of the given subcommand(s) | |
| 115 | +/// | list_transactions | Lists all the incoming and outgoing transactions of the wallet | |
| 116 | +/// | list_unspent | Lists the available spendable UTXOs | |
| 117 | +/// | policies | Returns the available spending policies for the descriptor | |
| 118 | +/// | public_descriptor | Returns the public version of the wallet's descriptor(s) | |
| 119 | +/// | repl | Opens an interactive shell | |
| 120 | +/// | sign | Signs and tries to finalize a PSBT | |
| 121 | +/// | sync | Syncs with the chosen Electrum server | |
| 122 | +/// |
| 123 | +/// # Example |
| 124 | +/// |
| 125 | +/// ```rust |
| 126 | +/// let app = cli::make_cli_subcommands(); |
| 127 | +/// let app = app.name("Demo App") |
| 128 | +/// .version("0.1.0") |
| 129 | +/// .author("Demo Author") |
| 130 | +/// .about("A demo cli wallet based on the bdk library."); |
| 131 | +/// |
| 132 | +/// let app = app.subcommand( |
| 133 | +/// SubCommand::with_name("do_something").about("Do something else with your app"), |
| 134 | +/// ); |
| 135 | +/// ``` |
| 136 | +
|
73 | 137 | pub fn make_cli_subcommands<'a, 'b>() -> App<'a, 'b> {
|
74 | 138 | App::new("Magical Bitcoin Wallet")
|
75 | 139 | .version(option_env!("CARGO_PKG_VERSION").unwrap_or("unknown"))
|
@@ -308,6 +372,42 @@ pub fn make_cli_subcommands<'a, 'b>() -> App<'a, 'b> {
|
308 | 372 | )
|
309 | 373 | }
|
310 | 374 |
|
| 375 | +/// Add [clap](https://docs.rs/clap/) global wallet options to an `App` |
| 376 | +/// |
| 377 | +/// Additional app specific options can be added. See [clap](https://docs.rs/clap/) for more |
| 378 | +/// information on how to customize the global flags for an `App`. |
| 379 | +/// |
| 380 | +/// # Wallet global flags |
| 381 | +/// |
| 382 | +/// | Short | Long | Description | |
| 383 | +/// |-------|-------|-------------| |
| 384 | +/// | -c | --change_descriptor <DESCRIPTOR> | Sets the descriptor to use for internal addresses | |
| 385 | +/// | -d | --descriptor <DESCRIPTOR> | Sets the descriptor to use for the external addresses | |
| 386 | +/// | -e | --esplora <ESPLORA> | Use the esplora server if given as parameter | |
| 387 | +/// | | --esplora_concurrency <ESPLORA_CONCURRENCY> | Concurrency of requests made to the esplora server [default: 4] | |
| 388 | +/// | -n | --network <NETWORK> | Sets the network [default: testnet] [possible values: testnet, regtest] | |
| 389 | +/// | -p | --proxy <SERVER:PORT> | Sets the SOCKS5 proxy for the Electrum client | |
| 390 | +/// | -s | --server <SERVER:PORT> | Sets the Electrum server to use [default: ssl://electrum.blockstream.info:60002] | |
| 391 | +/// | -w | --wallet <WALLET_NAME> | Selects the wallet to use [default: main] | |
| 392 | +/// |
| 393 | +/// # Example |
| 394 | +/// |
| 395 | +/// ```rust |
| 396 | +/// let app = cli::make_cli_subcommands(); |
| 397 | +/// let app = cli::add_global_flags(app); |
| 398 | +/// |
| 399 | +/// let app = app.arg( |
| 400 | +/// Arg::with_name("custom") |
| 401 | +/// .short("c") |
| 402 | +/// .long("custom") |
| 403 | +/// .value_name("CUSTOM") |
| 404 | +/// .help("Sets the custom option") |
| 405 | +/// .takes_value(true) |
| 406 | +/// .default_value("option1") |
| 407 | +/// .possible_values(&["option1", "option2"]), |
| 408 | +/// ); |
| 409 | +/// ``` |
| 410 | +
|
311 | 411 | pub fn add_global_flags<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> {
|
312 | 412 | let mut app = app
|
313 | 413 | .arg(
|
|
0 commit comments