Skip to content

Instantly share code, notes, and snippets.

@haard
Created December 22, 2015 08:08
Show Gist options
  • Save haard/1cdcf7aabb00b794a767 to your computer and use it in GitHub Desktop.
Save haard/1cdcf7aabb00b794a767 to your computer and use it in GitHub Desktop.
from argparse import ArgumentParser
from configparser import ConfigParser
import pprint
def main():
# Read file config
fparser = ConfigParser()
fparser.read('config.conf')
config = {'some.key': 'some_value'} # Hard-coded default
for section in fparser.sections():
for key, value in fparser[section].items():
if value is not None:
config[section + '.' + key] = value
# Read command-line config
parser = ArgumentParser()
parser.add_argument('--some.key')
parser.add_argument('--some.key2')
parser.add_argument('--other.key')
args = parser.parse_args()
for key, value in args.__dict__.items():
if not value is None:
config[key] = value
return config
if __name__ == '__main__':
pprint.pprint(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment