Skip to content
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

Flag with optional value is parsed differently from getopt_long #49

Closed
kornelski opened this issue Jun 19, 2017 · 1 comment
Closed

Comments

@kornelski
Copy link
Contributor

When using C getopt_long a long option with optional_argument will have a value only if --flag=value syntax is used. --flag arg is parsed as a flag without an argument, and a free argument (I've verified that on macOS and Linux/glibc).

getopts' optflagopt behaves differently. It always takes the next argument as the value, even if the = syntax is not used.

This is problematic for me, because I can't faithfully port C getopt_long program to Rust. I also think it's a less useful behavior, because it makes it impossible to set a optflagopt flag without a value if it's not the last/only argument.

extern crate getopts;

fn main() {
    let mut o = getopts::Options::new();
    o.optflagopt("", "value", "", "");
    
    let m = o.parse(&["--value", "foo", "bar"]).unwrap();
    println!("with space {:?} + {:?}", m.opt_str("value"), m.free);

    let m = o.parse(&["--value=foo", "bar"]).unwrap();
    println!("with = {:?} + {:?}", m.opt_str("value"), m.free);
}

This prints:

with space Some("foo") + ["bar"]
with = Some("foo") + ["bar"]

I'd prefer it to parse as:

with space None + ["foo", "bar"]
with = Some("foo") + ["bar"]

@tromey
Copy link
Contributor

tromey commented Aug 28, 2017

It seems like a buglet to me as well, given the existence of ParsingStyle::FloatingFrees.

jridgewell added a commit to jridgewell/getopts-rust that referenced this issue May 16, 2019
Simliar to rust-lang#49, this changes the parsing of short options to require no space between the option and argument. Now, `-aSomething` parse as an option with argument, and `-a Something` will parse as a present flag with a free non-option.
jridgewell added a commit to jridgewell/getopts-rust that referenced this issue May 16, 2019
Similar to rust-lang#49, this changes the parsing of short options to require no space between the option and argument. Now, `-aSomething` parse as an option with argument, and `-a Something` will parse as a present flag with a free non-option.

This is a breaking change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants