I wanted to use Getopt::Long::GetOptions for getting command line options to the script.
I have a requirement like this:
perl script.pl -c <name1> -c <name2> -m <name3> argument
Here we have option flags -c and -mm which are optional, and argument is mandatory.
Can anyone point out the correct usage for GetOptions?
From the Getopt::Long documentation:
GetOptionsdoes not return a false result when an option is not suppliedThat's why they're called 'options'.
In other words, if you are expecting a mandatory parameter, you need to explicitly check for it outside of the GetOptions call.
If argument is meant to be part of @ARGV and not the options, use -- to signal the end of options. In the example below, the script would access argument via $ARGV[0]:
perl script.pl -c <name1> -c <name2> -m <name3> -- argument
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With