Search code examples
rustcommand-line-argumentsclap

How to make clap ignore a specific field in a struct?


I'm building my Rust CLI with clap.

I have one struct with all the command line options. This struct is passed through to a lot of functions.

Now I would like to add a field to this struct, but not surface this field as a command line argument. I don't want to pass through an additional struct just for a single field.

Is there some clap syntax that allows clap to ignore a field in the struct, and just set it to some default?

This is a sample struct:

#[derive(Parser, Debug)]
pub struct AlignPairwiseParams {
  #[clap(long)]
  #[clap(default_value_t = AlignPairwiseParams::default().min_length)]
  pub min_length: usize,

  // I would like this field to be not surface through the CLI. How to do this?
  pub internal_use_only: bool,
}

Does something like #[clap(ignore)] exist?


Solution

  • From the derive reference:

    Arg Attributes

    • ...
    • skip [= <expr>]: Ignore this field, filling in with <expr>
      • Without <expr>: fills the field with Default::default()

    So #[clap(skip)].