How identify if more than one option is specified?

Discussion in 'Perl Misc' started by Hemant Shah, Sep 22, 2006.

  1. Hemant Shah

    Hemant Shah Guest

    Folks,

    Is there a easy way to find out if more than one option is specified.

    Example:

    use Getopt::Std;
    getopts(abc);
    if ($opt_a || $opt_b || $opt_c) {}

    I only want user to specify one of the options (either -a or -b -or -c).
    How do I easily check if they have specified more than one option.

    I can write "if" statement as follows:

    if (($opt_a && $opt_b && $opt_c) ||
    ($opt_a && $opt_b) ||
    ($opt_b && $opt_c) ||
    ($opt_a && $opt_c))
    {
    # print error message.
    }

    This could get ugly if the number of possible options is greater than 3.

    Is there an easier way?

    Thanks.

    --
    Hemant Shah /"\ ASCII ribbon campaign
    E-mail: \ / ---------------------
    X against HTML mail
    TO REPLY, REMOVE NoJunkMail / \ and postings
    FROM MY E-MAIL ADDRESS.
    -----------------[DO NOT SEND UNSOLICITED BULK E-MAIL]------------------
    I haven't lost my mind, Above opinions are mine only.
    it's backed up on tape somewhere. Others can have their own.
     
    Hemant Shah, Sep 22, 2006
    #1
    1. Advertising

  2. Hemant Shah

    Paul Lalli Guest

    Hemant Shah wrote:
    > Folks,
    >
    > Is there a easy way to find out if more than one option is specified.
    >
    > Example:
    >
    > use Getopt::Std;
    > getopts(abc);
    > if ($opt_a || $opt_b || $opt_c) {}
    >
    > I only want user to specify one of the options (either -a or -b -or -c).
    > How do I easily check if they have specified more than one option.
    >
    > I can write "if" statement as follows:
    >
    > if (($opt_a && $opt_b && $opt_c) ||
    > ($opt_a && $opt_b) ||
    > ($opt_b && $opt_c) ||
    > ($opt_a && $opt_c))
    > {
    > # print error message.
    > }
    >
    > This could get ugly if the number of possible options is greater than 3.
    >
    > Is there an easier way?


    This isn't significantly prettier, but...

    if (grep { defined } ($opt_a, $opt_b, $opt_c) > 1) {
    # print error message
    }

    Paul Lalli
     
    Paul Lalli, Sep 22, 2006
    #2
    1. Advertising

  3. Hemant Shah

    David Squire Guest

    Hemant Shah wrote:
    > Folks,
    >
    > Is there a easy way to find out if more than one option is specified.
    >
    > Example:
    >
    > use Getopt::Std;
    > getopts(abc);
    > if ($opt_a || $opt_b || $opt_c) {}
    >
    > I only want user to specify one of the options (either -a or -b -or -c).
    > How do I easily check if they have specified more than one option.


    If there should only ever be one, why not change your approach? Allow
    only one option, but let it be one that takes an argument, and let that
    argument select between the behaviours you are currently specifying via
    -a XOR -b XOR -c.


    DS
     
    David Squire, Sep 22, 2006
    #3
  4. Hemant Shah

    John Bokma Guest

    Hemant Shah <> wrote:

    >
    > Folks,
    >
    > Is there a easy way to find out if more than one option is
    > specified.
    >
    > Example:
    >
    > use Getopt::Std;
    > getopts(abc);
    > if ($opt_a || $opt_b || $opt_c) {}
    >
    > I only want user to specify one of the options (either -a or -b -or
    > -c). How do I easily check if they have specified more than one
    > option.


    my $option = shift;
    $option eq '-a' or $option eq '-b' or $option eq '-c'
    or die "usage: script -a|-b|-c";

    --
    John Experienced Perl programmer: http://castleamber.com/

    Perl help, tutorials, and examples: http://johnbokma.com/perl/
     
    John Bokma, Sep 22, 2006
    #4
  5. Hemant Shah

    Ben Morrow Guest

    Quoth :
    >
    > Is there a easy way to find out if more than one option is specified.
    >
    > Example:
    >
    > use Getopt::Std;
    > getopts(abc);
    > if ($opt_a || $opt_b || $opt_c) {}
    >
    > I only want user to specify one of the options (either -a or -b -or -c).
    > How do I easily check if they have specified more than one option.


    use Getopt::Declare;

    my $args = Getopt::Declare->new(<<'OPTS');

    -a Do some a-ish-type thing
    -b Do some b-ish-type thing
    -c Do some c-ish-type thing
    [mutex: -a -b -c]

    OPTS

    # (note that there should be a literal tab after each -x)

    # Ben

    --
    I must not fear. Fear is the mind-killer. I will face my fear and
    I will let it pass through me. When the fear is gone there will be
    nothing. Only I will remain.
    Frank Herbert, 'Dune'
     
    Ben Morrow, Sep 22, 2006
    #5
  6. Chris Mattern wrote:
    > In article <ef16de$non$>, Hemant Shah wrote:
    >>Folks,
    >>
    >> Is there a easy way to find out if more than one option is specified.
    >>
    >> Example:
    >>
    >> use Getopt::Std;
    >> getopts(abc);
    >> if ($opt_a || $opt_b || $opt_c) {}
    >>
    >> I only want user to specify one of the options (either -a or -b -or -c).
    >> How do I easily check if they have specified more than one option.
    >>
    >> I can write "if" statement as follows:
    >>
    >> if (($opt_a && $opt_b && $opt_c) ||
    >> ($opt_a && $opt_b) ||
    >> ($opt_b && $opt_c) ||
    >> ($opt_a && $opt_c))
    >> {
    >> # print error message.
    >> }
    >>
    >> This could get ugly if the number of possible options is greater than 3.
    >>
    >> Is there an easier way?

    >
    > Sure. Just count heads.
    >
    > my $arg_count=0;
    > $arg_count++ if $opt_a;
    > $arg_count++ if $opt_b;
    > $arg_count++ if $opt_c;


    my $arg_count = grep $_, $opt_a, $opt_b, $opt_c;


    > if ($arg_count != 1) {
    > die "You dummy, pick one and only one!"; }




    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Sep 22, 2006
    #6
  7. John W. Krahn wrote:
    > Chris Mattern wrote:
    >>In article <ef16de$non$>, Hemant Shah wrote:
    >>>Folks,
    >>>
    >>>Is there a easy way to find out if more than one option is specified.
    >>>
    >>>Example:
    >>>
    >>>use Getopt::Std;
    >>>getopts(abc);
    >>>if ($opt_a || $opt_b || $opt_c) {}
    >>>
    >>>I only want user to specify one of the options (either -a or -b -or -c).
    >>>How do I easily check if they have specified more than one option.
    >>>
    >>>I can write "if" statement as follows:
    >>>
    >>>if (($opt_a && $opt_b && $opt_c) ||
    >>> ($opt_a && $opt_b) ||
    >>> ($opt_b && $opt_c) ||
    >>> ($opt_a && $opt_c))
    >>>{
    >>> # print error message.
    >>>}
    >>>
    >>>This could get ugly if the number of possible options is greater than 3.
    >>>
    >>>Is there an easier way?

    >>Sure. Just count heads.
    >>
    >>my $arg_count=0;
    >>$arg_count++ if $opt_a;
    >>$arg_count++ if $opt_b;
    >>$arg_count++ if $opt_c;

    >
    > my $arg_count = grep $_, $opt_a, $opt_b, $opt_c;


    Or more correctly:

    my $arg_count = grep defined, $opt_a, $opt_b, $opt_c;



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Sep 22, 2006
    #7
  8. Hemant Shah

    Dr.Ruud Guest

    Hemant Shah schreef:


    > [Getopt::Std]
    > Is there a easy way to find out if more than one option is
    > specified.


    Yes: allow only one option, with an argument (as David already said).

    Of course there are many examples of popular executables with exclusive
    global options, like the -E and -G of grep. In such case, the last one
    should win.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Sep 23, 2006
    #8
  9. Hemant Shah

    Hemant Shah Guest

    While stranded on information super highway Chris Mattern wrote:
    > In article <ef16de$non$>, Hemant Shah wrote:
    >>
    >>Folks,
    >>
    >> Is there a easy way to find out if more than one option is specified.
    >>
    >> Example:
    >>
    >> use Getopt::Std;
    >> getopts(abc);
    >> if ($opt_a || $opt_b || $opt_c) {}
    >>
    >> I only want user to specify one of the options (either -a or -b -or -c).
    >> How do I easily check if they have specified more than one option.
    >>
    >> I can write "if" statement as follows:
    >>
    >> if (($opt_a && $opt_b && $opt_c) ||
    >> ($opt_a && $opt_b) ||
    >> ($opt_b && $opt_c) ||
    >> ($opt_a && $opt_c))
    >> {
    >> # print error message.
    >> }
    >>
    >> This could get ugly if the number of possible options is greater than 3.
    >>
    >> Is there an easier way?
    >>
    >> Thanks.
    >>

    >
    > Sure. Just count heads.
    >
    > my $arg_count=0;
    > $arg_count++ if $opt_a;
    > $arg_count++ if $opt_b;
    > $arg_count++ if $opt_c;
    >
    > if ($arg_count != 1) {
    > die "You dummy, pick one and only one!"; }


    Thanks. That would work for me.

    >
    > --
    > Christopher Mattern
    >
    > "Which one you figure tracked us?"
    > "The ugly one, sir."
    > "...Could you be more specific?"


    --
    Hemant Shah /"\ ASCII ribbon campaign
    E-mail: \ / ---------------------
    X against HTML mail
    TO REPLY, REMOVE NoJunkMail / \ and postings
    FROM MY E-MAIL ADDRESS.
    -----------------[DO NOT SEND UNSOLICITED BULK E-MAIL]------------------
    I haven't lost my mind, Above opinions are mine only.
    it's backed up on tape somewhere. Others can have their own.
     
    Hemant Shah, Sep 24, 2006
    #9
  10. Hemant Shah

    Hemant Shah Guest

    While stranded on information super highway "Mumia W. (reading news)" wrote:
    > On 09/22/2006 12:27 PM, Hemant Shah wrote:
    >> Folks,
    >>
    >> Is there a easy way to find out if more than one option is specified.
    >>
    >> Example:
    >>
    >> use Getopt::Std;
    >> getopts(abc);
    >> if ($opt_a || $opt_b || $opt_c) {}
    >>
    >> I only want user to specify one of the options (either -a or -b -or -c).
    >> How do I easily check if they have specified more than one option.
    >>
    >> I can write "if" statement as follows:
    >>
    >> if (($opt_a && $opt_b && $opt_c) ||
    >> ($opt_a && $opt_b) ||
    >> ($opt_b && $opt_c) ||
    >> ($opt_a && $opt_c))
    >> {
    >> # print error message.
    >> }
    >>
    >> This could get ugly if the number of possible options is greater than 3.
    >>
    >> Is there an easier way?
    >>
    >> Thanks.
    >>

    >
    > Put the results in a hash and count the number of keys in the hash like so:
    >
    > use Getopt::Std;
    > my %opts;
    > getopts ('abc', \%opts);
    >
    > if (keys %opts > 1) {
    > print "More than one option was given.\n";
    > }
    >
    > __HTH__


    Thanks, this would also work for me, but I liked the one where I keep
    the count of the arguments. This woulsd not work in another script I have
    where it has combination of optional and mandatory options.

    Example: [-dvhs] {-A | -B | -C}

    Where -d, -v, -h and -s are optional, but the user must specify one and only
    one of -A, -B and -C.


    >
    > --
    >


    --
    Hemant Shah /"\ ASCII ribbon campaign
    E-mail: \ / ---------------------
    X against HTML mail
    TO REPLY, REMOVE NoJunkMail / \ and postings
    FROM MY E-MAIL ADDRESS.
    -----------------[DO NOT SEND UNSOLICITED BULK E-MAIL]------------------
    I haven't lost my mind, Above opinions are mine only.
    it's backed up on tape somewhere. Others can have their own.
     
    Hemant Shah, Sep 24, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vince C.
    Replies:
    6
    Views:
    733
    James Kanze
    Jul 18, 2007
  2. Merciadri Luca
    Replies:
    4
    Views:
    828
  3. Bob Rashkin
    Replies:
    5
    Views:
    77
    Dennis Lee Bieber
    Dec 23, 2013
  4. Steven D'Aprano
    Replies:
    0
    Views:
    105
    Steven D'Aprano
    Dec 23, 2013
  5. Replies:
    3
    Views:
    92
    Gary Herron
    Dec 23, 2013
Loading...

Share This Page