◐ Shell
clean mode source ↗

Message 186110 - Python tracker

There's another 'feature' to the patch proposed here.  It only deletes the first '--' in the list of strings passed to '_get_values' for a particular action.

    parser = argparse.ArgumentParser()
    parser.add_argument('foo')
    parser.add_argument('bar', nargs='*')
    print(parser.parse_args('-- 1 -- 2 3 4'.split(' ')))
    # Namespace(bar=['2', '3', '4'], foo='1')

'_get_values' first gets ('foo',['--','1']), then ('bar',['--','2','3','4'])

    print(parser.parse_args('-- -- 1 -- 2 -- 3 4'.split(' ')))
    # with this '1st only' change
    # Namespace(bar=['1', '2', '--', '3', '4'], foo='--')
    # without it, deleting all '--'; note foo is empty
    # Namespace(bar=['1', '2', '3', '4'], foo=[])

And to confuse things a bit more:

    print(parser.parse_args('1 -- 2 3 4'.split(' ')))
    # Namespace(bar=['2', '3', '4'], foo='1')

passes ['1','--'] with 'foo'

If 'bar' nargs='...', bar gets all of the '--' (with or without this patch).

The handling of '--' is complex because it is used in one place to mean, 'everything else is an argument', effectively adding '-AA...' to the arg_strings_pattern. It also matches with the nargs_pattern (e.g. '(-*A-*)...').  And then it may or may not be removed in _get_values().