Structopt: Support allow_hyphen_values

Created on 15 Dec 2017  路  9Comments  路  Source: TeXitoi/structopt

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.

question

All 9 comments

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

Does it fixes your issue ?

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

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

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

Raw is no more a thing in 0.3

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

@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.

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
}

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TedDriggs picture TedDriggs  路  6Comments

yoshuawuyts picture yoshuawuyts  路  7Comments

liamdawson picture liamdawson  路  5Comments

lucab picture lucab  路  9Comments

SirWindfield picture SirWindfield  路  3Comments