Skip to content

Commit e7ff244

Browse files
authored
Merge pull request #74 from yassun7010/refine_select_command
Refine select argument.
2 parents 8e6b54b + 2e775a4 commit e7ff244

File tree

15 files changed

+239
-37
lines changed

15 files changed

+239
-37
lines changed

cmdcomp/v2/command/__init__.py

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,48 @@ def has_keyword_arguments(self) -> bool:
9090
...
9191

9292

93+
class _V2EmptyCommand(_V2BaseCommand):
94+
@property
95+
@override
96+
def subcommands(self) -> OrderedDict[SubcommandName, "V2Command"]:
97+
return OrderedDict()
98+
99+
@property
100+
@override
101+
def positional_arguments(self) -> OrderedDict[Position, V2Argument]:
102+
return OrderedDict()
103+
104+
@property
105+
@override
106+
def positional_wildcard_argument(self) -> V2Argument | None:
107+
return None
108+
109+
@property
110+
@override
111+
def keyword_arguments(self) -> OrderedDict[Keyword, V2Argument]:
112+
return OrderedDict()
113+
114+
@property
115+
@override
116+
def has_subcommands(self) -> bool:
117+
return False
118+
119+
@property
120+
@override
121+
def has_positional_arguments(self) -> bool:
122+
return False
123+
124+
@property
125+
@override
126+
def has_positional_wildcard_argument(self) -> bool:
127+
return False
128+
129+
@property
130+
@override
131+
def has_keyword_arguments(self) -> bool:
132+
return False
133+
134+
93135
class V2PoristionalArgumentsCommand(_V2BaseCommand):
94136
arguments: Annotated[
95137
OrderedDict[Position | Literal["*"] | Keyword, _InputArgument | None],
@@ -228,18 +270,39 @@ def has_keyword_arguments(self) -> bool:
228270
return len(self.keyword_arguments) != 0
229271

230272

231-
V2Command = V2PoristionalArgumentsCommand | V2SubcommandsCommand
273+
class V2RelayCommand(_V2EmptyCommand):
274+
"""relay completion of other command."""
275+
276+
type: Annotated[
277+
Literal["relay"],
278+
Field(title="relay completion of other command."),
279+
]
280+
281+
description: Annotated[
282+
str | None,
283+
Field(title="description of the argument."),
284+
] = None
285+
286+
alias: Annotated[
287+
str | list[str] | None,
288+
Field(title="alias of the argument."),
289+
] = None
290+
291+
target: Annotated[str, Field(title="relay target.")]
292+
293+
294+
V2Command = V2PoristionalArgumentsCommand | V2SubcommandsCommand | V2RelayCommand
232295

233296

234297
def _convert_argument(value: _InputArgument | None) -> V2Argument:
235298
match value:
236299
case str():
237-
return V2SelectArgument(type="select", raw_values=[value])
300+
return V2SelectArgument(type="select", options=[value])
238301

239302
case list():
240303
return V2SelectArgument(
241304
type="select",
242-
raw_values=[v for v in value],
305+
options=[v for v in value],
243306
)
244307

245308
case None:

cmdcomp/v2/command/argument/command_argument.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77

88

99
class V2CommandArgument(HasAlias, Model):
10-
"""complete with the result of executing the command in the Shell command."""
10+
"""complete with the result of executing the command."""
1111

12-
type: Literal["command"]
12+
type: Annotated[
13+
Literal["command"],
14+
Field(title="complete with the result of executing the command."),
15+
]
1316

1417
description: Annotated[
1518
str | None,

cmdcomp/v2/command/argument/file_argument.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88

99
class V2FileArgument(HasAlias, Model):
10+
"""completion of file names starting from the specified directory."""
11+
1012
type: Literal["file"]
1113

1214
description: Annotated[

cmdcomp/v2/command/argument/flag_argument.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77

88

99
class V2FlagArgument(HasAlias, Model):
10-
type: Literal["flag"]
10+
"""completion of flags to support enable/disable."""
11+
12+
type: Annotated[
13+
Literal["flag"],
14+
Field(title="completion of flags to support enable/disable."),
15+
]
1116

1217
description: Annotated[
1318
str | None,
Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from functools import cached_property
21
from typing import Annotated, Literal
32

43
from pydantic import Field
@@ -8,7 +7,12 @@
87

98

109
class V2SelectArgument(HasAlias, Model):
11-
type: Literal["select"]
10+
"""completion for choosing from options."""
11+
12+
type: Annotated[
13+
Literal["select"],
14+
Field(title="completion for choosing from options."),
15+
]
1216

1317
description: Annotated[
1418
str | None,
@@ -20,19 +24,30 @@ class V2SelectArgument(HasAlias, Model):
2024
Field(title="alias of the argument."),
2125
] = None
2226

23-
raw_values: Annotated[
24-
str | list[str],
27+
raw_options: Annotated[
28+
list[str] | None,
2529
Field(
26-
title="candidate selection.",
27-
alias="values",
30+
title="completion candidates.",
31+
alias="options",
2832
),
29-
]
33+
] = None
3034

31-
@cached_property
32-
def values(self) -> list[str]:
33-
match self.raw_values:
34-
case str():
35-
return [self.raw_values]
35+
values: Annotated[
36+
list[str] | str | None,
37+
Field(
38+
title="completion candidates.",
39+
description="this field is deprecated. use `options` instead.",
40+
json_schema_extra={"deprecated": True},
41+
),
42+
] = None
3643

37-
case list():
38-
return self.raw_values
44+
@property
45+
def options(self) -> list[str]:
46+
if self.raw_options is not None:
47+
return self.raw_options
48+
if self.values is None:
49+
return []
50+
elif isinstance(self.values, str):
51+
return [self.values]
52+
else:
53+
return self.values

cmdcomp/v2/templates/bash.sh.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
{%- macro argument_completion(argument) -%}
66
{% if argument.type == "select" -%}
77
if [ $cur -eq $COMP_CWORD ] ; then
8-
COMPREPLY=( $(compgen -W "{{ argument.values|join(" ") }}" -- "$cur") )
8+
COMPREPLY=( $(compgen -W "{{ argument.options|join(" ") }}" -- "$cur") )
99

1010
return 0
1111
else

cmdcomp/v2/templates/zsh.sh.jinja

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
{#- macro def -#}
1919
{%- macro contents(arg_name, argument) -%}
2020
{%- if argument.type == 'select' -%}
21-
:values:{{ "(" + argument.values|join(" ") + ")" }}
21+
:values:{{ "(" + argument.options|join(" ") + ")" }}
2222
{%- elif argument.type == 'file' -%}
2323
:file:_files{%- if argument.base_path is not none %} -W "{{ argument.base_path }}"{%- endif -%}
2424
{%- elif argument.type == 'command' -%}
@@ -41,6 +41,10 @@
4141

4242
case $cmd_name in
4343
{%- for cmd_name, command in commands.items() recursive %}
44+
{%- if command.type == "relay" %}
45+
{{ ([cmd_name] + command.aliases)|join("|") }})
46+
;;
47+
{%- else -%}
4448
{%- set scope = scope + "_" + cmd_name %}
4549
{{ ([cmd_name] + command.aliases)|join("|") }})
4650
{%- if command.has_subcommands %}
@@ -83,6 +87,7 @@
8387
esac
8488
{%- endif %}
8589
;;
90+
{%- endif %}
8691
{% endfor %}
8792
esac
8893

docs/config.schema.json

Lines changed: 88 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,11 @@
265265
},
266266
"V2CommandArgument": {
267267
"additionalProperties": false,
268-
"description": "complete with the result of executing the command in the Shell command.",
268+
"description": "complete with the result of executing the command.",
269269
"properties": {
270270
"type": {
271271
"const": "command",
272-
"title": "Type"
272+
"title": "complete with the result of executing the command."
273273
},
274274
"description": {
275275
"anyOf": [
@@ -330,6 +330,9 @@
330330
},
331331
{
332332
"$ref": "#/$defs/V2SubcommandsCommand"
333+
},
334+
{
335+
"$ref": "#/$defs/V2RelayCommand"
333336
}
334337
],
335338
"title": "Root"
@@ -345,6 +348,7 @@
345348
},
346349
"V2FileArgument": {
347350
"additionalProperties": false,
351+
"description": "completion of file names starting from the specified directory.",
348352
"properties": {
349353
"type": {
350354
"const": "file",
@@ -401,10 +405,11 @@
401405
},
402406
"V2FlagArgument": {
403407
"additionalProperties": false,
408+
"description": "completion of flags to support enable/disable.",
404409
"properties": {
405410
"type": {
406411
"const": "flag",
407-
"title": "Type"
412+
"title": "completion of flags to support enable/disable."
408413
},
409414
"description": {
410415
"anyOf": [
@@ -516,12 +521,63 @@
516521
"title": "V2PoristionalArgumentsCommand",
517522
"type": "object"
518523
},
524+
"V2RelayCommand": {
525+
"additionalProperties": false,
526+
"description": "relay completion of other command.",
527+
"properties": {
528+
"description": {
529+
"anyOf": [
530+
{
531+
"type": "string"
532+
},
533+
{
534+
"type": "null"
535+
}
536+
],
537+
"default": null,
538+
"title": "description of the argument."
539+
},
540+
"alias": {
541+
"anyOf": [
542+
{
543+
"type": "string"
544+
},
545+
{
546+
"items": {
547+
"type": "string"
548+
},
549+
"type": "array"
550+
},
551+
{
552+
"type": "null"
553+
}
554+
],
555+
"default": null,
556+
"title": "alias of the argument."
557+
},
558+
"type": {
559+
"const": "relay",
560+
"title": "relay completion of other command."
561+
},
562+
"target": {
563+
"title": "relay target.",
564+
"type": "string"
565+
}
566+
},
567+
"required": [
568+
"type",
569+
"target"
570+
],
571+
"title": "V2RelayCommand",
572+
"type": "object"
573+
},
519574
"V2SelectArgument": {
520575
"additionalProperties": false,
576+
"description": "completion for choosing from options.",
521577
"properties": {
522578
"type": {
523579
"const": "select",
524-
"title": "Type"
580+
"title": "completion for choosing from options."
525581
},
526582
"description": {
527583
"anyOf": [
@@ -553,24 +609,44 @@
553609
"default": null,
554610
"title": "alias of the argument."
555611
},
556-
"values": {
612+
"options": {
557613
"anyOf": [
558614
{
559-
"type": "string"
615+
"items": {
616+
"type": "string"
617+
},
618+
"type": "array"
560619
},
620+
{
621+
"type": "null"
622+
}
623+
],
624+
"default": null,
625+
"title": "completion candidates."
626+
},
627+
"values": {
628+
"anyOf": [
561629
{
562630
"items": {
563631
"type": "string"
564632
},
565633
"type": "array"
634+
},
635+
{
636+
"type": "string"
637+
},
638+
{
639+
"type": "null"
566640
}
567641
],
568-
"title": "candidate selection."
642+
"default": null,
643+
"deprecated": true,
644+
"description": "this field is deprecated. use `options` instead.",
645+
"title": "completion candidates."
569646
}
570647
},
571648
"required": [
572-
"type",
573-
"values"
649+
"type"
574650
],
575651
"title": "V2SelectArgument",
576652
"type": "object"
@@ -652,6 +728,9 @@
652728
{
653729
"$ref": "#/$defs/V2SubcommandsCommand"
654730
},
731+
{
732+
"$ref": "#/$defs/V2RelayCommand"
733+
},
655734
{
656735
"type": "null"
657736
}

0 commit comments

Comments
 (0)