@@ -44,22 +44,36 @@ def nullable_str(val: str):
44
44
45
45
46
46
def nullable_kvs (val : str ) -> Optional [Mapping [str , int ]]:
47
+ """Parses a string containing comma separate key [str] to value [int]
48
+ pairs into a dictionary.
49
+
50
+ Args:
51
+ val: String value to be parsed.
52
+
53
+ Returns:
54
+ Dictionary with parsed values.
55
+ """
47
56
if len (val ) == 0 :
48
57
return None
49
58
50
59
out_dict : Dict [str , int ] = {}
51
60
for item in val .split ("," ):
52
- try :
53
- key , value = item . split ( "=" )
54
- except TypeError as exc :
55
- msg = "Each item should be in the form KEY=VALUE"
56
- raise ValueError ( msg ) from exc
61
+ kv_parts = [ part . lower (). strip () for part in item . split ( "=" )]
62
+ if len ( kv_parts ) != 2 :
63
+ raise argparse . ArgumentTypeError (
64
+ "Each item should be in the form KEY=VALUE" )
65
+ key , value = kv_parts
57
66
58
67
try :
59
- out_dict [ key ] = int (value )
68
+ parsed_value = int (value )
60
69
except ValueError as exc :
61
70
msg = f"Failed to parse value of item { key } ={ value } "
62
- raise ValueError (msg ) from exc
71
+ raise argparse .ArgumentTypeError (msg ) from exc
72
+
73
+ if key in out_dict and out_dict [key ] != parsed_value :
74
+ raise argparse .ArgumentTypeError (
75
+ f"Conflicting values specified for key: { key } " )
76
+ out_dict [key ] = parsed_value
63
77
64
78
return out_dict
65
79
0 commit comments