diff --git a/src/lib.rs b/src/lib.rs index 372f7eef2f..929af95c15 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,7 +26,7 @@ use { into_usize::IntoUsize, representation::Representation, settings::Settings, - subcommand::{Subcommand, SubcommandResult}, + subcommand::{OutputFormat, Subcommand, SubcommandResult}, tally::Tally, }, anyhow::{anyhow, bail, ensure, Context, Error}, @@ -252,7 +252,7 @@ pub fn main() { let args = Arguments::parse(); - let minify = args.options.minify; + let format = args.options.format; match args.run() { Err(err) => { @@ -274,7 +274,7 @@ pub fn main() { } Ok(output) => { if let Some(output) = output { - output.print_json(minify); + output.print(format.unwrap_or_default()); } gracefully_shutdown_indexer(); } diff --git a/src/options.rs b/src/options.rs index ba76689845..6c3c7f2826 100644 --- a/src/options.rs +++ b/src/options.rs @@ -65,8 +65,8 @@ pub struct Options { pub(crate) index_transactions: bool, #[arg(long, help = "Run in integration test mode.")] pub(crate) integration_test: bool, - #[arg(long, help = "Minify JSON output.")] - pub(crate) minify: bool, + #[clap(long, short, long, help = "Specify output format. [default: json]")] + pub(crate) format: Option, #[arg( long, short, diff --git a/src/subcommand.rs b/src/subcommand.rs index 2da87735db..fd90a9fd0d 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -81,20 +81,28 @@ impl Subcommand { } } +#[derive(clap::ValueEnum, Debug, Clone, Copy, Serialize, Deserialize, Default)] +pub enum OutputFormat { + #[default] + Json, + Yaml, + Minify, +} + pub trait Output: Send { - fn print_json(&self, minify: bool); + fn print(&self, format: OutputFormat); } impl Output for T where T: Serialize + Send, { - fn print_json(&self, minify: bool) { - if minify { - serde_json::to_writer(io::stdout(), self).ok(); - } else { - serde_json::to_writer_pretty(io::stdout(), self).ok(); - } + fn print(&self, format: OutputFormat) { + match format { + OutputFormat::Json => serde_json::to_writer_pretty(io::stdout(), self).ok(), + OutputFormat::Yaml => serde_yaml::to_writer(io::stdout(), self).ok(), + OutputFormat::Minify => serde_json::to_writer(io::stdout(), self).ok(), + }; println!(); } } diff --git a/tests/wallet/dump.rs b/tests/wallet/dump.rs index f8fdca5b68..cdae859cfd 100644 --- a/tests/wallet/dump.rs +++ b/tests/wallet/dump.rs @@ -55,7 +55,7 @@ fn dump_and_restore_descriptors_with_minify() { create_wallet(&core, &ord); - let output = CommandBuilder::new("--minify wallet dump") + let output = CommandBuilder::new("--format minify wallet dump") .core(&core) .ord(&ord) .stderr_regex(".*")