diff options
| author | Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> | 2025-12-14 09:42:27 +0200 |
|---|---|---|
| committer | Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> | 2025-12-15 23:54:40 +0200 |
| commit | 10eddca5775e90653e89b1a9e81e213a36134c10 (patch) | |
| tree | 03c137116f88afd27e7df3ccd1c75f9e35aad822 | |
| parent | 1f06564b902b9ad53079c9962514659ca3ec04c2 (diff) | |
mu-labels: improve command-line handling
Esp., better handle the 'no subcommand' case: give the user some more useful
help.
| -rw-r--r-- | mu/mu-cmd-labels.cc | 8 | ||||
| -rw-r--r-- | mu/mu-options.cc | 23 | ||||
| -rw-r--r-- | mu/mu-options.hh | 2 |
3 files changed, 13 insertions, 20 deletions
diff --git a/mu/mu-cmd-labels.cc b/mu/mu-cmd-labels.cc index 9f373e3..95c8c7e 100644 --- a/mu/mu-cmd-labels.cc +++ b/mu/mu-cmd-labels.cc @@ -168,7 +168,10 @@ label_import(Mu::Store& store, const Options& opts) Result<void> Mu::mu_cmd_labels(Mu::Store &store, const Options &opts) { - switch (opts.labels.sub) { + if (!opts.labels.sub) + return Ok(); // nothing to do. + + switch (*opts.labels.sub) { case Options::Labels::Sub::List: return label_list(store, opts); case Options::Labels::Sub::RestoreList: @@ -181,7 +184,6 @@ Mu::mu_cmd_labels(Mu::Store &store, const Options &opts) return label_export(store, opts); case Options::Labels::Sub::Import: return label_import(store, opts); - default: return Err(Error{Error::Code::Internal, "invalid sub-command"}); @@ -191,7 +193,7 @@ Mu::mu_cmd_labels(Mu::Store &store, const Options &opts) #ifdef BUILD_TESTS /* - * Tests. + * Tests... * */ #include <config.h> diff --git a/mu/mu-options.cc b/mu/mu-options.cc index ac9d885..2690529 100644 --- a/mu/mu-options.cc +++ b/mu/mu-options.cc @@ -496,9 +496,8 @@ sub_init(CLI::App& sub, Options& opts) static void sub_labels(CLI::App& sub, Options& opts) { - sub.require_subcommand(0); + sub.require_subcommand(0, 1); - // update auto update{sub.add_subcommand("update", "update labels")}; update->add_option("--labels", opts.labels.delta_labels, "One or more comma-separated +label,-label") @@ -506,12 +505,11 @@ sub_labels(CLI::App& sub, Options& opts) ->type_name("<delta-label>") ->required(); update->add_flag("-n,--dry-run", opts.labels.dry_run, - "Output what would change without changing anything"); + "Output what would change without changing anything"); update->add_option("query", opts.labels.query, "Query for messages to update") ->required(); add_muhome_option(*update, opts); - // clear auto clear = sub.add_subcommand("clear", "clear all labels from matched messages"); clear ->add_option("query", opts.labels.query, "Query for messages to clear of labels") ->required(); @@ -519,22 +517,18 @@ sub_labels(CLI::App& sub, Options& opts) "Output what would change without changing anything"); add_muhome_option(*clear, opts); - // list [[maybe_unused]] auto list = sub.add_subcommand("list", "list labels in the store"); add_muhome_option(*list, opts); - // restore-list [[maybe_unused]] auto restore_list = sub.add_subcommand( "restore-list", "restore the labels cache"); add_muhome_option(*restore_list, opts); - // export [[maybe_unused]] auto exportsub = sub.add_subcommand("export", "export labels to a file"); add_muhome_option(*exportsub, opts); exportsub->add_option("output", opts.labels.file, "File to export labels to") ->type_name("<file>"); - // import auto importsub = sub.add_subcommand("import", "import labels from a file"); importsub->add_flag("-n,--dry-run", opts.labels.dry_run, "Output what would change without changing anything"); @@ -543,12 +537,6 @@ sub_labels(CLI::App& sub, Options& opts) ->type_name("<file>"); add_muhome_option(*importsub, opts); - // XXX: it'd be nice to make "update" the default command, such that - // mu label foo --labels +a,-b - // would be interpreted as - // mu label update foo --labels +a,-b - // but no succeeded yet; CLI11 treats 'foo' as unknown sub-command - sub.final_callback([&](){ if (sub.got_subcommand("list")) { opts.labels.sub = Options::Labels::Sub::List; @@ -568,6 +556,9 @@ sub_labels(CLI::App& sub, Options& opts) } else if (sub.got_subcommand("import")){ opts.labels.sub = Options::Labels::Sub::Import; opts.labels.read_only = opts.labels.dry_run; + } else { + mu_println("{}", sub.help()); + opts.labels.read_only = true; } }); } @@ -905,8 +896,8 @@ Copyright (C) 2008-2025 Dirk-Jan C. Binnema License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. -There is NO WARRANTY, to the extent permitted by law. -)"); +There is NO WARRANTY, to the extent permitted by law.)"); + app.set_version_flag("-V,--version", PACKAGE_VERSION); app.set_help_flag("-h,--help", "Show help information"); app.set_help_all_flag("--help-all"); diff --git a/mu/mu-options.hh b/mu/mu-options.hh index 9bef3db..e8f8da3 100644 --- a/mu/mu-options.hh +++ b/mu/mu-options.hh @@ -220,7 +220,7 @@ struct Options { Export, // export labels Import, // import labels }; - Sub sub; + Option<Sub> sub; // the chosen sub-command } labels; /* |
