-
-
Save von/949330 to your computer and use it in GitHub Desktop.
Demonstrate parse_known_args()
This file contains 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
#!/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