Clap: How can subcommands automatically use the same version as the parent command?

Created on 1 Mar 2016  路  5Comments  路  Source: clap-rs/clap

Hi there.

I am writing an app of a form like:

let args = App::new("blast")
                   .version("1.2.3")
                   .subcommand(SubCommand::with_name("status").help("help str"))
                   .get_matches();

And it appears blast status -h will suggest -V as a flag for blast status, but if you actually try this, it will just print the version as an empty string. I'm not sure if it necessarily needs to inherit from the app version, but it looks a little weird listing it in the subcommand's help if it is not set.

I am using:

  • clap = "2.1.2"
  • rustc 1.6.0 (c30b771ad 2016-01-19)

(Btw, other than this slight quirk, this library is fantastic. Thanks for your hard work.)

RFC / question

Most helpful comment

@clux Sorry, my free time has been non-existent lately!

Yes, you can use the AppSettings::VersionlessSubcommands enum variant to remove the version flag entirely.

All 5 comments

By default subcommands don't inherit the version of the parent command, this allows them to be developed as a separate program entirely (should that ever be required).

You can, however, tell clap to simply propagate the version of the parent down through all child subcommands if you'd like them to all have the same version.

To do this, you use the AppSettings::GlobalVersion enum variant along with the App::setting method.

let args = App::new("blast")
                   .setting(AppSettings::GlobalVersion) // all subcommands have version "1.2.3" now
                   .version("1.2.3")
                   .subcommand(SubCommand::with_name("status").help("help str"))
                   .get_matches();

Ah, I see. That's one useful solution.

Alternatively though; is there a way to remove the version from showing up in subcommand help?

./blast -h and ./blast status -h will both suggest looking at version even if the app was missing a .version() call. That is still misleading, and pollutes the help of small subcommands with few flags.

@clux Sorry, my free time has been non-existent lately!

Yes, you can use the AppSettings::VersionlessSubcommands enum variant to remove the version flag entirely.

Ooh, perfect. Thank you very much. I had entirely missed the documentation of that enum. Lots of useful stuff in there! I will close this now as it works perfectly.

:+1:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kbknapp picture kbknapp  路  23Comments

XAMPPRocky picture XAMPPRocky  路  17Comments

CreepySkeleton picture CreepySkeleton  路  17Comments

neysofu picture neysofu  路  41Comments

ruabmbua picture ruabmbua  路  17Comments