Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/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(defaults)
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment