Skip to content

Commit 673d8a1

Browse files
authored
Merge pull request #672 from python-cmd2/argparse_error
ACArgumentParser no longer prints complete help text when a parsing error occurs
2 parents 91e2710 + 14b33bb commit 673d8a1

File tree

4 files changed

+16
-17
lines changed

4 files changed

+16
-17
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* `StdSim.buffer.write()` now flushes when the wrapped stream uses line buffering and the bytes being written
88
contain a newline or carriage return. This helps when `pyscript` is echoing the output of a shell command
99
since the output will print at the same frequency as when the command is run in a terminal.
10+
* **ACArgumentParser** no longer prints complete help text when a parsing error occurs since long help messages
11+
scroll the actual error message off the screen.
1012
* **Python 3.4 EOL notice**
1113
* Python 3.4 reached its [end of life](https://www.python.org/dev/peps/pep-0429/) on March 18, 2019
1214
* This is the last release of `cmd2` which will support Python 3.4
@@ -27,7 +29,7 @@
2729
`argparse.Namespace` object they pass to the `do_*` methods. It is stored in an attribute called `__statement__`.
2830
This can be useful if a command function needs to know the command line for things like logging.
2931
* Added a `-t` option to the `load` command for automatically generating a transcript based on a script file
30-
* When in a *pyscript*, the stdout and stderr streams of shell commands and processes being piped to are now
32+
* When in a **pyscript**, the stdout and stderr streams of shell commands and processes being piped to are now
3133
captured and included in the ``CommandResult`` structure.
3234
* Potentially breaking changes
3335
* The following commands now write to stderr instead of stdout when printing an error. This will make catching

cmd2/argparse_completer.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ def get_lines(parts, indent, prefix=None):
908908
# join lines into usage
909909
usage = '\n'.join(lines)
910910

911-
# prefix with 'usage:'
911+
# prefix with 'Usage:'
912912
return '%s%s\n\n' % (prefix, usage)
913913

914914
def _format_action_invocation(self, action) -> str:
@@ -970,9 +970,6 @@ def _format_args(self, action, default_metavar) -> str:
970970
result = super()._format_args(action, default_metavar)
971971
return result
972972

973-
def format_help(self):
974-
return super().format_help() + '\n'
975-
976973

977974
# noinspection PyCompatibility
978975
class ACArgumentParser(argparse.ArgumentParser):
@@ -1005,7 +1002,7 @@ def add_subparsers(self, **kwargs):
10051002

10061003
def error(self, message: str) -> None:
10071004
"""Custom error override. Allows application to control the error being displayed by argparse"""
1008-
if len(self._custom_error_message) > 0:
1005+
if self._custom_error_message:
10091006
message = self._custom_error_message
10101007
self._custom_error_message = ''
10111008

@@ -1019,9 +1016,9 @@ def error(self, message: str) -> None:
10191016
formatted_message += '\n ' + line
10201017
linum += 1
10211018

1022-
sys.stderr.write(Fore.LIGHTRED_EX + '{}\n\n'.format(formatted_message) + Fore.RESET)
1023-
# sys.stderr.write('{}\n\n'.format(formatted_message))
1024-
self.print_help()
1019+
self.print_usage(sys.stderr)
1020+
sys.stderr.write(Fore.LIGHTRED_EX + '{}\n'.format(formatted_message) + Fore.RESET)
1021+
10251022
sys.exit(1)
10261023

10271024
def format_help(self) -> str:

tests/test_autocompletion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def test_autocomp_flags_narg_max(cmd2_app):
152152

153153
def test_autcomp_narg_beyond_max(cmd2_app):
154154
out, err = run_cmd(cmd2_app, 'suggest -t movie -d 3 4 5')
155-
assert 'Error: unrecognized arguments: 5' in err[0]
155+
assert 'Error: unrecognized arguments: 5' in err[1]
156156

157157

158158
def test_autocomp_subcmd_nested(cmd2_app):

tests/test_cmd2.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ def test_base_argparse_help(base_app):
8282

8383
def test_base_invalid_option(base_app):
8484
out, err = run_cmd(base_app, 'set -z')
85-
assert 'Error: unrecognized arguments: -z' in err[0]
86-
assert out[0] == 'Usage: set [-h] [-a] [-l] [param] [value]'
85+
assert err[0] == 'Usage: set [-h] [-a] [-l] [param] [value]'
86+
assert 'Error: unrecognized arguments: -z' in err[1]
8787

8888
def test_base_shortcuts(base_app):
8989
out, err = run_cmd(base_app, 'shortcuts')
@@ -285,7 +285,7 @@ def test_pyscript_with_exception(base_app, request):
285285

286286
def test_pyscript_requires_an_argument(base_app):
287287
out, err = run_cmd(base_app, "pyscript")
288-
assert "the following arguments are required: script_path" in err[0]
288+
assert "the following arguments are required: script_path" in err[1]
289289

290290

291291
def test_base_error(base_app):
@@ -314,7 +314,7 @@ def test_load_with_empty_args(base_app):
314314
out, err = run_cmd(base_app, 'load')
315315

316316
# The load command requires a file path argument, so we should get an error message
317-
assert "the following arguments are required" in err[0]
317+
assert "the following arguments are required" in err[1]
318318
assert base_app.cmdqueue == []
319319

320320

@@ -448,7 +448,7 @@ def test_base_relative_load(base_app, request):
448448

449449
def test_relative_load_requires_an_argument(base_app):
450450
out, err = run_cmd(base_app, '_relative_load')
451-
assert 'Error: the following arguments' in err[0]
451+
assert 'Error: the following arguments' in err[1]
452452
assert base_app.cmdqueue == []
453453

454454

@@ -1590,7 +1590,7 @@ def test_alias_create(base_app):
15901590

15911591
# Use the alias
15921592
out, err = run_cmd(base_app, 'fake')
1593-
assert "the following arguments are required: script_path" in err[0]
1593+
assert "the following arguments are required: script_path" in err[1]
15941594

15951595
# See a list of aliases
15961596
out, err = run_cmd(base_app, 'alias list')
@@ -1681,7 +1681,7 @@ def test_macro_create(base_app):
16811681

16821682
# Use the macro
16831683
out, err = run_cmd(base_app, 'fake')
1684-
assert "the following arguments are required: script_path" in err[0]
1684+
assert "the following arguments are required: script_path" in err[1]
16851685

16861686
# See a list of macros
16871687
out, err = run_cmd(base_app, 'macro list')

0 commit comments

Comments
 (0)