Skip to content
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
12 changes: 8 additions & 4 deletions magicli.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def magicli():
def is_command(argv, module):
"""
Checks if the first argument is a valid command in the module and returns
the function to call if `argv[0]` is public and not excluded in `__all__`,
the function to call if `argv[0]` is public and not excluded in `__all__`.
"""
if (
argv
Expand Down Expand Up @@ -110,7 +110,7 @@ def parse_short_options(short_options, docstring, iter_argv, parameters, kwargs)

def short_to_long_option(short, docstring):
"""
Converts a one character short option to a long option accoring to the help message.
Converts a one character short option to a long option according to the help message.
"""
template = f"-{short}, --"
if (start := docstring.find(template)) != -1:
Expand Down Expand Up @@ -168,7 +168,11 @@ def check_for_version(argv, parameters, docstring, module):
"version" not in parameters
and any(
(argv == [arg] and string in docstring)
for arg, string in [("--version", "--version"), ("-v", "-v, --version")]
for arg, string in [
("--version", "--version"),
("-v", "-v, --version"),
("-V", "-V, --version"),
]
)
and module
):
Expand Down Expand Up @@ -234,7 +238,7 @@ def load_module(name):


def get_commands(module):
"""Returns list of public commands, unless not present in `__all__`."""
"""Returns list of public commands that are not present in `__all__`."""
return [
name
for name, _ in inspect.getmembers(module, inspect.isfunction)
Expand Down
13 changes: 7 additions & 6 deletions tests/test_magicli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


def name():
"--version"
"-V, --version"
global ANSWER
ANSWER = 1

Expand Down Expand Up @@ -101,11 +101,12 @@ def test_short_option_with_wrong_type(mocked):

@mock.patch("importlib.import_module", side_effect=module_version)
def test_version(mocked, capsys):
sys.argv = ["name", "--version"]
with pytest.raises(SystemExit) as error:
magicli()
assert error.value.code is None
assert capsys.readouterr()[0] == "1.2.3\n"
for version in ["--version", "-V"]:
sys.argv = ["name", version]
with pytest.raises(SystemExit) as error:
magicli()
assert error.value.code is None
assert capsys.readouterr()[0] == "1.2.3\n"

sys.argv = ["name", "-v"]
with pytest.raises(SystemExit) as error:
Expand Down