in reply to Re: how to make -h optional in GetOpt::Long
in thread how to make -h optional in GetOpt::Long

Adding the ampersand does nothing if you keep the parameter. I don't believe a sub is appropriate here anyway -- they're meant to help parsing -- and it shouldn't be h=s either.

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; }, 'help' => \$help, # Will also handle -h ) or error_detect("3 Invalid commmand line options."); if ($help) { die("usage: ...\n"); }

We've already answered this question for him.

Replies are listed 'Best First'.
Re^3: how to make -h optional in GetOpt::Long
by bahadur (Sexton) on Jun 02, 2005 at 06:14 UTC
    but keeping every thing aside. how do u make a switch optional in GetOpt::Long? in STD you use a semi colon to denote that the switch is optional what do u do in Long? what if i have more than 2 switches which are optional? how do i express them as optional in Long?
      All switches are optional. (On that subject, the docs say "That's why they're called 'options'.") If you don't want a switch to be optional, you have to explicitely check to make sure a value was supplied, after calling GetOptions.