argh
argh copied to clipboard
Default subcommand
Is it possible to make a default subcommand with argh
? I want something like this:
#[derive(FromArgs, PartialEq, Debug)]
/// Top-level command.
struct TopLevel {
#[argh(subcommand)]
nested: Sub,
}
#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand)]
enum Sub {
#[argh(default)] // <-----
One(SubCommandOne),
Two(SubCommandTwo),
}
/* one and two subcommands*/
With
-
cmd one ...
resulting inSub::One(..)
-
cmd two ...
resulting inSub::Two(..)
-
cmd ...
resulting inSub::One(..)
(the default)
this is a valid way to do this i think:
#[derive(FromArgs, PartialEq, Debug)]
/// Top-level command.
struct TopLevel {
#[argh(subcommand)]
nested: Option<Sub>,
}
you can just do an unwrap_or
on the Option
which is properly set to None
if no subcommand is passed.
@mraerino I don't think this would work. If subcommand doesn't match (i.e.: default should be used) then your code will lose all arguments.
True. You wouldn't be able to use args on your default command. But global args still work. My expectation is that if you don't specify a subcommand you likely don't need any args specific to it.
My expectation is that if you don't specify a subcommand you likely don't need any args specific to it.
FWIW, I did. I was adding subcommands to an existing tool, and wanted one subcommand to be the default so that the tool would function as it did before if no subcommand was given. Its arguments shouldn't be valid for other subcommands, though.