From 60298dd04a2c8fe48bc58ef1d5430538b1996b46 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 24 Dec 2019 12:38:46 +0100 Subject: Fix: Make "list" default command of imag-tag The "list" command is advertised as default command for imag-tag. This commit fixes the implementation so that the binary actually behaves this way. For this, the list() function gets a new parameter which tells it whether there is a subcommand object to be expected in the Runtime. Signed-off-by: Matthias Beyer (cherry picked from commit 708c7b26b12364434316a5435b01a2c3869bfc6f) --- bin/core/imag-tag/src/lib.rs | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/bin/core/imag-tag/src/lib.rs b/bin/core/imag-tag/src/lib.rs index 3e931609..0211d3f1 100644 --- a/bin/core/imag-tag/src/lib.rs +++ b/bin/core/imag-tag/src/lib.rs @@ -89,7 +89,7 @@ impl ImagApplication for ImagTag { let process = |iter: &mut dyn Iterator>| -> Result<()> { match rt.cli().subcommand() { ("list", _) => iter - .map_ok(|id| list(id, &rt)) + .map_ok(|id| list(id, &rt, true)) .collect::>>() .map(|_| ()), @@ -185,6 +185,13 @@ impl ImagApplication for ImagTag { .map(|_| ()) }, + (_, None) => { + debug!("No subcommand, using 'list'"); + iter.map_ok(|id| list(id, &rt, false)) + .collect::>>() + .map(|_| ()) + }, + (other, _) => { debug!("Unknown command"); if rt.handle_unknown_subcommand("imag-tag", other, rt.cli())?.success() { @@ -253,13 +260,19 @@ fn alter(rt: &Runtime, path: StoreId, add: Option>, rem: Option Result<()> { +fn list(path: StoreId, rt: &Runtime, has_subcommand: bool) -> Result<()> { let entry = rt.store().get(path.clone())?.ok_or_else(|| err_msg("No entry found"))?; - let scmd = rt.cli().subcommand_matches("list").unwrap(); // safe, we checked in main() - let json_out = scmd.is_present("json"); - let line_out = scmd.is_present("linewise"); - let sepp_out = scmd.is_present("sep"); - let mut comm_out = scmd.is_present("commasep"); + let (scmd, json_out, line_out, sepp_out, mut comm_out) = if has_subcommand { + let scmd = rt.cli().subcommand_matches("list").unwrap(); + let json_out = scmd.is_present("json"); + let line_out = scmd.is_present("linewise"); + let sepp_out = scmd.is_present("sep"); + let comm_out = scmd.is_present("commasep"); + + (Some(scmd), json_out, line_out, sepp_out, comm_out) + } else { + (None, false, false, false, false) + }; if !vec![json_out, line_out, comm_out, sepp_out].iter().any(|v| *v) { // None of the flags passed, go to default @@ -279,7 +292,7 @@ fn list(path: StoreId, rt: &Runtime) -> Result<()> { } if sepp_out { - let sepp = scmd.value_of("sep").unwrap(); // we checked before + let sepp = scmd.map(|s| s.value_of("sep").unwrap()).unwrap_or(""); writeln!(rt.stdout(), "{}", tags.join(sepp))?; } -- cgit v1.2.3