Skip to content

Commit d80974d

Browse files
Make double-dash options taking priority oved single-dash long options.
1 parent 1327305 commit d80974d

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

Doc/library/argparse.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,8 +1322,12 @@ attribute is determined by the ``dest`` keyword argument of
13221322

13231323
For optional argument actions, the value of ``dest`` is normally inferred from
13241324
the option strings. :class:`ArgumentParser` generates the value of ``dest`` by
1325-
taking the first long option string and stripping away the initial ``-``
1326-
characters. If no long option strings were supplied, ``dest`` will be derived from
1325+
taking the first double-dash long option string and stripping away the initial
1326+
``-`` characters.
1327+
If no double-dash long option strings were supplied, ``dest`` will be derived
1328+
from the first single-dash long option string by stripping the initial ``-``
1329+
character.
1330+
If no long option strings were supplied, ``dest`` will be derived from
13271331
the first short option string by stripping the initial ``-`` character. Any
13281332
internal ``-`` characters will be converted to ``_`` characters to make sure
13291333
the string is a valid attribute name. The examples below illustrate this

Lib/argparse.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,7 +1660,6 @@ def _get_positional_kwargs(self, dest, **kwargs):
16601660
def _get_optional_kwargs(self, *args, **kwargs):
16611661
# determine short and long option strings
16621662
option_strings = []
1663-
long_option_strings = []
16641663
for option_string in args:
16651664
# error on strings that don't start with an appropriate prefix
16661665
if not option_string[0] in self.prefix_chars:
@@ -1672,16 +1671,22 @@ def _get_optional_kwargs(self, *args, **kwargs):
16721671
# infer destination, '--foo-bar' -> 'foo_bar' and '-x' -> 'x'
16731672
dest = kwargs.pop('dest', None)
16741673
if dest is None:
1675-
short_option_dest = None
1674+
priority = 0
16761675
for option_string in option_strings:
1677-
if len(option_string) > 2:
1678-
# long option: '--foo' or '-foo' -> 'foo'
1676+
if len(option_string) <= 2:
1677+
# short option: '-x' -> 'x'
1678+
if priority < 1:
1679+
dest = option_string.lstrip(self.prefix_chars)
1680+
priority = 1
1681+
elif option_string[1] not in self.prefix_chars:
1682+
# single-dash long option: '-foo' -> 'foo'
1683+
if priority < 2:
1684+
dest = option_string.lstrip(self.prefix_chars)
1685+
priority = 2
1686+
else:
1687+
# two-dash long option: '--foo' -> 'foo'
16791688
dest = option_string.lstrip(self.prefix_chars)
16801689
break
1681-
# short option: '-x' -> 'x'
1682-
if not short_option_dest:
1683-
short_option_dest = option_string.lstrip(self.prefix_chars)
1684-
dest = dest or short_option_dest
16851690
if not dest:
16861691
msg = f'dest= is required for options like {repr(option_strings)[1:-1]}'
16871692
raise TypeError(msg)

Lib/test/test_argparse.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,9 +582,9 @@ class TestOptionalsDest(ParserTestCase):
582582
"""Tests various means of setting destination"""
583583

584584
argument_signatures = [
585-
Sig('-x', '--foo-bar'),
585+
Sig('-x', '-foobar', '--foo-bar', '-barfoo', '-X'),
586586
Sig('--baz', dest='zabbaz'),
587-
Sig('-y', '-qux'),
587+
Sig('-y', '-qux', '-Y'),
588588
Sig('-z'),
589589
]
590590
failures = ['a']

0 commit comments

Comments
 (0)