Skip to content

Commit c9b7350

Browse files
committed
New passthrought in shell and ordered help for subcommands
1 parent c53d579 commit c9b7350

5 files changed

Lines changed: 42 additions & 3 deletions

File tree

Justfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@test *options: lint
1+
@test *options: format
22
uv run pytest {{options}}
33

44
@lint:

devcli/framework/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import inspect
1313

1414
import devcli.framework.console as console
15+
from devcli.core.cli import OrderedGroup
1516

1617
_logger = logging.getLogger()
1718

@@ -23,7 +24,7 @@ def new(description: str = None) -> typer.Typer:
2324
:returns: a typer.Typer
2425
"""
2526
_logger.info(f"creating new command description:{description}")
26-
return typer.Typer(help=description, no_args_is_help=True)
27+
return typer.Typer(help=description, no_args_is_help=True, cls=OrderedGroup)
2728

2829

2930
def stop(msg: str = "Error", exit_code: int = 1):

devcli/utils/shell.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,24 @@ def capture(command: str, cwd: str = os.curdir) -> str:
141141
return result.stdout
142142

143143

144+
def passthrough(command: str, cwd: str = os.curdir) -> int:
145+
"""
146+
Execute command with minimal interference, won't capture
147+
the output nor it can run things in parallel
148+
"""
149+
logger.debug(f"running shell passthrough: {command}")
150+
final_command = _prepare_command(command)
151+
logger.debug(f"about to execute: {final_command}, on: {cwd}")
152+
result = subprocess.run(
153+
final_command,
154+
shell=True,
155+
cwd=cwd,
156+
env=os.environ,
157+
)
158+
logger.debug(f"return code: {result.returncode}")
159+
return result.returncode
160+
161+
144162
def promote_value_to_key(nested_dict: dict, new_key: str, new_value: str) -> dict:
145163
"""This function works on a dict which has values that are also dicts.
146164
It will swap a value of the nested dict and add the key as a value.

tests/test_cli.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,22 @@ def test_should_load_dynamic_commands():
4444
# of the project, tests of the subcommand 'example' itself
4545
# are in test_example.py
4646
result = devcli("example")
47-
assert "ping" in result.output
47+
assert "config" in result.output
48+
49+
50+
def test_help_lists_subcommands_in_alphabetical_order():
51+
result = devcli("example")
52+
subcommands = ["│ config", "│ hello", "│ ping", "│ text"]
53+
index = 0 # order of the commands
54+
# per line, mark each found subcommand
55+
# if we can mark all they were in order
56+
for line in result.output.split("\n"):
57+
print(f"index: {index} line:{line}")
58+
if index < len(subcommands) and subcommands[index] in line:
59+
index += 1
60+
61+
# Found all subcommands in order we got to 4 index
62+
assert index == 4
4863

4964

5065
def test_show_config():

tests/utils/test_shell.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ def test_in_multi_run_is_possible_to_collect_exitcode():
4343
assert results[1]["exitcode"] == 222
4444

4545

46+
def test_passthrough_just_execute_without_capture():
47+
result = shell.passthrough("exit 255")
48+
assert result == 255
49+
50+
4651
def test_result_transform_dict():
4752
orig = {
4853
123: {"alias": "cmd1", "code": "111"},

0 commit comments

Comments
 (0)