mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
gh-138525: Support single-dash long options and prefix_chars in BooleanOptionalAction (GH-138692)
-nofoo is generated for -foo. ++no-foo is generated for ++foo. /nofoo is generated for /foo.
This commit is contained in:
parent
cde19e565c
commit
425fd85ca3
5 changed files with 102 additions and 4 deletions
|
|
@ -932,15 +932,26 @@ def __init__(self,
|
|||
deprecated=False):
|
||||
|
||||
_option_strings = []
|
||||
neg_option_strings = []
|
||||
for option_string in option_strings:
|
||||
_option_strings.append(option_string)
|
||||
|
||||
if option_string.startswith('--'):
|
||||
if option_string.startswith('--no-'):
|
||||
if len(option_string) > 2 and option_string[0] == option_string[1]:
|
||||
# two-dash long option: '--foo' -> '--no-foo'
|
||||
if option_string.startswith('no-', 2):
|
||||
raise ValueError(f'invalid option name {option_string!r} '
|
||||
f'for BooleanOptionalAction')
|
||||
option_string = '--no-' + option_string[2:]
|
||||
option_string = option_string[:2] + 'no-' + option_string[2:]
|
||||
_option_strings.append(option_string)
|
||||
neg_option_strings.append(option_string)
|
||||
elif len(option_string) > 2 and option_string[0] != option_string[1]:
|
||||
# single-dash long option: '-foo' -> '-nofoo'
|
||||
if option_string.startswith('no', 1):
|
||||
raise ValueError(f'invalid option name {option_string!r} '
|
||||
f'for BooleanOptionalAction')
|
||||
option_string = option_string[:1] + 'no' + option_string[1:]
|
||||
_option_strings.append(option_string)
|
||||
neg_option_strings.append(option_string)
|
||||
|
||||
super().__init__(
|
||||
option_strings=_option_strings,
|
||||
|
|
@ -950,11 +961,12 @@ def __init__(self,
|
|||
required=required,
|
||||
help=help,
|
||||
deprecated=deprecated)
|
||||
self.neg_option_strings = neg_option_strings
|
||||
|
||||
|
||||
def __call__(self, parser, namespace, values, option_string=None):
|
||||
if option_string in self.option_strings:
|
||||
setattr(namespace, self.dest, not option_string.startswith('--no-'))
|
||||
setattr(namespace, self.dest, option_string not in self.neg_option_strings)
|
||||
|
||||
def format_usage(self):
|
||||
return ' | '.join(self.option_strings)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue