Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print full usage on missing or invalid commands #225

Merged
merged 3 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions jupyter_core/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def jupyter_parser():
parser = JupyterParser(
description="Jupyter: Interactive Computing",
)
group = parser.add_mutually_exclusive_group(required=True)
group = parser.add_mutually_exclusive_group(required=False)
# don't use argparse's version action because it prints to stderr on py2
group.add_argument('--version', action='store_true',
help="show the jupyter command's version and exit")
Expand Down Expand Up @@ -122,12 +122,12 @@ def _jupyter_abspath(subcommand):
abs_path = which(jupyter_subcommand, path=search_path)
if abs_path is None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we filter out subcommands == 'help' as a special case to avoid jupyter help printing Jupyter command 'jupyter-help' not found.? This would essentially make jupyter help == jupyter --help. I can see both arguments, but this seems a bit more friendly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I wasn't sure if special casing made sense, but the fact that git help is a command convinced me that we should treat the two equivalently. Updated the PR.

raise Exception(
'Jupyter command `{}` not found.'.format(jupyter_subcommand)
'\nJupyter command `{}` not found.'.format(jupyter_subcommand)
)

if not os.access(abs_path, os.X_OK):
raise Exception(
'Jupyter command `{}` is not executable.'.format(jupyter_subcommand)
'\nJupyter command `{}` is not executable.'.format(jupyter_subcommand)
)

return abs_path
Expand Down Expand Up @@ -279,12 +279,16 @@ def main():
return

if not subcommand:
parser.print_usage(file=sys.stderr)
sys.exit("subcommand is required")
parser.print_help(file=sys.stderr)
sys.exit("\nPlease specify a subcommand or one of the optional arguments.")

try:
command = _jupyter_abspath(subcommand)
except Exception as e:
parser.print_help(file=sys.stderr)
# special-case alias of "jupyter help" to "jupyter --help"
if subcommand == "help":
return
sys.exit(e)

try:
Expand Down
2 changes: 1 addition & 1 deletion jupyter_core/tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def test_subcommand_not_found():
with pytest.raises(CalledProcessError) as excinfo:
get_jupyter_output('nonexistant-subcommand')
stderr = excinfo.value.stderr.decode('utf8')
assert stderr == 'Jupyter command `jupyter-nonexistant-subcommand` not found.' + os.linesep
assert 'Jupyter command `jupyter-nonexistant-subcommand` not found.' in stderr

@patch.object(sys, 'argv', [__file__] + sys.argv[1:])
def test_subcommand_list(tmpdir):
Expand Down