AnnaSchumann AnnaSchumann - 1 year ago 123
Perl Question

Force use of flags Getopt::Long

Is there a way to force the use of -flags when reading in command-line arguments using Getopt::Long? For example in my current situation:

GetOptions('r=s' => \$var1,
'lf=f' => \$var2,
'uf=f' => \$var3,
'trd=i' => \$var4,
'vd=f' => \$var5)
or die("$usage");

The script does not exit or display $usage if the arguments are still provided but without the flags (such as -lf). Instead it runs regardless until it inevitably errors later on as the arguments were not read into their respective variables (and may well be in the wrong order).

Answer Source

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

A: That's why they're called 'options'.

Source: Getopt::Long documentation

You could add conditionals to check value of flags and if they are not defined then call die or call usage.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download