Mercurial > hg > config
view python/cli.py @ 694:ebca6d85213a
File "/usr/lib/python3/dist-packages/IPython/config/__init__.py", line 16, in <module>
from .application import *
File "/usr/lib/python3/dist-packages/IPython/config/application.py", line 31, in <module>
from IPython.config.configurable import SingletonConfigurable
File "/usr/lib/python3/dist-packages/IPython/config/configurable.py", line 33, in <module>
from IPython.utils.text import indent, wrap_paragraphs
File "/usr/lib/python3/dist-packages/IPython/utils/text.py", line 28, in <module>
from IPython.external.path import path
File "/usr/lib/python3/dist-packages/IPython/external/path/__init__.py", line 2, in <module>
from path import *
File "/home/jhammel/python/path.py", line 25
print root(path)
^
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Wed, 09 Jul 2014 16:26:49 -0700 |
parents | e7948549afa1 |
children |
line wrap: on
line source
#!/usr/bin/env python """ program illustrating command line in the form of ``--option foo`` or ``--option=foo`` goes to a (key,value) paid and ``-tag`` gets appended to a list. Further options go to a further list:: >>> main(['-foo', '--bar=fleem', 'baz']) (['foo'], {'bar': 'fleem'}, ['baz']) """ import sys class ParserError(Exception): """error for exceptions while parsing the command line""" def main(_args=sys.argv[1:]): # return values _dict = {} tags = [] args = [] # parse the arguments key = None for arg in _args: if arg.startswith('---'): raise ParserError("arguments should start with '-' or '--' only") elif arg.startswith('--'): if key: raise ParserError("Key %s still open" % key) key = arg[2:] if '=' in key: key, value = key.split('=', 1) _dict[key] = value key = None continue elif arg.startswith('-'): if key: raise ParserError("Key %s still open" % key) tags.append(arg[1:]) continue else: if key: _dict[key] = arg continue args.append(arg) # return values return (_dict, tags, args) if __name__ == '__main__': try: _dict, tags, args = main() except ParserError, e: import pdb; pdb.set_trace() # for debugging print _dict print tags print args