Skip to content

Commit 8b2cf36

Browse files
authored
Merge pull request #39 from python-ellar/cli_version
CLI Version Option
2 parents d8b8f1b + 4cea1dd commit 8b2cf36

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

ellar_cli/main.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import typing as t
44

55
import click
6+
import ellar
67
import typer
78
from ellar.common.commands import EllarTyper
89
from ellar.common.constants import CALLABLE_COMMAND_INFO, MODULE_METADATA
@@ -11,6 +12,7 @@
1112
from ellar.core.services import Reflector
1213
from typer.models import CommandInfo
1314

15+
import ellar_cli
1416
from ellar_cli.constants import ELLAR_META
1517

1618
from .manage_commands import create_module, create_project, new_command, runserver
@@ -27,17 +29,33 @@
2729
_typer.command(name="create-module")(create_module)
2830

2931

32+
def version_callback(value: bool) -> None:
33+
if value:
34+
click.echo("===========================================================")
35+
click.echo(f"Ellar CLI Version: {ellar_cli.__version__}")
36+
click.echo("-----------------------------------------------------------")
37+
click.echo(f"Ellar Version: {ellar.__version__}")
38+
click.echo("===========================================================")
39+
raise typer.Exit(0)
40+
41+
3042
@_typer.callback()
3143
def typer_callback(
3244
ctx: typer.Context,
33-
project: t.Optional[str] = typer.Option(
34-
None,
35-
"-p",
45+
project: str = typer.Option(
46+
"default",
3647
"--project",
3748
show_default=True,
3849
exists=True,
3950
help="Run Specific Command on a specific project",
4051
),
52+
version: t.Optional[bool] = typer.Option(
53+
False,
54+
"--version",
55+
callback=version_callback,
56+
help="CLI Version",
57+
show_default=False,
58+
),
4159
) -> None:
4260
meta_: t.Optional[EllarCLIService] = EllarCLIService.import_project_meta(project)
4361
ctx.meta[ELLAR_META] = meta_
@@ -49,11 +67,11 @@ def build_typers() -> t.Any: # pragma: no cover
4967
argv = list(sys.argv)
5068
options, args = getopt.getopt(
5169
argv[1:],
52-
"hp:",
53-
["project=", "help"],
70+
"",
71+
["project=", "help", "version"],
5472
)
5573
for k, v in options:
56-
if k in ["-p", "--project"] and v:
74+
if k in ["--project"] and v:
5775
app_name = v
5876
except Exception as ex:
5977
click.echo(ex)

tests/test_build_typers.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_build_typers_command_works_for_default_project(cli_runner):
2222
def test_build_typers_ellar_typer_for_specific_project_works():
2323
os.chdir(sample_app_path)
2424
result = subprocess.run(
25-
["ellar", "-p", "example_project_2", "db", "create-migration"],
25+
["ellar", "--project", "example_project_2", "db", "create-migration"],
2626
stdout=subprocess.PIPE,
2727
)
2828
assert result.returncode == 0
@@ -40,14 +40,14 @@ def test_build_typers_command_for_specific_project_works():
4040
os.chdir(sample_app_path)
4141

4242
result = subprocess.run(
43-
["ellar", "-p", "example_project", "whatever-you-want"],
43+
["ellar", "--project", "example_project", "whatever-you-want"],
4444
stdout=subprocess.PIPE,
4545
)
4646
assert result.returncode == 0
4747
assert result.stdout == b"Whatever you want command\n"
4848

4949
result = subprocess.run(
50-
["ellar", "-p", "example_project_2", "whatever-you-want"],
50+
["ellar", "--project", "example_project_2", "whatever-you-want"],
5151
stdout=subprocess.PIPE,
5252
)
5353
assert result.returncode == 0
@@ -60,12 +60,12 @@ def test_help_command(cli_runner):
6060
assert result.returncode == 0
6161

6262
result = subprocess.run(
63-
["ellar", "-p", "example_project", "--help"], stdout=subprocess.PIPE
63+
["ellar", "--project", "example_project", "--help"], stdout=subprocess.PIPE
6464
)
6565
assert result.returncode == 0
6666

6767
result = subprocess.run(
68-
["ellar", "-p", "example_project_2", "--help"], stdout=subprocess.PIPE
68+
["ellar", "--project", "example_project_2", "--help"], stdout=subprocess.PIPE
6969
)
7070
assert result.returncode == 0
7171

tests/test_ellar_cli_service.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,13 @@ def test_import_root_module_works(write_empty_py_project, process_runner):
170170
root_module = ellar_cli_service.import_root_module()
171171

172172
assert issubclass(root_module, ModuleBase)
173+
174+
175+
def test_version_works(write_empty_py_project, process_runner):
176+
result = process_runner(["ellar", "--version"])
177+
assert result.returncode == 0
178+
179+
assert (
180+
result.stdout
181+
== b"===========================================================\nEllar CLI Version: 0.2.2\n-----------------------------------------------------------\nEllar Version: 0.4.7\n===========================================================\n"
182+
)

0 commit comments

Comments
 (0)