Python Forum

Full Version: is there an options parser that can handle + and ++
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i am looking for an options parser that can handle options with - or -- or + or ++. it must support combining single letter options in the same argument (for both - and + including - and + in the same argument), -- or ++ alone to end options (for arguments alone or arguments that look like options), and option to argument tracking (for each argument, which options are closest, before or after it). i want to convert all my old commands written in C. or else i might have build my own options class.
Argparse handles both i believe

https://docs.python.org/3/library/argpar...efix-chars

Im not sure beyond that as i never use + prefix for options.

I would assume something like this -f/--foo or -b/--bar or +f/++foo or +b/++bar can all be used. f and foo are result back to foo key in dictionary.
import argparse

parser = argparse.ArgumentParser(prog='PROG', prefix_chars='-+')
parser.add_argument('+f', '++foo')
parser.add_argument('-f', '--foo')
parser.add_argument('+b', '++bar')
parser.add_argument('-b', '--bar')
args = vars(parser.parse_args())
print(args)
Output:
metulburr@ubuntu:~$ python3.6 test11.py --foo test {'foo': 'test', 'bar': None} metulburr@ubuntu:~$ python3.6 test11.py -f test {'foo': 'test', 'bar': None} metulburr@ubuntu:~$ python3.6 test11.py -b b {'foo': None, 'bar': 'b'} metulburr@ubuntu:~$ python3.6 test11.py --bar test {'foo': None, 'bar': 'test'} metulburr@ubuntu:~$ python3.6 test11.py --bar test ++foo tester {'foo': 'tester', 'bar': 'test'} metulburr@ubuntu:~$ python3.6 test11.py --bar test --foo tester {'foo': 'tester', 'bar': 'test'}