'Is there a perl module equivalent to python's optparse.OptionParser? [closed]
I have a new script that I need to write in perl, to leverage our perl codebase (no, I can't convert it, not yet anyway). Our coding standards dictate Getopt::Long::GetOptions()
but I realize my option handling would be greatly improved and simplified if I could just drop python's optparse.OptionParser into it. The things I like in python: validation of inputs, both formats and required/optional, and auto help string generation based on argument config.
Any tips? I know I could roll my own, but I'd rather use someone else's work if it's already been done.
Solution 1:[1]
The canonical tool is Getopt::Long. It's not "declarative" as such. Is there some specific behavior you are trying to replicate?
EDIT: For more power, you might be interested in App::Cmd or parts of it (see also App::Cmd::Tutorial).
Internally this uses Getopt::Long::Descriptive which seems to meet most of your requirements. This might be especially useful for you because internally it uses Getopt::Long
and Params::Validate, so it might be very easy to move your current code base but now have validation.
Solution 2:[2]
There are lots of options in the GetOpt namespace on CPAN.
GetOpt::Std and GetOpt::Long are both core modules.
Solution 3:[3]
Now there is: Getopt::optparse
Solution 4:[4]
MooseX::Getopt and MooX::Options are, well... options. Both work similarly, and both use Getopt::Long::Descriptive under the hood, integrating it with a class to provide advanced validation and defaulting as well as help text. Then you just use the new_with_options
constructor and you get an instance of the class with all of its slots filled in from the commandline.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | brian d foy |
Solution 2 | Quentin |
Solution 3 | mhearse |
Solution 4 | hobbs |