Program option taking multiple values with subcommands #1707
Replies: 8 comments
-
I am using clap derive, and I do not find anything about global setting, I think that's what I am looking for. |
Beta Was this translation helpful? Give feedback.
-
Aghm... I'm not sure I really understand what you mean, but something like this will work as you requested: use clap::{Clap, Subcommand};
#[derive(Clap)]
struct Opt {
#[clap(long)]
option_general: String,
#[clap(subcommand)]
sub: Subcommands
}
#[derive(Subcommand)]
enum Subcommands {
Subcommand {
#[clap(long)]
option_for_subcommand: String
}
} |
Beta Was this translation helpful? Give feedback.
-
Yes, that's what I have done, but the result is frustrated. I think this may be some bug for 3.0, I use App in 2.33 to build my options, it's all ok |
Beta Was this translation helpful? Give feedback.
-
Phfff... OK, Could you please give me a detailed explanation of what you want, like
Something like that. I'm having really hard time struggling with your description. If you want others to get to understanding your problem, you need to explain what is expected, in detail, step by step, comprehensive.
If this is what you want, use |
Beta Was this translation helpful? Give feedback.
-
I have figured it out. I defined Vec type in my struct, all that's all the mess is. When I remove it from Opt, everything is fine. Could this be a bug? |
Beta Was this translation helpful? Give feedback.
-
Could you please post the whole snippet? |
Beta Was this translation helpful? Give feedback.
-
use clap::Clap;
#[derive(Clap)]
struct Opts {
#[clap(long)]
option_general: String,
#[clap(subcommand)]
sub: Subcommands,
#[clap(long, required = true)]
files: Vec<String>,
}
#[derive(Clap)]
enum Subcommands {
Subcommand {
#[clap(long)]
option_for_subcommand: String
}
}
fn main() {
let opt = Opts::parse();
} Please test this code, it will fail |
Beta Was this translation helpful? Give feedback.
-
Well, this is kind of expected. If you have an option taking multiple values followed by a subcommand, there's no way to detect where the values end and where the subcommand starts. So, use the second variant and not the first one. I'm closing this until you post a clear comprehensive explanation of what behavior you expect. |
Beta Was this translation helpful? Give feedback.
-
What should I do if I want something like this?
./program --optiona-general xxx subcommand --option-for-subcommand yy
Beta Was this translation helpful? Give feedback.
All reactions