Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default subcommand #57

Open
WaffleLapkin opened this issue Oct 4, 2020 · 4 comments
Open

Default subcommand #57

WaffleLapkin opened this issue Oct 4, 2020 · 4 comments

Comments

@WaffleLapkin
Copy link

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)
@mraerino
Copy link

mraerino commented Nov 6, 2020

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.

@WaffleLapkin
Copy link
Author

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

@mraerino
Copy link

mraerino commented Nov 6, 2020

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.

@tjkirch
Copy link

tjkirch commented Nov 6, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants