Created
April 30, 2011 01:57
-
-
Save von/949337 to your computer and use it in GitHub Desktop.
Revisions
-
von created this gist
Apr 30, 2011 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,35 @@ #!/usr/bin/env python import argparse import ConfigParser conf_parser = argparse.ArgumentParser( # Turn off help, so we print all options in response to -h add_help=False ) conf_parser.add_argument("-c", "--conf_file", help="Specify config file", metavar="FILE") args, remaining_argv = conf_parser.parse_known_args() defaults = { "option1" : "some default", "option2" : "some other default", } if args.conf_file: config = ConfigParser.SafeConfigParser() config.read([args.conf_file]) defaults = dict(config.items("Defaults")) # Don't surpress add_help here so it will handle -h parser = argparse.ArgumentParser( # Inherit options from config_parser parents=[conf_parser], # print script description with -h/--help description=__doc__, # Don't mess with format of description formatter_class=argparse.RawDescriptionHelpFormatter, ) parser.set_defaults(**defaults) parser.add_argument("--option1", help="some option") parser.add_argument("--option2", help="some other option") args = parser.parse_args(remaining_argv) print args