Skip to content

Commit

Permalink
Print usage for module tool without subcommand (#1515)
Browse files Browse the repository at this point in the history
* Print usage for module tool without subcommand
  • Loading branch information
bartv authored Oct 22, 2019
1 parent a4f686f commit c1d6c42
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/inmanta/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

from inmanta import const, module, moduletool, protocol
from inmanta.ast import CompilerException
from inmanta.command import CLIException, Commander, command
from inmanta.command import CLIException, Commander, command, ShowUsageException
from inmanta.compiler import do_compile
from inmanta.config import Config
from inmanta.const import EXIT_START_FAILED
Expand Down Expand Up @@ -625,6 +625,9 @@ def report(e: Exception) -> None:

try:
options.func(options)
except ShowUsageException as e:
print(e.args[0], file=sys.stderr)
parser.print_usage()
except CLIException as e:
report(e)
sys.exit(e.exitcode)
Expand Down
8 changes: 7 additions & 1 deletion src/inmanta/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@


class CLIException(Exception):
def __init__(self, exitcode, *args, **kwargs):
def __init__(self, exitcode: int, *args, **kwargs):
self.exitcode = exitcode
super(CLIException, self).__init__(*args, **kwargs)


class ShowUsageException(Exception):
"""
Raise this exception to show the usage message of the given level
"""


class Commander(object):
"""
This class handles commands
Expand Down
8 changes: 6 additions & 2 deletions src/inmanta/moduletool.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

import inmanta
from inmanta.ast import Namespace, RuntimeException
from inmanta.command import CLIException
from inmanta.command import CLIException, ShowUsageException
from inmanta.const import MAX_UPDATE_ATTEMPT
from inmanta.module import INSTALL_MASTER, INSTALL_RELEASES, Module, Project, gitprovider
from inmanta.parser.plyInmantaParser import parse
Expand Down Expand Up @@ -80,7 +80,11 @@ def execute(self, cmd, args):
outargs = {k: getattr(args, k) for k in margs if hasattr(args, k)}
method(**outargs)
else:
raise Exception("%s not implemented" % cmd)
if cmd is None or cmd == "":
msg = "A subcommand is required."
else:
msg = f"{cmd} does not exist."
raise ShowUsageException(msg)

def get_project(self, load=False) -> Project:
project = Project.get()
Expand Down
8 changes: 8 additions & 0 deletions tests/test_app_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import pytest

from inmanta.app import cmd_parser
from inmanta.command import ShowUsageException
from inmanta.config import Config
from inmanta.const import VersionState

Expand Down Expand Up @@ -85,6 +86,13 @@ def test_help_sub(inmanta_config, capsys):
assert "update" in out


def test_module_help(inmanta_config, capsys):
with pytest.raises(ShowUsageException) as info:
app(["module"])

assert info.value.args[0].startswith("A subcommand is required.")


@pytest.mark.parametrize("push_method", [([]), (["-d"]), (["-d", "--full"])])
@pytest.mark.asyncio
async def test_export(tmpdir, server, client, push_method):
Expand Down

0 comments on commit c1d6c42

Please sign in to comment.