cmdargparse 0.3.1__tar.gz → 0.4.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (21) hide show
  1. {cmdargparse-0.3.1 → cmdargparse-0.4.0}/PKG-INFO +40 -1
  2. {cmdargparse-0.3.1 → cmdargparse-0.4.0}/README.md +39 -0
  3. {cmdargparse-0.3.1 → cmdargparse-0.4.0}/cmdargparse/command.py +45 -8
  4. {cmdargparse-0.3.1 → cmdargparse-0.4.0}/cmdargparse.egg-info/PKG-INFO +40 -1
  5. {cmdargparse-0.3.1 → cmdargparse-0.4.0}/setup.py +1 -1
  6. {cmdargparse-0.3.1 → cmdargparse-0.4.0}/LICENSE +0 -0
  7. {cmdargparse-0.3.1 → cmdargparse-0.4.0}/cmdargparse/__init__.py +0 -0
  8. {cmdargparse-0.3.1 → cmdargparse-0.4.0}/cmdargparse/actions/__init__.py +0 -0
  9. {cmdargparse-0.3.1 → cmdargparse-0.4.0}/cmdargparse/actions/store.py +0 -0
  10. {cmdargparse-0.3.1 → cmdargparse-0.4.0}/cmdargparse/argument.py +0 -0
  11. {cmdargparse-0.3.1 → cmdargparse-0.4.0}/cmdargparse/custom.py +0 -0
  12. {cmdargparse-0.3.1 → cmdargparse-0.4.0}/cmdargparse/field.py +0 -0
  13. {cmdargparse-0.3.1 → cmdargparse-0.4.0}/cmdargparse/namespace.py +0 -0
  14. {cmdargparse-0.3.1 → cmdargparse-0.4.0}/cmdargparse/parser.py +0 -0
  15. {cmdargparse-0.3.1 → cmdargparse-0.4.0}/cmdargparse/unset.py +0 -0
  16. {cmdargparse-0.3.1 → cmdargparse-0.4.0}/cmdargparse.egg-info/SOURCES.txt +0 -0
  17. {cmdargparse-0.3.1 → cmdargparse-0.4.0}/cmdargparse.egg-info/dependency_links.txt +0 -0
  18. {cmdargparse-0.3.1 → cmdargparse-0.4.0}/cmdargparse.egg-info/requires.txt +0 -0
  19. {cmdargparse-0.3.1 → cmdargparse-0.4.0}/cmdargparse.egg-info/top_level.txt +0 -0
  20. {cmdargparse-0.3.1 → cmdargparse-0.4.0}/pyproject.toml +0 -0
  21. {cmdargparse-0.3.1 → cmdargparse-0.4.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cmdargparse
3
- Version: 0.3.1
3
+ Version: 0.4.0
4
4
  Summary: A declarative way to define `cmd2` argument parsers.
5
5
  Home-page: https://github.com/krnd/cmdargparse
6
6
  Author: Kilian Kaiping (krnd)
@@ -208,6 +208,45 @@ vv_ww: bool = cmdfield.flag() # -vv-ww OR --vv-ww
208
208
  ```
209
209
 
210
210
 
211
+ ### Subcommands
212
+
213
+ * `subcommands` <br/>
214
+ Allows subcommands to be attached to the command.
215
+ * `subcommand_to` <br/>
216
+ Attaches the command as a subcommand to the specified command.
217
+ <br/> *Named after the function `*_<name>`, or explicitly via
218
+ `(command, name)`.*
219
+
220
+ #### Arguments
221
+ Subcommands also get the arguments of their parent commands and the constants
222
+ of the root command.
223
+
224
+ Inheritance cannot be used to share options between subcommands.
225
+
226
+ #### Examples
227
+ ```py
228
+ @cmdcommand(_MyCommandArgs, subcommands=True)
229
+ def do_mycommand(self, args: _MyCommandArgs) -> None:
230
+ args.cmd2_subcommand_func(args)
231
+
232
+ @cmdargument
233
+ class _MySubArgs(_MyCommandArgs): ...
234
+
235
+ @cmdcommand(_MySubArgs, subcommand_to="mycommand") # mycommand mysub
236
+ def mycommand_mysub(self, args: _MySubArgs) -> None: ...
237
+
238
+ @cmdcommand(_MySubArgs, subcommand_to=("mycommand", "my-sub")) # mycommand my-sub
239
+ def mycommand_my_sub(self, args: _MySubArgs) -> None: ...
240
+
241
+ @cmdcommand(_MyGroupArgs, subcommands=True, subcommand_to="mycommand") # mycommand mygroup
242
+ def mycommand_mygroup(self, args: _MyGroupArgs) -> None:
243
+ args.cmd2_subcommand_func(args)
244
+
245
+ @cmdcommand(_MySubArgs, subcommand_to="mycommand mygroup") # mycommand mygroup mysub
246
+ def mycommand_mygroup_mysub(self, args: _MySubArgs) -> None: ...
247
+ ```
248
+
249
+
211
250
  ## Q&A
212
251
  <!----------------------------------------------------------------------------->
213
252
 
@@ -174,6 +174,45 @@ vv_ww: bool = cmdfield.flag() # -vv-ww OR --vv-ww
174
174
  ```
175
175
 
176
176
 
177
+ ### Subcommands
178
+
179
+ * `subcommands` <br/>
180
+ Allows subcommands to be attached to the command.
181
+ * `subcommand_to` <br/>
182
+ Attaches the command as a subcommand to the specified command.
183
+ <br/> *Named after the function `*_<name>`, or explicitly via
184
+ `(command, name)`.*
185
+
186
+ #### Arguments
187
+ Subcommands also get the arguments of their parent commands and the constants
188
+ of the root command.
189
+
190
+ Inheritance cannot be used to share options between subcommands.
191
+
192
+ #### Examples
193
+ ```py
194
+ @cmdcommand(_MyCommandArgs, subcommands=True)
195
+ def do_mycommand(self, args: _MyCommandArgs) -> None:
196
+ args.cmd2_subcommand_func(args)
197
+
198
+ @cmdargument
199
+ class _MySubArgs(_MyCommandArgs): ...
200
+
201
+ @cmdcommand(_MySubArgs, subcommand_to="mycommand") # mycommand mysub
202
+ def mycommand_mysub(self, args: _MySubArgs) -> None: ...
203
+
204
+ @cmdcommand(_MySubArgs, subcommand_to=("mycommand", "my-sub")) # mycommand my-sub
205
+ def mycommand_my_sub(self, args: _MySubArgs) -> None: ...
206
+
207
+ @cmdcommand(_MyGroupArgs, subcommands=True, subcommand_to="mycommand") # mycommand mygroup
208
+ def mycommand_mygroup(self, args: _MyGroupArgs) -> None:
209
+ args.cmd2_subcommand_func(args)
210
+
211
+ @cmdcommand(_MySubArgs, subcommand_to="mycommand mygroup") # mycommand mygroup mysub
212
+ def mycommand_mygroup_mysub(self, args: _MySubArgs) -> None: ...
213
+ ```
214
+
215
+
177
216
  ## Q&A
178
217
  <!----------------------------------------------------------------------------->
179
218
 
@@ -30,15 +30,15 @@ class _cmdcommand(type):
30
30
  cls,
31
31
  argtype: Type[TARGUMENT],
32
32
  /,
33
+ *,
34
+ subcommands: bool = False,
35
+ subcommand_to: str | Tuple[str, str] | None = None,
33
36
  ) -> Callable[
34
37
  [Callable[[TSELF, TARGUMENT], TRETURN]],
35
38
  Callable[[TSELF, TARGUMENT], TRETURN],
36
39
  ]:
