# HG changeset patch # User Jeff Hammel # Date 1415142668 28800 # Node ID be9f792abaadc9cbc506e711ce5629532d0e3998 # Parent 2ebf5cef55de4af8620ee4eb46cede7ff6f9b8fa mv diff -r 2ebf5cef55de -r be9f792abaad python/cli.py --- a/python/cli.py Tue Nov 04 15:09:33 2014 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,59 +0,0 @@ -#!/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 diff -r 2ebf5cef55de -r be9f792abaad python/example/cli.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/python/example/cli.py Tue Nov 04 15:11:08 2014 -0800 @@ -0,0 +1,59 @@ +#!/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