Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9ae5050

Browse files
committed
Issue #12713: reverted fix pending further discussion.
1 parent d1c2a8e commit 9ae5050

4 files changed

Lines changed: 32 additions & 98 deletions

File tree

Doc/library/argparse.rst

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,18 +1668,6 @@ Sub-commands
16681668
>>> parser.parse_args(['co', 'bar'])
16691669
Namespace(foo='bar')
16701670

1671-
argparse supports non-ambiguous abbreviations of subparser names.
1672-
1673-
For example, the following three calls are all supported
1674-
and do the same thing, as the abbreviations used are not ambiguous::
1675-
1676-
>>> parser.parse_args(['checkout', 'bar'])
1677-
Namespace(foo='bar')
1678-
>>> parser.parse_args(['check', 'bar'])
1679-
Namespace(foo='bar')
1680-
>>> parser.parse_args(['che', 'bar'])
1681-
Namespace(foo='bar')
1682-
16831671
One particularly effective way of handling sub-commands is to combine the use
16841672
of the :meth:`add_subparsers` method with calls to :meth:`set_defaults` so
16851673
that each subparser knows which Python function it should execute. For

Lib/argparse.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,12 +1110,6 @@ def __call__(self, parser, namespace, values, option_string=None):
11101110
parser_name = values[0]
11111111
arg_strings = values[1:]
11121112

1113-
# get full parser_name from (optional) abbreviated one
1114-
for p in self._name_parser_map:
1115-
if p.startswith(parser_name):
1116-
parser_name = p
1117-
break
1118-
11191113
# set the parser name if requested
11201114
if self.dest is not SUPPRESS:
11211115
setattr(namespace, self.dest, parser_name)
@@ -2313,18 +2307,11 @@ def _get_value(self, action, arg_string):
23132307

23142308
def _check_value(self, action, value):
23152309
# converted value must be one of the choices (if specified)
2316-
if action.choices is not None:
2317-
ac = [ax for ax in action.choices if str(ax).startswith(str(value))]
2318-
if len(ac) == 0:
2319-
args = {'value': value,
2320-
'choices': ', '.join(map(repr, action.choices))}
2321-
msg = _('invalid choice: %(value)r (choose from %(choices)s)')
2322-
raise ArgumentError(action, msg % args)
2323-
elif len(ac) > 1:
2324-
args = {'value': value,
2325-
'choices': ', '.join(ac)}
2326-
msg = _('ambiguous choice: %(value)r could match %(choices)s')
2327-
raise ArgumentError(action, msg % args)
2310+
if action.choices is not None and value not in action.choices:
2311+
args = {'value': value,
2312+
'choices': ', '.join(map(repr, action.choices))}
2313+
msg = _('invalid choice: %(value)r (choose from %(choices)s)')
2314+
raise ArgumentError(action, msg % args)
23282315

23292316
# =======================
23302317
# Help-formatting methods

Lib/test/test_argparse.py

