MCPcopy Create free account
hub / github.com/dblalock/bolt / parse_cmd_line

Function parse_cmd_line

experiments/python/pyience.py:288–421  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

286
287
288def 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

Callers

nothing calls this directly

Calls 7

_is_kv_argFunction · 0.85
_is_flag_argFunction · 0.85
UsageErrorClass · 0.85
_split_kv_argFunction · 0.85
_clean_flag_argFunction · 0.85
_to_appropriate_typeFunction · 0.85
formatMethod · 0.80

Tested by

no test coverage detected