-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: App::arg_add_or_modify method #1730
Conversation
Hmm.. I am not convinced this should be in the library. Or maybe this is not how it should be implemented. I understand the use case, it's just I am trying to think of a better way to do this. |
This was definitely a patch of frustration rather than feature design. 😄 If you have a better design or alternative way to achieve this -- specifically, when the |
@jhwgh1968 why can't you just set in in structopt? Or do you mean the app is generated by third party and you simply can't change the |
I will set up a small reproduction repo when I am back at the computer I originally posted from. In the mean time, here is the basic problem from memory. The goal is to allow this syntax: The only way I could do that was a hack, shown below:
However, the App Setting is global, so the above also allows this: That should be a syntax error, because the external command is sensitive to the position of its flags. Instead, the Rust code goes ahead with this undesired call: And the external command handles a.txt and b.txt differently. I could imagine two solutions to this problem:
Hopefully that explain it, @CreepySkeleton. If I missed something, or this is solved in Clap 3.x another way, let me know. |
(That feeling when you've been asked the question so many times you even can't remember how many times it's been, probably a billion. In fact, so many that you're starting to suspect that, when you die and end up in Hell, the first thing Satan will be going to tell you will not be - "Abandon hope all ye who enter here", no; the first unspeakable thing you'll hear will be - "Hey you, poor little soul! How do I call %some method from clap% with structopt?". Sound familiar?) You can do it via raw methods: #[derive(Debug, StructOpt)]
#[structopt(name = "example", about = "An example of multiple multi-value lists, which need different options.")]
struct Opt {
/// Activate debug mode
#[structopt(short, long)]
debug: bool,
/// The list of files to pass to the external command. Must not start with a dash.
files: Vec<String>,
/// The list of argument flags to pass to the external command. The position of these flags has an effect,
/// So they should all be at the start.
#[structopt(
long = "extern-flags",
setting = clap::AppSettings::AllowHyphens // <== HERE
)]
flags: Vec<String>
} That will do exactly what you want, but I don't recommend it. The standard for "external flags" is to use the
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
#[structopt(name = "example", about = "An example of multiple multi-value lists, which need different options.")]
struct Opt {
/// Activate debug mode
#[structopt(short, long)]
debug: bool,
/// The list of files to pass to the external command. Must not start with a dash.
files: Vec<String>,
/// The list of argument flags to pass to the external command. The position of these flags has an effect,
/// So they should all be at the start.
#[structopt(raw = true)]
flags: Vec<String>
}
fn main() {
let res = Opt::from_args();
println!("{:#?}", res);
}
Neat, isn't it? |
Sorry to make you feel like that, @CreepySkeleton! I tried using I missed Based on your explanation, I will close this PR -- and perhaps later, write a PR with documentation improvements instead. Your patience is appreciated -- and I have my own question like that the Devil would ask in my own technical work, so if it comes to that, I'll join you and we can have a party. 😄 |
I was having this issue in Clap 2.x, and am hoping for a backport. My apologies if 3.x has solved this problem already in a different way.
To quote from the documentation: