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

one_of's are not exclusive #32

Open
Davis-A opened this issue Mar 27, 2020 · 4 comments
Open

one_of's are not exclusive #32

Davis-A opened this issue Mar 27, 2020 · 4 comments

Comments

@Davis-A
Copy link

Davis-A commented Mar 27, 2020

Exclusive one of options are not throwing an error in some combinations.

Code below:

#!/usr/bin/perl

use strict;
use ME;
use DateTime::Format::Strptime;
use Getopt::Long::Descriptive;


my ($opt, $usage) = describe_options(
    '%c %o',
    [ "mode" => hidden => { one_of => [
        [ "ten-minute|t"  => "10 minute truncation" ],
        [ "minute|m"  => "1 minute truncation" ],
        [ "hour|h" => "hour truncation" ],
    ] } ],
    [ 'help', "print usage and exit", { shortcircuit => 1 } ],
    { show_defaults => 1 },
);
print($usage->text), exit if $opt->help;

print $opt->mode . "\n";

Example running and output:

[adavis@box ~]# ./script.pl --help
 script.pl [-hmt] [long options...]
        -t --ten-minute  10 minute truncation
        -m --minute      1 minute truncation
        -h --hour        hour truncation
        --help           print usage and exit
[adavis@box ~]# ./script.pl --ten-minute
ten_minute
[adavis@box ~]# ./script.pl --minute
minute
[adavis@box ~]# ./script.pl --hour
hour
[adavis@box ~]# ./script.pl --ten-minute --minute
minute
[adavis@box ~]# ./script.pl --ten-minute --hour
hour
[adavis@box ~]# ./script.pl --minute --hour
these options conflict; each wants to set the mode: minute hour
[adavis@box ~]# ./script.pl --minute --hour --ten-minute
these options conflict; each wants to set the mode: hour minute
[adavis@box ~]#

Individually they show the correct behavior, set the $opt->mode.

But when combined in this case the top option --ten-minute never conflicts with the next two.

@bpj
Copy link

bpj commented May 28, 2021

A reasonable workaround until this bug is fixed:

die "The --foo, --bar and --baz options are mutually exclusive.\n" . $usage->text
    if 1 < grep defined($opt->$_), qw(foo bar baz);

die "You must give exactly one of the --foo, --bar and --baz options.\n" . $usage->text 
    unless 1 == grep defined($opt->$_), qw(foo bar baz);

@szr8
Copy link

szr8 commented May 28, 2021

@bpj I think you may have dropped these: ,,

In all seriousness, it really looks better with the oxford comma:

die "The --foo, --bar, and --baz options are mutually exclusive.\n" . $usage->text
    if 1 < grep defined($opt->$_), qw(foo bar baz);

die "You must give exactly one of the --foo, --bar, and --baz options.\n" . $usage->text 
    unless 1 == grep defined($opt->$_), qw(foo bar baz);

@bpj
Copy link

bpj commented May 29, 2021

@szr8 I guess I have some gut reaction against separating options with anything but space! 😃 The string has to be adapted to the needs/whims of the program/programmer anyway. The code works.

@abraxxa
Copy link

abraxxa commented May 16, 2024

I just stumbled over the same issue which seems to come from having a dash in the option name.

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

4 participants