Skip to content

Instantly share code, notes, and snippets.

@von
Created April 30, 2011 01:48
Show Gist options
  • Save von/949330 to your computer and use it in GitHub Desktop.
Save von/949330 to your computer and use it in GitHub Desktop.
Demonstrate parse_known_args()
#!/usr/bin/env python
import argparse
import ConfigParser
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--conf_file",
help="Specify config file", metavar="FILE")
args, remaining_argv = 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"))
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