argh icon indicating copy to clipboard operation
argh copied to clipboard

Default subcommand

Open WaffleLapkin opened this issue 3 years ago • 4 comments

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 in Sub::One(..)
  • cmd two ... resulting in Sub::Two(..)
  • cmd ... resulting in Sub::One(..) (the default)

WaffleLapkin avatar Oct 04 '20 21:10 WaffleLapkin

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 avatar Nov 06 '20 00:11 mraerino

@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.

WaffleLapkin avatar Nov 06 '20 20:11 WaffleLapkin

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.

mraerino avatar Nov 06 '20 21:11 mraerino

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.

tjkirch avatar Nov 06 '20 22:11 tjkirch