bahadur has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, this is my code
Getopt::Long::GetOptions ( 'u=s' => \$user, 'p=s' => \$passwd, 's=s' => sub { local *_ = \$_[1]; /^([^:]+):(\d+)$/ or die("Invalid format for option s.\n"); $host = $1; $port = $2;}, ) or error_detect("3 Invalid commmand line options.");
now this code handles the switches u, p and s. but suppose i am supposed to give a command line swtich like h which does nothing but to invoke a help message. how do that with it.
what i mean is that all switches require some sort of argument. but if the user types in the switch -h than no argument is required with it and the program should directly print a help message.
so how does GetOpt::Long handle switches which do not require any arguments with them and their mere presence means a specific action is to be done. other examples of such switches can be -all etc etc.

Replies are listed 'Best First'.
Re: handling switches with no argument using GetOpt::Long
by Joost (Canon) on May 19, 2005 at 11:28 UTC
Re: handling switches with no argument using GetOpt::Long
by holli (Abbot) on May 19, 2005 at 11:27 UTC
    did you read the docs?
    Handling simple options is straightforward:
    my $verbose = ''; # option variable with default value (false) my $all = ''; # option variable with default value (false) GetOptions ('verbose' => \$verbose, 'all' => \$all);
    The call to GetOptions() parses the command line arguments that are present in @ARGV and sets the option variable to the value 1 if the option did occur on the command line. Otherwise, the option variable is not touched. Setting the option value to true is often called enabling the option.


    holli, /regexed monk/