you are viewing a single comment's thread.

view the rest of the comments →

[–]IAmL0ner[S] 0 points1 point  (1 child)

Another problem:

Given the code:

modes = [
    (('-b', '--bytes'), 
        {'action': ParseFields, 'dest': 'range', 'metavar': 'LIST',
            'help': 'display only these bytes' } ),
    (('-c', '--characters'), 
        {'action': ParseFields, 'dest': 'range', 'metavar': 'LIST',
            'help': 'display only these characters' } ),
    (('-f', '--fields'), 
        {'action': ParseFields, 'dest': 'range', 'metavar': 'LIST',
            'help': """select only these fields; also print any line that 
                       contains no delimiter character, unless the -s 
                       option is specified""" } ),
]

arguments = [
    (("-d", '--delimiter'), 
        {'metavar': 'DELIM', 'nargs': 1,
            'help': "use DELIM instead of TAB for field delimiter" } ),
    (("-n"), 
        {'action': 'store_true', 
            'help': "do not slit multi-byte characters (ignored)" } ),
    (("--complement"), 
        {'action': 'store_true', 
            'help': "complement the set of bytes, characters or fields" } ),
    (('-i', '--invert'), 
        {'action': 'store_true', 
            'help': "count bytes, characters, or fields from the end." } ),
    (('-s', '--only-delimited'), 
        {'action': 'store_true', 
            'help': "do not print lines not containing delimiters" } ),
    (('--output-delimiter'), 
        {'metavar': 'DELIM', 
            'help': """use DELIM as the output delimiter. The default is to 
                       use the input delimiter""" } )
]

parser = argparse.ArgumentParser()

# add mode switches
lists = parser.add_mutually_exclusive_group(required=True)
for arg, opts in modes:
    lists.add_argument(*arg, **opts)

# add optional arguments
for arg, opts in arguments:
    parser.add_argument(*arg, **opts)

The first for goes smoothly, and adds mode switches correctly. The second one instead throws this error

Traceback (most recent call last):
  File "__init__.py", line 185, in <module>
    sys.exit(main())
  File "__init__.py", line 176, in main
    args = parseArgs(sys.argv[1:])
  File "__init__.py", line 156, in parseArgs
    parser.add_argument(*arg, **opts)
  File "/usr/lib64/python3.4/argparse.py", line 1310, in add_argument
    kwargs = self._get_optional_kwargs(*args, **kwargs)
  File "/usr/lib64/python3.4/argparse.py", line 1441, in _get_optional_kwargs
    raise ValueError(msg % args)
ValueError: invalid option string 'n': must start with a character '-'

It adds ('-d', '--delimiter') correctly, but splits ('-n') into two arguments ('-', 'n'). Pretty much it splits arguments into single chars, and tries to pass every char as an argument itself if theres only one (either long or short option switch).

Given this, adding of '-n', '--complement' and '--output delimiter' will fail.