Perl Monk, Perl Meditation | |
PerlMonks |
Trickery with Getopt::Long (flag option, with optional integer arg)by stevieb (Canon) |
on Aug 18, 2017 at 15:03 UTC ( [id://1197613]=perlquestion: print w/replies, xml ) | Need Help?? |
stevieb has asked for the wisdom of the Perl Monks concerning the following question: I'm in a position with Getopt::Long where I need to allow an option to operate as a flag, but also accept an argument to itself (an integer), and it also must set 0 as the flag value if no integer is passed along with it. The only way so far I've found to do it involves a bit of workaround, and I'm just wondering if I'm over thinking this. I haven't found another way to do this, so perhaps I'm overlooking something...
So what that does is sets the argument variable to -1 as a default. If --arg|-a is sent in without an integer value along side it, I catch the warning and if it references the option properly, I set it to 0. If an integer is supplied to the option, things just work as normal. I'm happy with the code as it works very well, so I'm just looking to find out if there are built-in or other alternatives for this type of situation. Thanks, -stevieb
Back to
Seekers of Perl Wisdom
|
|