-
-
Save joech4n/c7a8c2a8c9ba0d5a6891 to your computer and use it in GitHub Desktop.
From http://blog.vwelch.com/2011/04/combining-configparser-and-argparse.html | Default config file is ~/.<SCRIPT FILE NAME>
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", default=os.path.expanduser('~') + '/.' + os.path.basename(__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 | |
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
[Defaults] | |
option1 = Hello World! | |
option2 = Have a nice day. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment