Skip to content

Commit

Permalink
Respect CLI context order
Browse files Browse the repository at this point in the history
  • Loading branch information
frthjf committed Mar 19, 2024
1 parent 2670e96 commit d8734da
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

# Unreleased

No current changes
- Respect CLI context order

# v4.9.2

- Always use first CLI argument as target to allow non-component targets
- Determine CLI target based on order to allow non-component targets
- Ensure that config field is always reloaded from index to avoid incorrect recomputation

# v4.9.1
Expand Down
11 changes: 7 additions & 4 deletions src/machinable/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def _push(_elements, _dotlist, _version):
version.append(arg)
elif arg.startswith("--"):
# method
methods.append(arg[2:])
methods.append((len(elements), arg[2:]))
else:
# module
_push(elements, dotlist, version)
Expand Down Expand Up @@ -115,20 +115,23 @@ def main(args: Optional[List] = None):
component = None
for i, (module, *version) in enumerate(elements):
element = get(module, version)
if i == 0:
if i == len(elements) - 1:
component = element
else:
contexts.append(element.__enter__())

if component is None:
raise ValueError("You have to provide at least one interface")

for method in methods:
for i, method in methods:
# check if cli_{method} exists before falling back on {method}
target = getattr(
component, f"cli_{method}", getattr(component, method)
)
target()
if callable(target):
target()
else:
print(target)

for context in reversed(contexts):
context.__exit__()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_cli_main(capfd, tmp_storage):
main(["get", "interface.dummy", "--__call__"])
out, err = capfd.readouterr()
assert out == "Hello world!\n"
main(["get", "hello", "name=there", "interface.dummy", "--__call__"])
main(["get", "interface.dummy", "hello", "name=there", "--__call__"])
out, err = capfd.readouterr()
assert out == "Hello there!\n"

Expand Down

0 comments on commit d8734da

Please sign in to comment.