Skip to content

Commit

Permalink
add subgroup command resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
StardustDL committed Mar 12, 2024
1 parent 2815ca0 commit 3c65e15
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ jobs:
aexpy -vvv report ./cache/diff.json.gz ./cache/report.json.gz
- name: Test Image Runner
run: |
aexpy tool runimage ./cache -t stardustdl/aexpy:latest -- --version
aexpy runimage ./cache -t stardustdl/aexpy:latest -- --version
- name: Test Service Provider
env:
AEXPY_SERVICE: "./scripts/services.py"
Expand All @@ -86,7 +86,7 @@ jobs:
aexpy -vvv report ./cache/diff3.json.gz ./cache/report3.json.gz
- name: Test Stats
run: |
python -m aexpy tool stat ./cache/*.json.gz ./cache/stats.json.gz
python -m aexpy stat ./cache/*.json.gz ./cache/stats.json.gz
- name: Test View
shell: bash
run: |
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ When you installed AexPy package, you could use `tool runimage` command for a qu
```sh
# Use the same version of the image as current AexPy version
aexpy tool runimage ./mount -- --version
aexpy runimage ./mount -- --version
# Use a specified image tag
aexpy tool runimage ./mount -t stardustdl/aexpy:latest -- --version
Expand Down Expand Up @@ -329,6 +330,7 @@ It loads products from given files, runs builtin counters, and then records them

```sh
aexpy tool stat ./*.json ./stats.json
aexpy stat ./*.json ./stats.json

aexpy view ./stats.json
```
Expand Down
39 changes: 30 additions & 9 deletions src/aexpy/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from io import BytesIO, TextIOWrapper
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import IO, Any, Literal
from typing import IO, Any, Literal, cast, override

import click

Expand All @@ -31,18 +31,38 @@ class CliContext:


class AliasedGroup(click.Group):
def get_command(self, /, ctx, cmd_name):
rv = click.Group.get_command(self, ctx, cmd_name)
@override
def get_command(self, /, ctx: click.Context, cmd_name: str):
rv = super().get_command(ctx, cmd_name)
if rv is not None:
return rv
matches = [x for x in self.list_commands(ctx) if x.startswith(cmd_name)]
matches = [
(x, super().get_command(ctx, x))
for x in self.list_commands(ctx)
if x.startswith(cmd_name)
]
if not matches:
matches = [
(y, cast(click.Group, super().get_command(ctx, x)).get_command(ctx, y))
for x in self.list_commands(ctx)
if isinstance(super().get_command(ctx, x), click.Group)
for y in cast(click.Group, super().get_command(ctx, x)).list_commands(
ctx
)
if y.startswith(cmd_name)
]
if not matches:
return None
elif len(matches) == 1:
return click.Group.get_command(self, ctx, matches[0])
ctx.fail(f"Too many matches: {', '.join(sorted(matches))}")
if len(matches) == 1:
return matches[0][1]

ctx.fail(
f"Too many command matches: {', '.join(sorted([n for n, _ in matches]))}"
)

def resolve_command(self, /, ctx, args):
def resolve_command(
self, /, ctx: click.Context, args: list[str]
) -> tuple[str | None, click.Command | None, list[str]]:
# always return the full command name
_, cmd, args = super().resolve_command(ctx, args)
assert cmd is not None, "Command is None."
Expand Down Expand Up @@ -78,7 +98,8 @@ def exitWithContext[T: Product](context: ProduceContext[T]):
def versionMessage():
parts = [
"%(prog)s v%(version)s",
f"{getShortCommitId()}@{str(getBuildDate().date())}",
getShortCommitId(),
str(getBuildDate().date()),
]
if runInContainer():
parts.append("in-container")
Expand Down
4 changes: 3 additions & 1 deletion src/aexpy/tools/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
def tool(
ctx: click.Context,
) -> None:
"""Advanced tools."""
"""Advanced tools
The command name 'tool' can be omitted, directly using the name of subcommands."""
pass


Expand Down

0 comments on commit 3c65e15

Please sign in to comment.