Issue 34191: argparse: Missing subparser error message should be more clear (original) (raw)
argparse produces a long unreadable, non-understandable for non-programmers error message when a subparser is not specified in the command line.
Note that the below message contains Ubuntu specifics, but it seems it would be nearly as unclear on other OSes too.
The worst thing about this error message is that to produce something readable instead I need to somehow parse the command string (or at least to catch TypeError).
$ python3.7 test.py Traceback (most recent call last): File "test.py", line 10, in args = parser.parse_args() File "/usr/lib/python3.7/argparse.py", line 1754, in parse_args args, argv = self.parse_known_args(args, namespace) File "/usr/lib/python3.7/argparse.py", line 1786, in parse_known_args namespace, args = self._parse_known_args(args, namespace) File "/usr/lib/python3.7/argparse.py", line 2021, in _parse_known_args ', '.join(required_actions)) TypeError: sequence item 0: expected str instance, NoneType found Error in sys.excepthook: Traceback (most recent call last): File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook from apport.fileutils import likely_packaged, get_recent_crashes File "/usr/lib/python3/dist-packages/apport/init.py", line 5, in from apport.report import Report File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in import apport.fileutils File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in from apport.packaging_impl import impl as packaging File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 24, in import apt File "/usr/lib/python3/dist-packages/apt/init.py", line 23, in import apt_pkg ModuleNotFoundError: No module named 'apt_pkg'
Original exception was: Traceback (most recent call last): File "test.py", line 10, in args = parser.parse_args() File "/usr/lib/python3.7/argparse.py", line 1754, in parse_args args, argv = self.parse_known_args(args, namespace) File "/usr/lib/python3.7/argparse.py", line 1786, in parse_known_args namespace, args = self._parse_known_args(args, namespace) File "/usr/lib/python3.7/argparse.py", line 2021, in _parse_known_args ', '.join(required_actions)) TypeError: sequence item 0: expected str instance, NoneType found
The script follows.
#!/usr/bin/env python
import argparse
parser = argparse.ArgumentParser(description="Automatically process XML") subparsers = parser.add_subparsers(title='subcommands')
chain_parser = subparsers.add_parser('chain', aliases=['c'], help='Automatically run a chain of transformations')
args = parser.parse_args()
Your code runs fine under 3.6.5.
But if I add 'subparsers.required=True', I get your error. It's having problems formatting the name of the subparsers command when issuing the error message.
If I add a 'dest' to the add_subparsers I get the expected error message:
import argparse
parser = argparse.ArgumentParser(description="Automatically process XML")
subparsers = parser.add_subparsers(title='subcommands', dest='cmd')
subparsers.required=True
chain_parser = subparsers.add_parser('chain', aliases=['c'],
help='Automatically run a chain of transformations')
args = parser.parse_args()
print(args)
The default 'dest' for subparsers is None, and the error arises from
','.join([None])
The problems with a missing 'dest' came up in earlier discussions about making required/not required subparsers. 3.7 made subparsers 'required' by default, but it appears that it hasn't addressed this missing 'dest' problem.
But I haven't followed the latest release details closely.