问题
I have a Python script which takes a lot of arguments.
I currently use a configuration.ini
file (read using configparser
), but would like to allow the user to override specific arguments using command line.
If I'd only have had two arguments I'd have used something like:
if not arg1:
arg1 = config[section]['arg1']
But I don't want to do that for 30 arguments.
Any easy way to take optional arguments from cmd line, and default to the config
file?
回答1:
Try the following, using dict.update():
import argparse
import configparser
config = configparser.ConfigParser()
config.read('config.ini')
defaults = config['default']
parser = argparse.ArgumentParser()
parser.add_argument('-a', dest='arg1')
parser.add_argument('-b', dest='arg2')
parser.add_argument('-c', dest='arg3')
args = vars(parser.parse_args())
result = dict(defaults)
result.update({k: v for k, v in args.items() if v is not None}) # Update if v is not None
With this example of ini file:
[default]
arg1=val1
arg2=val2
arg3=val3
and
python myargparser.py -a "test"
result
would contain:
{'arg1': 'test', 'arg2': 'val2', 'arg3': 'val3'}
回答2:
You can use a ChainMap from the collections
module.
From the doc:
A ChainMap groups multiple dicts or other mappings together to create a single, updateable view. [...]
Lookups search the underlying mappings successively until a key is found. [...]
So, you could create
- a
config
dict containing the key-value pairs from your config file, - a
cmd_line_args
dict containing the ones given on the command line
Then, create a ChainMap:
from collections import ChainMap
combined = ChainMap(cmd_line_args, config)
When you access combined['arg1']
, arg1 will first be looked up in the cmd_line_args
dict, and if it isn't found there, config[arg1]
will be returned.
You can chain as many dicts as you wish, which lets you combine as many levels of defaults as you wish.
来源:https://stackoverflow.com/questions/48538581/argparse-defaults-from-file