-
-
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
Remove magic arg type in Args
#2687
Comments
Note to self: when implementing this, look into also implementing #2688 |
The idea of using helper constructors |
From the discussion,
|
Yeah, I got the idea from ripgrep which I copied until I switched to |
@pksunkara I was thinking more about the long game of leave Just for backstory; I think the point I was trying to make in the thread between @epage and I was that currently since But this minor constructor problem is indicative of certain other similar cases in clap that can/do either cause confusion for the consumer, or ambiguities for clap parsing itself. Ambiguities aren't the worst thing, but they lead to implicit parsing rules, that if not specified somewhere can cause subtle breakage or cause unintended consequences as the API grows or evolves. clap's original design(tm) was deliberately different from something like Hopefully that makes sense, or at least captures some the "why" from the early days 😄 |
Thanks for the background! Unless someone has an alternative idea on how things might work, I don't think API optionsName-onlyfn positional(name: &str);
fn flag(name: &str);
fn option(name: &str);
Option-all-the-wayfn postional(name: &str);
fn flag(name: &str, long: Option<&str>, short: Option<char>);
fn option(name: &str: long: Option<&str>, short: Option<char>);
Helperfn new(name: &str);
fn postional(name: &str);
fn flag(name: &str, long: &str);
fn option(name: &str: long: &str);
|
Could the constructors take a trait, implemented on &str and char, such that "abc" is a long option and 'a' is a short option? You could also implement it for an array, allowing |
I feel like I'm missing something in looking back at this issue. My original proposal was to make
Couldn't quite do it with an array (since the types are different) but you could with a tuple. |
Right, sorry. |
what if you had an api like: fn postional(name: &str);
fn flag(name: &str, flagName: FlagName);
fn option(name: &str, flagName: FlagName);
enum FlagName {
Short(char),
Long(&'static str),
LongAndShort(&'static str, char),
} (I'm sure someone else can come up with a better name). I think changing |
Postponing to 5.0 because I realized its changing too many variables on people as they upgrade, making it more difficult, since they already need to switch from |
#2688 is already getting gnarly and this will help in communicating it. I think I'm going to pull it back in |
This removes the need for `TakesValue` bookkeeping for when we knew we took values but didn't know how many we should take. Fixes clap-rs#2687
This removes the need for `TakesValue` bookkeeping for when we knew we took values but didn't know how many we should take. Fixes clap-rs#2687
Discussed in #2674
Originally posted by epage August 9, 2021
Arg::new()
creates a positional argument thattakes_value(true)
Args::new().long()
creates a named flag (takes_value(false)
)Args::new().long().takes_value(true)
is required to created a named arg / optionThis requires us to guess the users intent at various stages. kbknapp explained this when trying to explain existing challenges with
values
vsoccurrences
:I'm even running into a problem I'm debugging for #751 that I think comes from this.
Just a small tweak would make clap less brittle for change and I believe make the user experience better, if we switch to
Arg::new()
creates a positional argument thattakes_value(true)
Args::new().long()
creates a named arg / option (ietakes_value(true)
)Args::new().long().takes_value(false)
is required to created a flag@kbknapp was open to changing this, though with a different approach (this was before clarifying the approach above, see below for his thoughts on each approach)
We could fix the "always valid object" problem by instead making some helper constructors (
positional
,flag
, andoption
where the latter take long names since 99% of the time, they are used)Between flag-opt-in and construct invalid options (I didn't bring up the named constructors), kbknapp said
The text was updated successfully, but these errors were encountered: