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

Support allow_hyphen_values #41

Closed
coder543 opened this issue Dec 15, 2017 · 9 comments
Closed

Support allow_hyphen_values #41

coder543 opened this issue Dec 15, 2017 · 9 comments
Labels

Comments

@coder543
Copy link

coder543 commented Dec 15, 2017

Currently, providing a negative value for an integer parameter results in an error about an unexpected argument. It would be nice if structopt supported clap's allow_hyphen_values option. structopt actually seems to, but only on Nightly, since you have to do...

#[structopt(allow_hyphen_values=true, long = "timezone", default_value = "-4", help = "Specifies the timezone as the hours of offset from UTC.")]

and that literal true value requires #![feature(attr_literals)] to work currently.

@TeXitoi
Copy link
Owner

TeXitoi commented Dec 15, 2017

On mobile, but I'll try to explain. Use the _raw variant : allow_hyphen_value_raw = "true"

Does it fixes your issue ?

@TeXitoi
Copy link
Owner

TeXitoi commented Dec 15, 2017

Side note: timezone can be half and quarter of an hour. Look at India :'-(

@azriel91
Copy link

In case someone else stumbles upon this, you can go:

#[derive(StructOpt)]
#[structopt(name = "app"))]
struct Opt {
    // ...

    // Pass the rest of the arguments through
    #[structopt(raw(allow_hyphen_values = "true"))]
    rest_of_args: Vec<String>,
}

The command needs to be invoked like so:

# the '--' separator is important
$ app -- rest_of_args_1 --rest_of_args_option

@caiohamamura
Copy link

caiohamamura commented Nov 7, 2019

It was not working here because I was deriving Debug for struct Opt, after I dropped it, it worked but without the raw clause.

@TeXitoi
Copy link
Owner

TeXitoi commented Nov 8, 2019

Raw is no more a thing in 0.3

@PvdBerg1998
Copy link

#[structopt(allow_hyphen_values(true))] compiles, but doesnt work.

@TeXitoi
Copy link
Owner

TeXitoi commented Dec 29, 2020

@PvdBerg1998 please provide a more comprehensive example, that compiles, with a call explaining what you have, and what you're expecting. Also, adding a comment in a closed issue might not be noticed.

@PvdBerg1998
Copy link

I'm expecting the argument to get parsed with a hyphen in front, e.g. "-20".

I've found out this works with a negative x or y:

#[derive(Clone, Debug, PartialEq, Eq, StructOpt)]
#[structopt(setting = structopt::clap::AppSettings::AllowLeadingHyphen)]
struct Args {
    x: String,
    y: String
}

But this does not, although it does compile like I said:

#[derive(Clone, Debug, PartialEq, Eq, StructOpt)]
struct Args {
    #[structopt(allow_hyphen_values(true))]
    x: String,
    #[structopt(allow_hyphen_values(true))]
    y: String
}

@kkharji
Copy link

kkharji commented Apr 12, 2021

I'm having the same issue

    #[structopt(default_value = "0", allow_hyphen_values = true)]
    date: Vec<String>,

Also when I tried @PvdBerg1998 solution it gives me an error saying setting I get no named method.

I'm trying to pass -1

cc @TeXitoi

edit:

Okay this #[structopt(alias = "edit", setting = AppSettings::AllowNegativeNumbers)] fixed it for me.

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

No branches or pull requests

6 participants