Skip to content

Instantly share code, notes, and snippets.

@joech4n
Forked from von/p.py
Last active August 29, 2015 14:12
Show Gist options
  • Save joech4n/c7a8c2a8c9ba0d5a6891 to your computer and use it in GitHub Desktop.
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>
#!/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
[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