Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python argparse dict arg

I want to receive a dict(str -> str) argument from the command line. Does argparse.ArgumentParser provide it? Or any other library?

For the command line:

program.py --dict d --key key1 --value val1 --key key2 --value val2 

I expect the following dictionary:

d = {"key1": "val1", "key2": "val2"} 
like image 604
orion_tvv Avatar asked May 01 '15 11:05

orion_tvv


1 Answers

Here's another solution using a custom action, if you want to specify dict key pairs together comma-separated --

import argparse import sys parser = argparse.ArgumentParser(description='parse key pairs into a dictionary')  class StoreDictKeyPair(argparse.Action):      def __call__(self, parser, namespace, values, option_string=None):          my_dict = {}          for kv in values.split(","):              k,v = kv.split("=")              my_dict[k] = v          setattr(namespace, self.dest, my_dict)  parser.add_argument("--key_pairs", dest="my_dict", action=StoreDictKeyPair, metavar="KEY1=VAL1,KEY2=VAL2...")  args = parser.parse_args(sys.argv[1:]) print args 

Running:

python parse_kv.py --key_pairs 1=2,a=bbb,c=4 --key_pairs test=7,foo=bar 

Output:

Namespace(my_dict={'1': '2', 'a': 'bbb', 'c': '4', 'test': '7', 'foo': 'bar'}) 

If you want to use nargs instead of comma-separated values in string:

class StoreDictKeyPair(argparse.Action):      def __init__(self, option_strings, dest, nargs=None, **kwargs):          self._nargs = nargs          super(StoreDictKeyPair, self).__init__(option_strings, dest, nargs=nargs, **kwargs)      def __call__(self, parser, namespace, values, option_string=None):          my_dict = {}          print "values: {}".format(values)          for kv in values:              k,v = kv.split("=")              my_dict[k] = v          setattr(namespace, self.dest, my_dict)  parser.add_argument("--key_pairs", dest="my_dict", action=StoreDictKeyPair, nargs="+", metavar="KEY=VAL")  args = parser.parse_args(sys.argv[1:]) print args 

Running

python arg_test4.py --key_pairs 1=2 a=bbb c=4 test=7 foo=bar 

Outputs:

values: ['1=2', 'a=bbb', 'c=4', 'test=7', 'foo=bar'] Namespace(my_dict={'1': '2', 'a': 'bbb', 'c': '4', 'test': '7', 'foo': 'bar'}) 
like image 72
storm_m2138 Avatar answered Sep 28 '22 08:09

storm_m2138