cli: add subcommands using clap's builder API
I want to make most of the `dispatch()` code unaware of which commands
exist, so it becomes easier to tweak that set of commands in a custom
binary.
@@ -24,7 +24,7 @@ use std::time::Instant;
use std::{fs, io};
use chrono::{FixedOffset, TimeZone, Utc};
-use clap::{ArgGroup, CommandFactory, Subcommand};
+use clap::{ArgGroup, CommandFactory, FromArgMatches, Subcommand};
use config::Value;
use criterion::Criterion;
use git2::{Oid, Repository};
@@ -1059,8 +1059,6 @@ fn update_working_copy(
struct Args {
#[clap(flatten)]
global_args: GlobalArgs,
- #[clap(subcommand)]
- command: Commands,
}
#[derive(clap::Args, Clone, Debug)]
@@ -1123,7 +1121,7 @@ struct GlobalArgs {
color: Option<ColorChoice>,
}
-#[derive(Subcommand, Clone, Debug)]
+#[derive(clap::Parser, Clone, Debug)]
enum Commands {
Version(VersionArgs),
Init(InitArgs),
@@ -5685,15 +5683,18 @@ pub fn dispatch(ui: &mut Ui, args_os: ArgsOs) -> Result<(), CommandError> {
}
}
- let app = Args::command();
+ let app: clap::Command = Commands::augment_subcommands(Args::command());
+ let app = app.arg_required_else_help(true);
let string_args = resolve_aliases(&app, ui.settings(), &string_args)?;
- let args: Args = clap::Parser::parse_from(&string_args);
+ let matches = app.clone().get_matches_from(&string_args);
+ let args: Args = Args::from_arg_matches(&matches).unwrap();
if let Some(choice) = args.global_args.color {
// Here we assume ui was created for_terminal().
ui.reset_color_for_terminal(choice);
}
- let command_helper = CommandHelper::new(app, string_args, args.global_args.clone());
- match &args.command {
+ let command_helper = CommandHelper::new(app, string_args, args.global_args);
+ let derived_subcommands: Commands = Commands::from_arg_matches(&matches).unwrap();
+ match &derived_subcommands {
Commands::Version(sub_args) => cmd_version(ui, &command_helper, sub_args),
Commands::Init(sub_args) => cmd_init(ui, &command_helper, sub_args),
Commands::Checkout(sub_args) => cmd_checkout(ui, &command_helper, sub_args),
@@ -5739,6 +5740,7 @@ mod tests {
#[test]
fn verify_app() {
- Args::command().debug_assert();
+ let app = Commands::augment_subcommands(Args::command());
+ app.debug_assert();
}
}
