Skip to content

Commit c022350

Browse files
authored
Merge pull request RustPython#3837 from fanninpm/argparse-3.10
Update argparse to CPython 3.10
2 parents cc90bc0 + e80412a commit c022350

File tree

2 files changed

+283
-110
lines changed

2 files changed

+283
-110
lines changed

Lib/argparse.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,9 @@ def _format_actions_usage(self, actions, groups):
392392
group_actions = set()
393393
inserts = {}
394394
for group in groups:
395+
if not group._group_actions:
396+
raise ValueError(f'empty group {group}')
397+
395398
try:
396399
start = actions.index(group._group_actions[0])
397400
except ValueError:
@@ -526,12 +529,13 @@ def _format_action(self, action):
526529
parts = [action_header]
527530

528531
# if there was help for the action, add lines of help text
529-
if action.help:
532+
if action.help and action.help.strip():
530533
help_text = self._expand_help(action)
531-
help_lines = self._split_lines(help_text, help_width)
532-
parts.append('%*s%s\n' % (indent_first, '', help_lines[0]))
533-
for line in help_lines[1:]:
534-
parts.append('%*s%s\n' % (help_position, '', line))
534+
if help_text:
535+
help_lines = self._split_lines(help_text, help_width)
536+
parts.append('%*s%s\n' % (indent_first, '', help_lines[0]))
537+
for line in help_lines[1:]:
538+
parts.append('%*s%s\n' % (help_position, '', line))
535539

536540
# or add a newline if the description doesn't end with one
537541
elif not action_header.endswith('\n'):
@@ -722,11 +726,13 @@ def _get_action_name(argument):
722726
if argument is None:
723727
return None
724728
elif argument.option_strings:
725-
return '/'.join(argument.option_strings)
729+
return '/'.join(argument.option_strings)
726730
elif argument.metavar not in (None, SUPPRESS):
727731
return argument.metavar
728732
elif argument.dest not in (None, SUPPRESS):
729733
return argument.dest
734+
elif argument.choices:
735+
return '{' + ','.join(argument.choices) + '}'
730736
else:
731737
return None
732738

@@ -842,6 +848,7 @@ def _get_kwargs(self):
842848
'default',
843849
'type',
844850
'choices',
851+
'required',
845852
'help',
846853
'metavar',
847854
]
@@ -872,8 +879,8 @@ def __init__(self,
872879
option_string = '--no-' + option_string[2:]
873880
_option_strings.append(option_string)
874881

875-
if help is not None and default is not None:
876-
help += f" (default: {default})"
882+
if help is not None and default is not None and default is not SUPPRESS:
883+
help += " (default: %(default)s)"
877884

878885
super().__init__(
879886
option_strings=_option_strings,
@@ -1250,9 +1257,9 @@ def __call__(self, string):
12501257
# the special argument "-" means sys.std{in,out}
12511258
if string == '-':
12521259
if 'r' in self._mode:
1253-
return _sys.stdin
1254-
elif 'w' in self._mode:
1255-
return _sys.stdout
1260+
return _sys.stdin.buffer if 'b' in self._mode else _sys.stdin
1261+
elif any(c in self._mode for c in 'wax'):
1262+
return _sys.stdout.buffer if 'b' in self._mode else _sys.stdout
12561263
else:
12571264
msg = _('argument "-" with mode %r') % self._mode
12581265
raise ValueError(msg)
@@ -1666,7 +1673,8 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
16661673
"""Object for parsing command line strings into Python objects.
16671674
16681675
Keyword Arguments:
1669-
- prog -- The name of the program (default: sys.argv[0])
1676+
- prog -- The name of the program (default:
1677+
``os.path.basename(sys.argv[0])``)
16701678
- usage -- A usage message (default: auto-generated from arguments)
16711679
- description -- A description of what the program does
16721680
- epilog -- Text following the argument descriptions
@@ -1719,7 +1727,7 @@ def __init__(self,
17191727

17201728
add_group = self.add_argument_group
17211729
self._positionals = add_group(_('positional arguments'))
1722-
self._optionals = add_group(_('optional arguments'))
1730+
self._optionals = add_group(_('options'))
17231731
self._subparsers = None
17241732

17251733
# register types

0 commit comments

Comments
 (0)