Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perl Getopt::Long::GetOptions usage

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?

like image 719
Arpit Avatar asked Feb 01 '26 20:02

Arpit


1 Answers

From the Getopt::Long documentation:

GetOptions does not return a false result when an option is not supplied

That'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
like image 106
Zaid Avatar answered Feb 03 '26 10:02

Zaid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!