Lines changed: 27 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1842,22 +1842,6 @@ def _get_parser(self, subparser_help=False, prefix_chars=None,
18421842
parser3.add_argument('t', type=int, help='t help')
18431843
parser3.add_argument('u', nargs='...', help='u help')
18441844

1845-
# add fourth sub-parser (to test abbreviations)
1846-
parser4_kwargs = dict(description='lost description')
1847-
if subparser_help:
1848-
parser4_kwargs['help'] = 'lost help'
1849-
parser4 = subparsers.add_parser('lost', **parser4_kwargs)
1850-
parser4.add_argument('-w', type=int, help='w help')
1851-
parser4.add_argument('x', choices='abc', help='x help')
1852-
1853-
# add fifth sub-parser, with longer name (to test abbreviations)
1854-
parser5_kwargs = dict(description='long description')
1855-
if subparser_help:
1856-
parser5_kwargs['help'] = 'long help'
1857-
parser5 = subparsers.add_parser('long', **parser5_kwargs)
1858-
parser5.add_argument('-w', type=int, help='w help')
1859-
parser5.add_argument('x', choices='abc', help='x help')
1860-
18611845
# return the main parser
18621846
return parser
18631847

@@ -1873,24 +1857,6 @@ def test_parse_args_failures(self):
18731857
args = args_str.split()
18741858
self.assertArgumentParserError(self.parser.parse_args, args)
18751859

1876-
def test_parse_args_abbreviation(self):
1877-
# check some non-failure cases:
1878-
self.assertEqual(
1879-
self.parser.parse_args('0.5 long b -w 7'.split()),
1880-
NS(foo=False, bar=0.5, w=7, x='b'),
1881-
)
1882-
self.assertEqual(
1883-
self.parser.parse_args('0.5 lon b -w 7'.split()),
1884-
NS(foo=False, bar=0.5, w=7, x='b'),
1885-
)
1886-
self.assertEqual(
1887-
self.parser.parse_args('0.5 los b -w 7'.split()),
1888-
NS(foo=False, bar=0.5, w=7, x='b'),
1889-
)
1890-
# check a failure case: 'lo' is ambiguous
1891-
self.assertArgumentParserError(self.parser.parse_args,
1892-
'0.5 lo b -w 7'.split())
1893-
18941860
def test_parse_args(self):
18951861
# check some non-failure cases:
18961862
self.assertEqual(
@@ -1943,80 +1909,78 @@ def test_dest(self):
19431909

19441910
def test_help(self):
19451911
self.assertEqual(self.parser.format_usage(),
1946-
'usage: PROG [-h] [--foo] bar {1,2,3,lost,long} ...\n')
1912+
'usage: PROG [-h] [--foo] bar {1,2,3} ...\n')
19471913
self.assertEqual(self.parser.format_help(), textwrap.dedent('''\
1948-
usage: PROG [-h] [--foo] bar {1,2,3,lost,long} ...
1914+
usage: PROG [-h] [--foo] bar {1,2,3} ...
19491915
19501916
main description
19511917
19521918
positional arguments:
1953-
bar bar help
1954-
{1,2,3,lost,long} command help
1919+
bar bar help
1920+
{1,2,3} command help
19551921
19561922
optional arguments:
1957-
-h, --help show this help message and exit
1958-
--foo foo help
1923+
-h, --help show this help message and exit
1924+
--foo foo help
19591925
'''))
19601926

19611927
def test_help_extra_prefix_chars(self):
19621928
# Make sure - is still used for help if it is a non-first prefix char
19631929
parser = self._get_parser(prefix_chars='+:-')
19641930
self.assertEqual(parser.format_usage(),
1965-
'usage: PROG [-h] [++foo] bar {1,2,3,lost,long} ...\n')
1931+
'usage: PROG [-h] [++foo] bar {1,2,3} ...\n')
19661932
self.assertEqual(parser.format_help(), textwrap.dedent('''\
1967-
usage: PROG [-h] [++foo] bar {1,2,3,lost,long} ...
1933+
usage: PROG [-h] [++foo] bar {1,2,3} ...
19681934
19691935
main description
19701936
19711937
positional arguments:
1972-
bar bar help
1973-
{1,2,3,lost,long} command help
1938+
bar bar help
1939+
{1,2,3} command help
19741940
19751941
optional arguments:
1976-
-h, --help show this help message and exit
1977-
++foo foo help
1942+
-h, --help show this help message and exit
1943+
++foo foo help
19781944
'''))
19791945

19801946

19811947
def test_help_alternate_prefix_chars(self):
19821948
parser = self._get_parser(prefix_chars='+:/')
19831949
self.assertEqual(parser.format_usage(),
1984-
'usage: PROG [+h] [++foo] bar {1,2,3,lost,long} ...\n')
1950+
'usage: PROG [+h] [++foo] bar {1,2,3} ...\n')
19851951
self.assertEqual(parser.format_help(), textwrap.dedent('''\
1986-
usage: PROG [+h] [++foo] bar {1,2,3,lost,long} ...
1952+
usage: PROG [+h] [++foo] bar {1,2,3} ...
19871953
19881954
main description
19891955
19901956
positional arguments:
1991-
bar bar help
1992-
{1,2,3,lost,long} command help
1957+
bar bar help
1958+
{1,2,3} command help
19931959
19941960
optional arguments:
1995-
+h, ++help show this help message and exit
1996-
++foo foo help
1961+
+h, ++help show this help message and exit
1962+
++foo foo help
19971963
'''))
19981964

19991965
def test_parser_command_help(self):
20001966
self.assertEqual(self.command_help_parser.format_usage(),
2001-
'usage: PROG [-h] [--foo] bar {1,2,3,lost,long} ...\n')
1967+
'usage: PROG [-h] [--foo] bar {1,2,3} ...\n')
20021968
self.assertEqual(self.command_help_parser.format_help(),
20031969
textwrap.dedent('''\
2004-
usage: PROG [-h] [--foo] bar {1,2,3,lost,long} ...
1970+
usage: PROG [-h] [--foo] bar {1,2,3} ...
20051971
20061972
main description
20071973
20081974
positional arguments:
2009-
bar bar help
2010-
{1,2,3,lost,long} command help
2011-
1 1 help
2012-
2 2 help
2013-
3 3 help
2014-
lost lost help
2015-
long long help
1975+
bar bar help
1976+
{1,2,3} command help
1977+
1 1 help
1978+
2 2 help
1979+
3 3 help
20161980
20171981
optional arguments:
2018-
-h, --help show this help message and exit
2019-
--foo foo help
1982+
-h, --help show this help message and exit
1983+
--foo foo help
20201984
'''))
20211985

20221986
def test_subparser_title_help(self):
@@ -2119,8 +2083,6 @@ def test_alias_help(self):
21192083
1 help
21202084
2 2 help
21212085
3 3 help
2122-
lost lost help
2123-
long long help
21242086
"""))
21252087

21262088
# ============

Misc/NEWS

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ Library
6060
- Issue #9998: On Linux, ctypes.util.find_library now looks in LD_LIBRARY_PATH
6161
for shared libraries.
6262

63-
- Issue #12713: Allowed abbreviation of subcommands by end-users for users of
64-
argparse.
65-
6663
Tests
6764
-----
6865

0 commit comments

Comments
 (0)