Parses the list of command line arguments into a dictionary of key-value pairs Parameters ---------- argv : iterable of strings This should be sys.argv if supplied. Otherwise, sys.argv is read. positional_keys : iterable of strings, optional If k strings are spe
(argv=None, positional_keys=None, allow_flags=True,
infer_types=True)
| 286 | |
| 287 | |
| 288 | def parse_cmd_line(argv=None, positional_keys=None, allow_flags=True, |
| 289 | infer_types=True): |
| 290 | """Parses the list of command line arguments into a dictionary of |
| 291 | key-value pairs |
| 292 | |
| 293 | Parameters |
| 294 | ---------- |
| 295 | argv : iterable of strings |
| 296 | This should be sys.argv if supplied. Otherwise, sys.argv is read. |
| 297 | |
| 298 | positional_keys : iterable of strings, optional |
| 299 | If k strings are specified, the up to the first k arguments will |
| 300 | be treated as values to be paired with these keys. Arguments of the |
| 301 | form foo=bar will never be treated this way. |
| 302 | |
| 303 | allow_flags : bool, optional |
| 304 | If True, allows arguments of the form --myArg. When passed, this will |
| 305 | add {'myArg': True} to the returned dictionary. This is equivalent to |
| 306 | myArg=True |
| 307 | |
| 308 | infer_types : bool, optional |
| 309 | If True, attempts to infer the type of each value in the returned |
| 310 | dictionary. E.g., instead of returning {'height': '72'}, it will |
| 311 | return {'height': 72}. |
| 312 | |
| 313 | Returns |
| 314 | ------- |
| 315 | argKV : dict: string -> inferred type or string |
| 316 | A dictionary whose keys and values are specified by the command line |
| 317 | arguments |
| 318 | |
| 319 | >>> # ------------------------ positional args only |
| 320 | >>> argv = ['pyience.py', 'fooVal', 'barVal'] |
| 321 | >>> d = parse_cmd_line(argv, positional_keys=['fooKey', 'barKey']) |
| 322 | >>> len(d) |
| 323 | 2 |
| 324 | >>> d['fooKey'] |
| 325 | 'fooVal' |
| 326 | >>> d['barKey'] |
| 327 | 'barVal' |
| 328 | >>> # ------------------------ key-value args |
| 329 | >>> argv = ['pyience.py', 'fooVal', 'bletchKey=bletchVal', 'blahKey=blahVal'] |
| 330 | >>> d = parse_cmd_line(argv, positional_keys=['fooKey', 'barKey']) |
| 331 | >>> len(d) |
| 332 | 3 |
| 333 | >>> d['fooKey'] |
| 334 | 'fooVal' |
| 335 | >>> d.get('barKey', 'notHere') |
| 336 | 'notHere' |
| 337 | >>> d['bletchKey'] |
| 338 | 'bletchVal' |
| 339 | >>> d['blahKey'] |
| 340 | 'blahVal' |
| 341 | >>> # ------------------------ flags |
| 342 | >>> argv = ['pyience.py', 'fooVal', 'bletchKey=bletchVal', '--myFlag'] |
| 343 | >>> d = parse_cmd_line(argv, positional_keys=['fooKey', 'barKey']) |
| 344 | >>> d['myFlag'] |
| 345 | True |
nothing calls this directly
no test coverage detected