Skip to content

Commit

Permalink
Merge pull request #225 from ivanov/better-errors
Browse files Browse the repository at this point in the history
Print full usage on missing or invalid commands
  • Loading branch information
kevin-bates authored May 7, 2021
2 parents 19c9fca + d762b35 commit 46a9c51
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
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:
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

0 comments on commit 46a9c51

Please sign in to comment.