Skip to content

Commit

Permalink
🍻 adjust completion
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Jan 7, 2024
1 parent 31e1fae commit 07655d3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
24 changes: 13 additions & 11 deletions src/arclet/alconna/_internal/_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,14 +663,14 @@ def _handle_shortcut_reg(argv: Argv, groups: tuple[str, ...], gdict: dict[str, s


def _prompt_unit(analyser: Analyser, argv: Argv, trig: Arg):
if comp := trig.field.get_completion():
if isinstance(comp, str):
return [Prompt(comp, False)]
releases = argv.release(recover=True)
target = str(releases[-1]) or str(releases[-2])
o = list(filter(lambda x: target in x, comp)) or comp
return [Prompt(i, False, target) for i in o]
return [Prompt(analyser.command.formatter.param(trig), False)]
if not (comp := trig.field.get_completion()):
return [Prompt(analyser.command.formatter.param(trig), False)]
if isinstance(comp, str):
return [Prompt(f"{trig.name}: {comp}", False)]

This comment has been minimized.

Copy link
@MelodyKnit

MelodyKnit Dec 8, 2024

Prompt中带有name

为什么RF佬要给Prompt加name,可不可以改成可选

releases = argv.release(recover=True)
target = str(releases[-1]) or str(releases[-2])
o = list(filter(lambda x: target in x, comp)) or comp
return [Prompt(f"{trig.name}: {i}", False, target) for i in o]


def _prompt_sentence(analyser: Analyser):
Expand All @@ -691,10 +691,12 @@ def _prompt_none(analyser: Analyser, argv: Argv, got: list[str]):
res: list[Prompt] = []
if not analyser.args_result and analyser.self_args.argument:
unit = analyser.self_args.argument[0]
if comp := unit.field.get_completion():
res.extend([Prompt(comp, False)] if isinstance(comp, str) else [Prompt(i, False) for i in comp])
else:
if not (comp := unit.field.get_completion()):
res.append(Prompt(analyser.command.formatter.param(unit), False))
elif isinstance(comp, str):
res.append(Prompt(f"{unit.name}: {comp}", False))
else:
res.extend(Prompt(f"{unit.name}: {i}", False) for i in comp)
for opt in filter(
lambda x: x.name not in (argv.special if len(analyser.command.options) > 3 else argv.completion_names),
analyser.command.options,
Expand Down
7 changes: 4 additions & 3 deletions src/arclet/alconna/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,13 +428,14 @@ def __or__(self, other: Alconna) -> Self:
def _calc_hash(self):
return hash((self.path + str(self.prefixes), self.meta, *self.options, *self.args))

def __call__(self, *args, **kwargs):
def __call__(self, *args):
if args:
return self.parse(list(args)) # type: ignore
head = handle_argv()
argv = [(f"\"{arg}\"" if any(arg.count(sep) for sep in self.separators) else arg) for arg in sys.argv[1:]]
if head != self.command:
return self.parse(sys.argv[1:]) # type: ignore
return self.parse([head, *sys.argv[1:]]) # type: ignore
return self.parse(argv) # type: ignore
return self.parse([head, *argv]) # type: ignore

@property
def headers(self):
Expand Down
6 changes: 3 additions & 3 deletions tests/core_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,11 +604,11 @@ def test_completion():
+ Option("fool")
+ Option(
"foo",
Args["bar", "a|b|c", Field(completion=lambda: "test completion; choose a, b or c")],
Args["bar", "a|b|c", Field(completion=lambda: "choose a, b or c")],
)
+ Option(
"off",
Args["baz", "aaa|aab|abc", Field(completion=lambda: ["aaa", "aab", "abc"])],
Args["baz", "aaa|aab|abc", Field(completion=lambda: ["use aaa", "use aab", "use abc"])],
)
+ Args["test", int, Field(1, completion=lambda: "try -1")]
)
Expand All @@ -618,7 +618,7 @@ def test_completion():
alc20.parse("core20 fo --comp")
alc20.parse("core20 foo --comp")
alc20.parse("core20 fool --comp")
alc20.parse("core20 off c --comp")
alc20.parse("core20 off b --comp")

alc20_1 = Alconna("core20_1", Args["foo", int], Option("bar"))
res = alc20_1.parse("core20_1 -cp")
Expand Down

0 comments on commit 07655d3

Please sign in to comment.