How identify if more than one option is specified?

H

Hemant Shah

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: (e-mail address removed) \ / ---------------------
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.
 
P

Paul Lalli

Hemant said:
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
 
D

David Squire

Hemant said:
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
 
J

John Bokma

Hemant Shah said:
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";
 
B

Ben Morrow

Quoth (e-mail address removed):
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
 
J

John W. Krahn

Chris said:
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
 
D

Dr.Ruud

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

Hemant Shah

While said:
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: (e-mail address removed) \ / ---------------------
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.
 
H

Hemant Shah

While said:
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: (e-mail address removed) \ / ---------------------
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top