Skip to content

Commit bdb5777

Browse files
committed
Merge #224: fix(pretty-format): use --pretty in any position
c475363 chore(clippy): clippy fixes (Vadim Anufriev) daebe53 test(pretty): add tests for pretty option (Vadim Anufriev) 17a4dca fix(pretty): use pretty option in any position (Vadim Anufriev) Pull request description: <!-- You can erase any parts of this template not applicable to your Pull Request. --> ### Description Fix `--pretty` flag to work in any position by marking it as a global argument. Previously the flag only worked before subcommands (`bdk-cli --pretty key generate`) but failed after (`bdk-cli key generate --pretty`). ### Notes to the reviewers **Before the fix:** Works: ```bash -> % cargo run -- --pretty key generate +-------------+-----------------------------------------------------------------------------------------------------------------+ | Fingerprint | 3e68be40 | +-------------+-----------------------------------------------------------------------------------------------------------------+ | Mnemonic | wheat equip exhaust funny panic lend message champion slim derive alcohol differ | +-------------+-----------------------------------------------------------------------------------------------------------------+ | Xprv | tprv8ZgxMBicQKsPdmyStjLXkChBJnUa5DBVJY5J46C3qbe98623M9uqD9wrDtcSZe4hLCodZdKacuY8YPzrNWFUEUwSrpa12KiEBikwLTjiYWP | +-------------+-----------------------------------------------------------------------------------------------------------------+ ``` Doesn't work: ```bash -> % cargo run -- key generate --pretty error: unexpected argument '--pretty' found Usage: bdk-cli key generate [OPTIONS] For more information, try '--help'. ``` <!-- In this section you can include notes directed to the reviewers, like explaining why some parts of the PR were done in a specific way --> ## Changelog notice <!-- Notice the release manager should include in the release tag message changelog --> <!-- See https://keepachangelog.com/en/1.0.0/ for examples --> ### Checklists #### All Submissions: * [x] I've signed all my commits * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk-cli/blob/master/CONTRIBUTING.md) * [x] I ran `cargo fmt` and `cargo clippy` before committing #### Bugfixes: * [ ] This pull request breaks the existing API * [x] I've added tests to reproduce the issue which are now passing * [ ] I'm linking the issue being fixed by this PR ACKs for top commit: notmandatory: ACK c475363 tvpeter: tACK c475363 Tree-SHA512: 748860bf542b0da80407b0b15b068b6bb4a519913eec85251439a32e9d40ccec0af13edfd21ef4ab42f43d26e79e1996c958c9569955bb90f8d9033717aae792
2 parents a3fcf8a + c475363 commit bdb5777

File tree

3 files changed

+54
-9
lines changed

3 files changed

+54
-9
lines changed

src/commands.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub struct CliOpts {
5252
#[arg(env = "DATADIR", short = 'd', long = "datadir")]
5353
pub datadir: Option<std::path::PathBuf>,
5454
/// Output results in pretty format (instead of JSON).
55-
#[arg(long = "pretty")]
55+
#[arg(long = "pretty", global = true)]
5656
pub pretty: bool,
5757
/// Top level cli sub-commands.
5858
#[command(subcommand)]

src/handlers.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,9 +1093,8 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result<String, Error> {
10931093
}
10941094
};
10951095

1096-
let mut wallet = new_persisted_wallet(network, &mut persister, &wallet_opts)?;
1097-
let blockchain_client =
1098-
new_blockchain_client(&wallet_opts, &wallet, database_path)?;
1096+
let mut wallet = new_persisted_wallet(network, &mut persister, wallet_opts)?;
1097+
let blockchain_client = new_blockchain_client(wallet_opts, &wallet, database_path)?;
10991098

11001099
let result = handle_online_wallet_subcommand(
11011100
&mut wallet,
@@ -1108,10 +1107,10 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result<String, Error> {
11081107
};
11091108
#[cfg(not(any(feature = "sqlite", feature = "redb")))]
11101109
let result = {
1111-
let wallet = new_wallet(network, &wallet_opts)?;
1110+
let wallet = new_wallet(network, wallet_opts)?;
11121111
let blockchain_client =
1113-
crate::utils::new_blockchain_client(&wallet_opts, &wallet, database_path)?;
1114-
let mut wallet = new_wallet(network, &wallet_opts)?;
1112+
crate::utils::new_blockchain_client(wallet_opts, &wallet, database_path)?;
1113+
let mut wallet = new_wallet(network, wallet_opts)?;
11151114
handle_online_wallet_subcommand(&mut wallet, blockchain_client, online_subcommand)
11161115
.await?
11171116
};
@@ -1162,10 +1161,10 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result<String, Error> {
11621161
};
11631162
#[cfg(not(any(feature = "sqlite", feature = "redb")))]
11641163
let result = {
1165-
let mut wallet = new_wallet(network, &wallet_opts)?;
1164+
let mut wallet = new_wallet(network, wallet_opts)?;
11661165
handle_offline_wallet_subcommand(
11671166
&mut wallet,
1168-
&wallet_opts,
1167+
wallet_opts,
11691168
&cli_opts,
11701169
offline_subcommand.clone(),
11711170
)?

tests/cli_flags.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) 2020-2025 Bitcoin Dev Kit Developers
2+
//
3+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
4+
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
6+
// You may not use this file except in accordance with one or both of these
7+
// licenses.
8+
9+
//! CLI Flags Tests
10+
//!
11+
//! Tests for global CLI flags and their behavior
12+
13+
use std::process::Command;
14+
15+
#[test]
16+
fn test_without_pretty_flag() {
17+
let output = Command::new("cargo")
18+
.args("run -- key generate".split_whitespace())
19+
.output()
20+
.unwrap();
21+
22+
assert!(output.status.success());
23+
24+
let stdout = String::from_utf8_lossy(&output.stdout);
25+
assert!(serde_json::from_str::<serde_json::Value>(&stdout).is_ok());
26+
}
27+
28+
#[test]
29+
fn test_pretty_flag_before_subcommand() {
30+
let output = Command::new("cargo")
31+
.args("run -- --pretty key generate".split_whitespace())
32+
.output()
33+
.unwrap();
34+
35+
assert!(output.status.success());
36+
}
37+
38+
#[test]
39+
fn test_pretty_flag_after_subcommand() {
40+
let output = Command::new("cargo")
41+
.args("run -- key generate --pretty".split_whitespace())
42+
.output()
43+
.unwrap();
44+
45+
assert!(output.status.success());
46+
}

0 commit comments

Comments
 (0)