0

I have this example:

use clap::Parser;

#[derive(clap::ValueEnum, Clone, Debug)]
enum InfoType {
    All,
    Headers,
    Metadata,
}

#[derive(Parser, Debug)]
#[clap(version)]
#[clap(about = "Prints out metadata for each file")]
struct Args {
    #[clap(long)]
    #[clap(help = "What to print")]
    #[clap(value_enum, default_value_t=InfoType::All)]
    info: InfoType,

    #[clap(value_parser, required(true))]
    #[clap(help = "file(s)")]
    files: Vec<String>,
}

fn main() {
    let args = Args::parse();
    println!("args {:?}", args)
}

Here the cli interface becomes almost but not quite what I want

USAGE:
    my_test [OPTIONS] <FILES>...

ARGS:
    <FILES>...    file(s)

OPTIONS:
    -h, --help           Print help information
        --info <INFO>    What to print [default: all] [possible values: all, headers, metadata]
    -V, --version        Print version information

Now this can be run as e.g.

my_test /path/file1 /path/file2 # enables the --info all by default
my_test --info headers /path/file1 /path/file2

What I would like instead is to have the --info <INFO> flag just as --metadata , --all etc. So I can run

my_test /path/file1 /path/file2 # enables the --all all by default
my_test --info /path/file1 /path/file2

But still parsing those flags to the same enum variable - how would I set up this with clap ?

binary01
  • 1,728
  • 2
  • 13
  • 27
  • Probably relevant: [how to create mutually exclusive flags in clap](https://github.com/clap-rs/clap/blob/v3.1.18/examples/tutorial_derive/README.md#argument-relations) – Finomnis Sep 28 '22 at 08:50
  • @Finomnis I've seen that, and it uses 4 variables for the mutually exclusive flags - I can't seem to figure out how only get those flags as a single enum variable though – binary01 Sep 28 '22 at 09:14
  • 2
    There's an open discussion here: https://github.com/clap-rs/clap/issues/2621 – Finomnis Sep 28 '22 at 09:18
  • 2
    Seems to be in the roadmap for `clap 4.x`. – Finomnis Sep 28 '22 at 09:20

0 Answers0