37
40
  """Decorate as command function."""
38
41
 
39
- if "_cmdargparse_command_decorator" in argtype.__dict__:
40
- return argtype.__dict__["_cmdargparse_command_decorator"]
41
-
42
42
  parser = ArgumentParser()
43
43
 
44
44
  _dcfields = dataclasses.fields(
@@ -46,6 +46,17 @@ class _cmdcommand(type):
46
46
  )
47
47
  _fieldtypes = typing.get_type_hints(argtype, include_extras=True)
48
48
 
49
+ if subcommand_to is not None:
50
+ _inherited = {
51
+ field.name
52
+ for base in argtype.__bases__
53
+ if dataclasses.is_dataclass(base)
54
+ for field in dataclasses.fields(base)
55
+ }
56
+ _dcfields = tuple(
57
+ field for field in _dcfields if field.name not in _inherited
58
+ )
59
+
49
60
  default_form: _DeclFormSpecifier
50
61
  default_form = getattr(argtype, "_cmdargparse_default_form")
51
62
 
@@ -99,19 +110,45 @@ class _cmdcommand(type):
99
110
 
100
111
  parser.add_argument(*name_or_flags, **args)
101
112
 
102
- setattr(
103
- argtype,
104
- "_cmdargparse_command_decorator",
105
- _cmdargparse_command_decorator := cmd2.with_argparser(
113
+ if subcommands:
114
+ parser.add_subparsers(
115
+ title="subcommands",
116
+ metavar="SUBCOMMAND",
117
+ required=True,
118
+ )
119
+
120
+ _cmdargparse_command_decorator = (
121
+ cmd2.with_argparser(
106
122
  parser,
107
123
  ns_provider=cls.namespace_provider(argtype),
108
- ),
124
+ )
125
+ if subcommand_to is None
126
+ else cls.subcommand_decorator(parser, subcommand_to)
109
127
  )
110
128
 
111
129
  return _cmdargparse_command_decorator # type: ignore
112
130
 
113
131
  # ################## HELPERS ###########################
114
132
 
133
+ @staticmethod
134
+ def subcommand_decorator(
135
+ parser: ArgumentParser,
136
+ subcommand_to: Tuple[str, str] | str,
137
+ /,
138
+ ) -> Callable[[Callable[..., Any]], Callable[..., Any]]:
139
+ def _decorator(func: Callable[..., Any]) -> Callable[..., Any]:
140
+ if isinstance(subcommand_to, tuple):
141
+ (command, subcommand) = subcommand_to
142
+
143
+ else:
144
+ command = subcommand_to
145
+
146
+ (_, _, subcommand) = func.__name__.rpartition("_")
147
+
148
+ return cmd2.as_subcommand_to(command, subcommand, parser)(func)
149
+
150
+ return _decorator
151
+
115
152
  @staticmethod
116
153
  def namespace_provider(
117
154
  argtype: Type[TARGUMENT],
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cmdargparse
3
- Version: 0.3.1
3
+ Version: 0.4.0
4
4
  Summary: A declarative way to define `cmd2` argument parsers.
5
5
  Home-page: https://github.com/krnd/cmdargparse
6
6
  Author: Kilian Kaiping (krnd)
@@ -208,6 +208,45 @@ vv_ww: bool = cmdfield.flag() # -vv-ww OR --vv-ww
208
208
  ```
209
209
 
210
210
 
211
+ ### Subcommands
212
+
213
+ * `subcommands` <br/>
214
+ Allows subcommands to be attached to the command.
215
+ * `subcommand_to` <br/>
216
+ Attaches the command as a subcommand to the specified command.
217
+ <br/> *Named after the function `*_<name>`, or explicitly via
218
+ `(command, name)`.*
219
+
220
+ #### Arguments
221
+ Subcommands also get the arguments of their parent commands and the constants
222
+ of the root command.
223
+
224
+ Inheritance cannot be used to share options between subcommands.
225
+
226
+ #### Examples
227
+ ```py
228
+ @cmdcommand(_MyCommandArgs, subcommands=True)
229
+ def do_mycommand(self, args: _MyCommandArgs) -> None:
230
+ args.cmd2_subcommand_func(args)
231
+
232
+ @cmdargument
233
+ class _MySubArgs(_MyCommandArgs): ...
234
+
235
+ @cmdcommand(_MySubArgs, subcommand_to="mycommand") # mycommand mysub
236
+ def mycommand_mysub(self, args: _MySubArgs) -> None: ...
237
+
238
+ @cmdcommand(_MySubArgs, subcommand_to=("mycommand", "my-sub")) # mycommand my-sub
239
+ def mycommand_my_sub(self, args: _MySubArgs) -> None: ...
240
+
241
+ @cmdcommand(_MyGroupArgs, subcommands=True, subcommand_to="mycommand") # mycommand mygroup
242
+ def mycommand_mygroup(self, args: _MyGroupArgs) -> None:
243
+ args.cmd2_subcommand_func(args)
244
+
245
+ @cmdcommand(_MySubArgs, subcommand_to="mycommand mygroup") # mycommand mygroup mysub
246
+ def mycommand_mygroup_mysub(self, args: _MySubArgs) -> None: ...
247
+ ```
248
+
249
+
211
250
  ## Q&A
212
251
  <!----------------------------------------------------------------------------->
213
252
 
@@ -7,7 +7,7 @@ with open("README.md", "r", encoding="utf-8") as file:
7
7
 
8
8
  setup(
9
9
  name="cmdargparse",
10
- version="0.3.1",
10
+ version="0.4.0",
11
11
  description="A declarative way to define `cmd2` argument parsers.",
12
12
  long_description=README,
13
13
  long_description_content_type="text/markdown",
File without changes
File without changes
File without changes