Custom version output with clap

I'd like to mimic the behavior of of -h vs --help for -V vs --version, in that I'd like -V to give just application name an version, but --version outputs more information.

I would settle for requiring -v/--verbose for the long version output, rather than activating it via --version.

But I'm unsure how to accomplish any of these.

I think I could accomplish the --verbose variant by simply disabling the default "version" logic:

#[derive(Parser, Debug)]
#[command(disable_version_flag = true)]
struct Args {
  ...
}

.. and then just add my own option for -V and --version.

But how would one go about implementing the other one -- where -V and --version are basically two different options, but are treated as one (with regards to help texts and such)? Is this something ArgGroups can be used for?

Taking a step back .. are there some other mechanisms in clap that can help me achieve this?

Wow, so what happened before I wrote the original post was that I had searched for some way to do this, and I saw someone mention long_version. But when I searched for it in the reference I couldn't find it. No idea why, but now I'm thinking I may have misspelled it in some really bad way? Anyway, I figured it was some old clap 3 feature or something, but was no longer available.

Turns out, it's very much in clap 4.x.

So the answer is use Command::long_version()/Command::render_long_version().

2 Likes