Parsing Enum with values using Clap

I have a cli with this


#[derive(Subcommand)]
pub enum Arguments {
    #[clap(about = "Handle quadratic equations")]
    Quadratic {
       // #[clap(subcommand)]
      //  subcommand: QuadraticsCommands,

        #[clap(flatten)]
        arguments : QuadraticArguments
    },
}

Where I would like QuadraticArguments to be an enum like this as I need 2 possible input 'cases'

#[derive(Parser)]
pub enum QuadraticArguments {
    Input {
        #[arg(short,long)]
        input : String
    },
    Named {
        #[arg(short)]
        a : Option<String>,
        #[arg(short)]
        b : Option<String>,
        #[arg(short)]
        c : Option<String>, 
    }
}

So I have tried to derive ArgEnum , ValueEnum but both don't compile / work.

I have tried searching the answer on different websites and even the clap documentations but I see no mention to solve this. Thank you for your help in advance !

What do you want the invocation to look like? Do you want to input and named to be further subcommands to the quadratic subcommand?

No QuadraticArguments and its fields ilke QuadraticArguments::Input and QuadraticArguments::Named should be possible arguments given like
The general way it should be

  • cargo run quadratic -i "input"
  • cargo run quadratic -a 2 -b 3

The cargo run quadratic part is done , and not the issue

I think you want groups and conflicts.

Yes that is what I want , but is there a way to create a struct for a-c kind of like this

#[derive(clap::Parser,Clone)]
#[group(id = "named")]
pub struct QuadraticArguments {
    #[arg(short)]
    a: Option<String>,
    #[arg(short)]
    b: Option<String>,
    #[arg(short)]
    c: Option<String>, 
}

#[derive(Subcommand)]
pub enum Arguments {


    #[clap(about = "Handle quadratic equations")]
    Quadratic {
      //  #[clap(subcommand)]
      //  subcommand: QuadraticsCommands,

        #[arg(short, long, group = "unnamed", conflicts_with = "named")]
        input: Option<String>,

        #[arg(short, long)]
        arguments : QuadraticArguments
    },
}

I tried running but I get

the method `value_parser` exists for reference `&&&&&&_AutoValueParser<QuadraticArguments>`, but its trait bounds were not satisfied
the following trait bounds were not satisfied:
`QuadraticArguments: ValueEnum`
which is required by `&&&&&_AutoValueParser<QuadraticArguments>: clap::builder::via_prelude::_ValueParserViaValueEnum`
`QuadraticArguments: ValueParserFactory`
which is required by `&&&&&&_AutoValueParser<QuadraticArguments>: clap::builder::via_prelude::_ValueParserViaFactory`
`QuadraticArguments: From<OsString>`
which is required by `&&&&_AutoValueParser<QuadraticArguments>: clap::builder::via_prelude::_ValueParserViaFromOsString`
`QuadraticArguments: From<&'s std::ffi::OsStr>`
which is required by `&&&_AutoValueParser<QuadraticArguments>: clap::builder::via_prelude::_ValueParserViaFromOsStr`
`QuadraticArguments: From<std::string::String>`
which is required by `&&_AutoValueParser<QuadraticArguments>: clap::builder::via_prelude::_ValueParserViaFromString`
`QuadraticArguments: From<&'s str>`
which is required by `&_AutoValueParser<QuadraticArguments>: clap::builder::via_prelude::_ValueParserViaFromStr`
`QuadraticArguments: FromStr`
which is required by `_AutoValueParser<QuadraticArguments>: clap::builder::via_prelude::_ValueParserViaParse`

I tried implementing FromStr but then I still get errors, and I am sure there a better way to do this , then implementing those 5 traits seperatly

#[clap(flatten)]

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.