Is it poss: use clap with derive but set the about programmatically?

I am using clap's derive API:

#[derive(Parser, Debug)]
#[clap(
    name = "myapp",
    version,
    about = "about this app"
)]
pub struct Cli {
...
}

fn get_config() -> Config {
   let cli = Cli::parse();
   ...
}

However, I would really like to set the Cli Parser's about (or long_about) programmatically. Is this possible?

You should be able to get the Command struct with the CommandFactory trait derived as part of Parser. And then add your about section and follow the docs to get back your struct.

Example

I can do the first bit:

let command = Cli::command();
let _ = command.about(get_about());

But I can't work out the syntax for the second part to parse the args in the Cli struct.

I've updated my example.

Thank you! It now works :slight_smile:

Look closer at the example and your error.

You still have a Command struct, you need to get the matches, you should call the get_matches method of Command.

Yes, I realised I needed the get_matches() call. Thanks again!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.