Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)].

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading