Python novice here. I am attempting to set up an argument in argparse that takes the form of "-x option=value -x option2=value..."
I'm using the following which passes the arguments.
swlist_parser.add_argument("-x", nargs="+",action='append',metavar='option',
help="-x option=value, set the option to value")
I have a dictionary of default arguments that are available for the command. I want to use -x option=value to modify the dictionary of default arguments with the one on the command line.
swlist -x distribution_target_directory=/tmp -x admin_directory=/var/lib/sw -x another_option=another_value ...
# Code I have
# args.x is now the following:
if args.x != None:
print("-x options passed: ")
cliopts = {}
for opt in (args.x):
print(opt)
-x options passed:
['distribution_target_directory=/tmp']
['admin_directory=/var']
The arguments to -x are now class 'list'. I need a solution to add these to a dictionary.
I tried modifying the argparse to be dictionary. This option doesn't allow for the option=value
swlist_parser.add_argument("-x", nargs="+",action='append',type=dict,metavar='option', help="-x option=value, set the option to value")
swlist.py -x distribution_target_directory=/tmp -x admin_directory=/var -X /home/pweber/pauls.defaults
usage: swlist [options] [software_selections] [@ target_selections]
swlist: error: argument -x: invalid dict value: 'distribution_target_directory=/tmp'
The expected output would allow for this code to add the options to a dicitonary
if args.x != None:
print("-x options passed: ")
cliopts = {}
for opt in (args.x):
# convert option=value to dictionary`your text`
cliopts = cliopts|<opt converted to dict>