1
votes

I wrote a script that needs to get few arguments from the user, and I encountered a problem while trying to read my script arguments.

The script can get the -type for running the functions on one file type or can get the flag -all in order to run on all file types.

my $opt = GetOptions (
    'help|h' => \$help,
    'type=s' => \$type,
    'all'    => \$all,
);

I am trying to think about all the wrong options that a user can run and found that when the user is running myscript.pl -type -all, the module reads the -all as the -type string.

Is there any elegant way to avoid such a thing?

2

2 Answers

4
votes

Allow either -type or -all, but not both. Alternatively, remove the -all option and if -type is followed by all, treat it as you're trying to treat -all now.

my $opt = GetOptions (
    'help|h' => \$help,
    'type=s' => \$type,
    'all'    => sub { $type = "all"; },
);
2
votes

Use 'type:s' instead of 'type=s' and the value for -type will be optional. Elegant? I don't know, but it's perlish.