diff --git a/src/commands.rs b/src/commands.rs index 62a6a2d..54cddf0 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -52,7 +52,7 @@ pub struct CliOpts { #[arg(env = "DATADIR", short = 'd', long = "datadir")] pub datadir: Option, /// Output results in pretty format (instead of JSON). - #[arg(long = "pretty")] + #[arg(long = "pretty", global = true)] pub pretty: bool, /// Top level cli sub-commands. #[command(subcommand)] diff --git a/src/handlers.rs b/src/handlers.rs index 941c550..d9d2cbe 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -1093,9 +1093,8 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result { } }; - let mut wallet = new_persisted_wallet(network, &mut persister, &wallet_opts)?; - let blockchain_client = - new_blockchain_client(&wallet_opts, &wallet, database_path)?; + let mut wallet = new_persisted_wallet(network, &mut persister, wallet_opts)?; + let blockchain_client = new_blockchain_client(wallet_opts, &wallet, database_path)?; let result = handle_online_wallet_subcommand( &mut wallet, @@ -1108,10 +1107,10 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result { }; #[cfg(not(any(feature = "sqlite", feature = "redb")))] let result = { - let wallet = new_wallet(network, &wallet_opts)?; + let wallet = new_wallet(network, wallet_opts)?; let blockchain_client = - crate::utils::new_blockchain_client(&wallet_opts, &wallet, database_path)?; - let mut wallet = new_wallet(network, &wallet_opts)?; + crate::utils::new_blockchain_client(wallet_opts, &wallet, database_path)?; + let mut wallet = new_wallet(network, wallet_opts)?; handle_online_wallet_subcommand(&mut wallet, blockchain_client, online_subcommand) .await? }; @@ -1162,10 +1161,10 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result { }; #[cfg(not(any(feature = "sqlite", feature = "redb")))] let result = { - let mut wallet = new_wallet(network, &wallet_opts)?; + let mut wallet = new_wallet(network, wallet_opts)?; handle_offline_wallet_subcommand( &mut wallet, - &wallet_opts, + wallet_opts, &cli_opts, offline_subcommand.clone(), )? diff --git a/tests/cli_flags.rs b/tests/cli_flags.rs new file mode 100644 index 0000000..beec798 --- /dev/null +++ b/tests/cli_flags.rs @@ -0,0 +1,46 @@ +// Copyright (c) 2020-2025 Bitcoin Dev Kit Developers +// +// This file is licensed under the Apache License, Version 2.0 or the MIT license +// , at your option. +// You may not use this file except in accordance with one or both of these +// licenses. + +//! CLI Flags Tests +//! +//! Tests for global CLI flags and their behavior + +use std::process::Command; + +#[test] +fn test_without_pretty_flag() { + let output = Command::new("cargo") + .args("run -- key generate".split_whitespace()) + .output() + .unwrap(); + + assert!(output.status.success()); + + let stdout = String::from_utf8_lossy(&output.stdout); + assert!(serde_json::from_str::(&stdout).is_ok()); +} + +#[test] +fn test_pretty_flag_before_subcommand() { + let output = Command::new("cargo") + .args("run -- --pretty key generate".split_whitespace()) + .output() + .unwrap(); + + assert!(output.status.success()); +} + +#[test] +fn test_pretty_flag_after_subcommand() { + let output = Command::new("cargo") + .args("run -- key generate --pretty".split_whitespace()) + .output() + .unwrap(); + + assert!(output.status.success()); +}