Skip to content

Commit 1a36d7b

Browse files
committed
Add: HasAlias mixin.
1 parent cc51aa6 commit 1a36d7b

File tree

10 files changed

+71
-96
lines changed

10 files changed

+71
-96
lines changed

cmdcomp/v1/app_info.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
1-
from functools import cached_property
21
from typing import Annotated
32

43
from pydantic import Field
54

65
from cmdcomp.model import Model
6+
from cmdcomp.v2.mixin.has_alias import HasAlias
77

88

9-
class V1AppInfo(Model):
9+
class V1AppInfo(HasAlias, Model):
1010
"""your cli app info."""
1111

1212
name: Annotated[str, Field(title="your cli app name.")]
1313

1414
alias: Annotated[
15-
str | list[str],
15+
str | list[str] | None,
1616
Field(
1717
title="alias of the cli app name.",
18-
default_factory=list,
18+
default=None,
1919
),
2020
]
21-
22-
@cached_property
23-
def aliases(self) -> list[str]:
24-
if isinstance(self.alias, str):
25-
return [self.alias]
26-
else:
27-
return self.alias

cmdcomp/v1/command/__init__.py

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from functools import cached_property, reduce
1+
from functools import reduce
22
from operator import add
33
from typing import Annotated, NewType, OrderedDict
44

@@ -9,6 +9,7 @@
99
from cmdcomp.v1.command.option import V1OptionType, V1SpecificOptions, V1StrOption
1010
from cmdcomp.v1.command.option.command_option import V1CommandOption
1111
from cmdcomp.v1.command.option.file_option import V1FileOption
12+
from cmdcomp.v2.mixin.has_alias import HasAlias
1213

1314
V1SubcommandName = NewType("V1SubcommandName", str)
1415

@@ -18,16 +19,16 @@
1819
V1Completions = V1Candidates | dict[str, "V1Completions"] # type: ignore
1920

2021

21-
class V1SubCommandsCommand(Model):
22+
class V1SubCommandsCommand(HasAlias, Model):
2223
"""A command that can specify a subcommand."""
2324

2425
model_config = ConfigDict(arbitrary_types_allowed=True)
2526

2627
alias: Annotated[
27-
str | list[str],
28+
str | list[str] | None,
2829
Field(
2930
title="alias of the command.",
30-
default_factory=list,
31+
default=None,
3132
),
3233
]
3334

@@ -44,24 +45,17 @@ class V1SubCommandsCommand(Model):
4445
default_factory=OrderedDict,
4546
)
4647

47-
@cached_property
48-
def aliases(self) -> list[str]:
49-
if isinstance(self.alias, str):
50-
return [self.alias]
51-
else:
52-
return self.alias
53-
5448

55-
class V1SpecificOptionsCommand(Model):
49+
class V1SpecificOptionsCommand(HasAlias, Model):
5650
"""A command that can specify options."""
5751

5852
model_config = ConfigDict(arbitrary_types_allowed=True)
5953

6054
alias: Annotated[
61-
str | list[str],
55+
str | list[str] | None,
6256
Field(
6357
title="alias of the command.",
64-
default_factory=list,
58+
default=None,
6559
),
6660
]
6761

@@ -72,13 +66,6 @@ class V1SpecificOptionsCommand(Model):
7266
),
7367
]
7468

75-
@cached_property
76-
def aliases(self) -> list[str]:
77-
if isinstance(self.alias, str):
78-
return [self.alias]
79-
else:
80-
return self.alias
81-
8269

8370
V1Command = V1SubCommandsCommand | V1SpecificOptionsCommand
8471

cmdcomp/v2/app_info.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
1-
from functools import cached_property
21
from typing import Annotated
32

43
from pydantic import Field
54

65
from cmdcomp.model import Model
6+
from cmdcomp.v2.mixin.has_alias import HasAlias
77

88

9-
class V2AppInfo(Model):
9+
class V2AppInfo(HasAlias, Model):
1010
"""your cli app info."""
1111

1212
name: Annotated[str, Field(title="your cli app name.")]
1313

1414
alias: Annotated[
15-
str | list[str],
15+
str | list[str] | None,
1616
Field(
1717
title="alias of the cli app name.",
18-
default_factory=list,
18+
default=None,
1919
),
2020
]
21-
22-
@cached_property
23-
def aliases(self) -> list[str]:
24-
if isinstance(self.alias, str):
25-
return [self.alias]
26-
else:
27-
return self.alias

cmdcomp/v2/command/__init__.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
V2SelectArgument,
1111
V2ValueArgument,
1212
)
13+
from cmdcomp.v2.mixin.has_alias import HasAlias
1314

1415
from .argument import V2Argument
1516

@@ -19,7 +20,7 @@
1920
_InputArgument = str | list[str] | V2Argument
2021

2122

22-
class _V2BaseCommand(Model, metaclass=ABCMeta):
23+
class _V2BaseCommand(HasAlias, Model, metaclass=ABCMeta):
2324
model_config = ConfigDict(arbitrary_types_allowed=True)
2425

2526
description: str | None = Field(
@@ -28,20 +29,13 @@ class _V2BaseCommand(Model, metaclass=ABCMeta):
2829
)
2930

3031
alias: Annotated[
31-
str | list[str],
32+
str | list[str] | None,
3233
Field(
3334
title="alias of the command.",
34-
default_factory=list,
35+
default=None,
3536
),
3637
]
3738

38-
@cached_property
39-
def aliases(self) -> list[str]:
40-
if isinstance(self.alias, str):
41-
return [self.alias]
42-
else:
43-
return self.alias
44-
4539
@cached_property
4640
def subcommand_names_with_alias(self) -> list[SubcommandName]:
4741
result: list[SubcommandName] = []
Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from functools import cached_property
21
from typing import Annotated, Literal
32

43
from pydantic import Field
54

65
from cmdcomp.model import Model
6+
from cmdcomp.v2.mixin.has_alias import HasAlias
77

88

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

1212
type: Literal["command"]
@@ -25,12 +25,3 @@ class V2CommandArgument(Model):
2525
str,
2626
Field(title="command to execute."),
2727
]
28-
29-
@cached_property
30-
def aliases(self) -> list[str]:
31-
if isinstance(self.alias, str):
32-
return [self.alias]
33-
elif isinstance(self.alias, list):
34-
return self.alias
35-
else:
36-
return []
Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from functools import cached_property
21
from typing import Literal
32

43
from pydantic import Field
54

65
from cmdcomp.model import Model
6+
from cmdcomp.v2.mixin.has_alias import HasAlias
77

88

9-
class V2FileArgument(Model):
9+
class V2FileArgument(HasAlias, Model):
1010
type: Literal["file"]
1111

1212
description: str | None = Field(
@@ -23,12 +23,3 @@ class V2FileArgument(Model):
2323
title="path of the directory from which to base filename completion.",
2424
default=None,
2525
)
26-
27-
@cached_property
28-
def aliases(self) -> list[str]:
29-
if isinstance(self.alias, str):
30-
return [self.alias]
31-
elif isinstance(self.alias, list):
32-
return self.alias
33-
else:
34-
return []
Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from functools import cached_property
21
from typing import Literal
32

43
from pydantic import Field
54

65
from cmdcomp.model import Model
6+
from cmdcomp.v2.mixin.has_alias import HasAlias
77

88

9-
class V2FlagArgument(Model):
9+
class V2FlagArgument(HasAlias, Model):
1010
type: Literal["flag"]
1111

1212
description: str | None = Field(
@@ -18,12 +18,3 @@ class V2FlagArgument(Model):
1818
title="alias of the argument.",
1919
default=None,
2020
)
21-
22-
@cached_property
23-
def aliases(self) -> list[str]:
24-
if isinstance(self.alias, str):
25-
return [self.alias]
26-
elif isinstance(self.alias, list):
27-
return self.alias
28-
else:
29-
return []

cmdcomp/v2/command/argument/select_argument.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
from cmdcomp.exception import NeverReach
88
from cmdcomp.model import Model
99
from cmdcomp.v2.command.argument.value_argument import V2ValueArgument
10+
from cmdcomp.v2.mixin.has_alias import HasAlias
1011

1112

12-
class V2SelectArgument(Model):
13+
class V2SelectArgument(HasAlias, Model):
1314
type: Literal["select"]
1415

1516
description: str | None = Field(
@@ -50,12 +51,3 @@ def values(self) -> list[V2ValueArgument]:
5051

5152
case _:
5253
raise NeverReach(self.raw_values)
53-
54-
@cached_property
55-
def aliases(self) -> list[str]:
56-
if isinstance(self.alias, str):
57-
return [self.alias]
58-
elif isinstance(self.alias, list):
59-
return self.alias
60-
else:
61-
return []

cmdcomp/v2/mixin/has_alias.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from abc import ABCMeta
2+
from functools import cached_property
3+
4+
5+
class HasAlias(metaclass=ABCMeta):
6+
# @property
7+
# @abstractmethod
8+
# def alias(self) -> str | list[str] | None:
9+
# ...
10+
11+
@cached_property
12+
def aliases(self) -> list[str]:
13+
alias = self.alias # type: ignore
14+
if alias is None:
15+
return []
16+
elif isinstance(alias, str):
17+
return [alias]
18+
else:
19+
return alias

docs/config.schema.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@
1818
"type": "string"
1919
},
2020
"type": "array"
21+
},
22+
{
23+
"type": "null"
2124
}
2225
],
26+
"default": null,
2327
"title": "alias of the cli app name."
2428
}
2529
},
@@ -128,8 +132,12 @@
128132
"type": "string"
129133
},
130134
"type": "array"
135+
},
136+
{
137+
"type": "null"
131138
}
132139
],
140+
"default": null,
133141
"title": "alias of the command."
134142
},
135143
"options": {
@@ -164,8 +172,12 @@
164172
"type": "string"
165173
},
166174
"type": "array"
175+
},
176+
{
177+
"type": "null"
167178
}
168179
],
180+
"default": null,
169181
"title": "alias of the command."
170182
},
171183
"options": {
@@ -221,8 +233,12 @@
221233
"type": "string"
222234
},
223235
"type": "array"
236+
},
237+
{
238+
"type": "null"
224239
}
225240
],
241+
"default": null,
226242
"title": "alias of the cli app name."
227243
}
228244
},
@@ -452,8 +468,12 @@
452468
"type": "string"
453469
},
454470
"type": "array"
471+
},
472+
{
473+
"type": "null"
455474
}
456475
],
476+
"default": null,
457477
"title": "alias of the command."
458478
},
459479
"arguments": {
@@ -600,8 +620,12 @@
600620
"type": "string"
601621
},
602622
"type": "array"
623+
},
624+
{
625+
"type": "null"
603626
}
604627
],
628+
"default": null,
605629
"title": "alias of the command."
606630
},
607631
"arguments": {

0 commit comments

Comments
 (0)