Python Forum
is there an options parser that can handle + and ++
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
is there an options parser that can handle + and ++
#1
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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'}
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  command line options Skaperen 5 2,597 Aug-14-2020, 08:48 AM
Last Post: DeaD_EyE
  parser for LVM head Skaperen 0 1,420 Nov-02-2019, 04:04 AM
Last Post: Skaperen
  variable args + named options Skaperen 0 1,837 Aug-15-2018, 11:14 PM
Last Post: Skaperen
  cli arguments parser Skaperen 0 2,601 Mar-21-2018, 01:41 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020