Created
July 8, 2014 03:19
-
-
Save sapamja/76e3fd7cc025925fb189 to your computer and use it in GitHub Desktop.
option_parser with Required
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import optparse | |
| # http://code.activestate.com/recipes/573441-extended-optparse-to-allow-definition-of-required-/ | |
| strREQUIRED = 'required' | |
| class OptionWithDefault(optparse.Option): | |
| ATTRS = optparse.Option.ATTRS + [strREQUIRED] | |
| def __init__(self, *opts, **attrs): | |
| if attrs.get(strREQUIRED, False): | |
| attrs['help'] = '(Required) ' + attrs.get('help', "") | |
| optparse.Option.__init__(self, *opts, **attrs) | |
| class OptionParser(optparse.OptionParser): | |
| def __init__(self, **kwargs): | |
| kwargs['option_class'] = OptionWithDefault | |
| optparse.OptionParser.__init__(self, **kwargs) | |
| def check_values(self, values, args): | |
| for option in self.option_list: | |
| if hasattr(option, strREQUIRED) and option.required: | |
| if not getattr(values, option.dest): | |
| self.error("option %s is required" % (str(option))) | |
| return optparse.OptionParser.check_values(self, values, args) | |
| # demonstration of usage: | |
| import sys | |
| if __name__ == "__main__": | |
| parser = OptionParser(usage="Demonstration of OptionParser with 'required' option") | |
| parser.add_option("-i", "--input", required=True, | |
| help="Input file") | |
| dctOptions, lstArgs = parser.parse_args(sys.argv) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment