mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
gh-138697: Fix inferring dest from a single-dash long option in argparse (#138699)
* gh-138697: Fix inferring dest from a single-dash long option in argparse If a short option and a single-dash long option are passed to add_argument(), dest is now inferred from the single-dash long option. * Make double-dash options taking priority over single-dash long options. --------- Co-authored-by: Savannah Ostrowski <savannah@python.org>
This commit is contained in:
parent
b3383085f9
commit
77cb39e0c7
5 changed files with 58 additions and 22 deletions
|
|
@ -581,13 +581,22 @@ class TestOptionalsShortLong(ParserTestCase):
|
|||
class TestOptionalsDest(ParserTestCase):
|
||||
"""Tests various means of setting destination"""
|
||||
|
||||
argument_signatures = [Sig('--foo-bar'), Sig('--baz', dest='zabbaz')]
|
||||
argument_signatures = [
|
||||
Sig('-x', '-foobar', '--foo-bar', '-barfoo', '-X'),
|
||||
Sig('--baz', dest='zabbaz'),
|
||||
Sig('-y', '-qux', '-Y'),
|
||||
Sig('-z'),
|
||||
]
|
||||
failures = ['a']
|
||||
successes = [
|
||||
('--foo-bar f', NS(foo_bar='f', zabbaz=None)),
|
||||
('--baz g', NS(foo_bar=None, zabbaz='g')),
|
||||
('--foo-bar h --baz i', NS(foo_bar='h', zabbaz='i')),
|
||||
('--baz j --foo-bar k', NS(foo_bar='k', zabbaz='j')),
|
||||
('--foo-bar f', NS(foo_bar='f', zabbaz=None, qux=None, z=None)),
|
||||
('-x f', NS(foo_bar='f', zabbaz=None, qux=None, z=None)),
|
||||
('--baz g', NS(foo_bar=None, zabbaz='g', qux=None, z=None)),
|
||||
('--foo-bar h --baz i', NS(foo_bar='h', zabbaz='i', qux=None, z=None)),
|
||||
('--baz j --foo-bar k', NS(foo_bar='k', zabbaz='j', qux=None, z=None)),
|
||||
('-qux l', NS(foo_bar=None, zabbaz=None, qux='l', z=None)),
|
||||
('-y l', NS(foo_bar=None, zabbaz=None, qux='l', z=None)),
|
||||
('-z m', NS(foo_bar=None, zabbaz=None, qux=None, z='m')),
|
||||
]
|
||||
|
||||
|
||||
|
|
@ -5611,6 +5620,8 @@ def test_invalid_option_strings(self):
|
|||
self.assertTypeError('-', errmsg='dest= is required')
|
||||
self.assertTypeError('--', errmsg='dest= is required')
|
||||
self.assertTypeError('---', errmsg='dest= is required')
|
||||
self.assertTypeError('-', '--', '---',
|
||||
errmsg="dest= is required for options like '-', '--', '---'")
|
||||
|
||||
def test_invalid_prefix(self):
|
||||
self.assertValueError('--foo', '+foo',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue