Skip to content

Commit 2e775a4

Browse files
committed
Add: RelayCommand.
1 parent 81fe6de commit 2e775a4

File tree

7 files changed

+146
-21
lines changed

7 files changed

+146
-21
lines changed

cmdcomp/v2/command/__init__.py

Lines changed: 64 additions & 1 deletion
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,7 +270,28 @@ 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:

cmdcomp/v2/templates/zsh.sh.jinja

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,9 @@
330330
},
331331
{
332332
"$ref": "#/$defs/V2SubcommandsCommand"
333+
},
334+
{
335+
"$ref": "#/$defs/V2RelayCommand"
333336
}
334337
],
335338
"title": "Root"
@@ -502,9 +505,6 @@
502505
{
503506
"$ref": "#/$defs/V2FlagArgument"
504507
},
505-
{
506-
"$ref": "#/$defs/V2RelayArgument"
507-
},
508508
{
509509
"type": "null"
510510
}
@@ -521,14 +521,10 @@
521521
"title": "V2PoristionalArgumentsCommand",
522522
"type": "object"
523523
},
524-
"V2RelayArgument": {
524+
"V2RelayCommand": {
525525
"additionalProperties": false,
526526
"description": "relay completion of other command.",
527527
"properties": {
528-
"type": {
529-
"const": "relay",
530-
"title": "relay completion of other command."
531-
},
532528
"description": {
533529
"anyOf": [
534530
{
@@ -559,6 +555,10 @@
559555
"default": null,
560556
"title": "alias of the argument."
561557
},
558+
"type": {
559+
"const": "relay",
560+
"title": "relay completion of other command."
561+
},
562562
"target": {
563563
"title": "relay target.",
564564
"type": "string"
@@ -568,7 +568,7 @@
568568
"type",
569569
"target"
570570
],
571-
"title": "V2RelayArgument",
571+
"title": "V2RelayCommand",
572572
"type": "object"
573573
},
574574
"V2SelectArgument": {
@@ -610,16 +610,43 @@
610610
"title": "alias of the argument."
611611
},
612612
"options": {
613-
"items": {
614-
"type": "string"
615-
},
616-
"title": "completion candidates.",
617-
"type": "array"
613+
"anyOf": [
614+
{
615+
"items": {
616+
"type": "string"
617+
},
618+
"type": "array"
619+
},
620+
{
621+
"type": "null"
622+
}
623+
],
624+
"default": null,
625+
"title": "completion candidates."
626+
},
627+
"values": {
628+
"anyOf": [
629+
{
630+
"items": {
631+
"type": "string"
632+
},
633+
"type": "array"
634+
},
635+
{
636+
"type": "string"
637+
},
638+
{
639+
"type": "null"
640+
}
641+
],
642+
"default": null,
643+
"deprecated": true,
644+
"description": "this field is deprecated. use `options` instead.",
645+
"title": "completion candidates."
618646
}
619647
},
620648
"required": [
621-
"type",
622-
"options"
649+
"type"
623650
],
624651
"title": "V2SelectArgument",
625652
"type": "object"
@@ -683,9 +710,6 @@
683710
{
684711
"$ref": "#/$defs/V2FlagArgument"
685712
},
686-
{
687-
"$ref": "#/$defs/V2RelayArgument"
688-
},
689713
{
690714
"type": "null"
691715
}
@@ -704,6 +728,9 @@
704728
{
705729
"$ref": "#/$defs/V2SubcommandsCommand"
706730
},
731+
{
732+
"$ref": "#/$defs/V2RelayCommand"
733+
},
707734
{
708735
"type": "null"
709736
}

examples/v2/config.cmdcomp.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ type = "command"
7272
description = "run script."
7373
execute = "echo 'script1.sh script2.sh script3.sh'"
7474

75+
[root.subcommands.git]
76+
type = "relay"
77+
description = "git command."
78+
target = "git"
79+
7580
[root.subcommands.test]
7681
description = "test command."
7782

examples/v2/config.cmdcomp.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ root:
6363
type: command
6464
description: "run script."
6565
execute: "echo 'script1.sh script2.sh script3.sh'"
66+
git:
67+
type: relay
68+
description: git command.
69+
target: "git"
6670
test:
6771
description: "test command."
6872
subcommands:

examples/v2/output.bash

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ _cliname() {
4040
cur=$(( cur + opts_cur + 1 ))
4141
;;
4242

43+
_cliname,git)
44+
cmd="_cliname_git"
45+
cur=$(( cur + opts_cur + 1 ))
46+
;;
47+
4348
_cliname,test)
4449
cmd="_cliname_test"
4550
cur=$(( cur + opts_cur + 1 ))
@@ -120,7 +125,7 @@ _cliname() {
120125
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
121126
return 0
122127
elif [ $cur -eq $COMP_CWORD ] ; then
123-
opts="list ls cd scripts test"
128+
opts="list ls cd scripts git test"
124129
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
125130
return 0
126131
fi
@@ -261,6 +266,18 @@ _cliname() {
261266
return 0
262267
;;
263268

269+
_cliname_git)
270+
cmd_cur=$cur
271+
while [ $cur -lt $COMP_CWORD ] ; do
272+
cur=$(( cur + 1 ))
273+
done
274+
275+
opts=""
276+
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
277+
278+
return 0
279+
;;
280+
264281
_cliname_test)
265282
cmd_cur=$cur
266283
while [ $cur -lt $COMP_CWORD ] ; do

examples/v2/output.zsh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ _cliname() {
1717
{list,ls}'[list project files.]'
1818
cd'[cd project directory.]'
1919
scripts'[operate scripts.]'
20+
git'[git command.]'
2021
test'[test command.]'
2122
)
2223

@@ -78,6 +79,9 @@ _cliname() {
7879
esac
7980
;;
8081

82+
git)
83+
;;
84+
8185
test)
8286
local -a __test_subcmds
8387
__test_subcmds=(

0 commit comments

Comments
 (0)