cmdargparse 0.3.1__tar.gz → 0.5.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.5.0}/PKG-INFO +43 -1
  2. {cmdargparse-0.3.1 → cmdargparse-0.5.0}/README.md +42 -0
  3. {cmdargparse-0.3.1 → cmdargparse-0.5.0}/cmdargparse/command.py +55 -8
  4. {cmdargparse-0.3.1 → cmdargparse-0.5.0}/cmdargparse/namespace.py +13 -0
  5. {cmdargparse-0.3.1 → cmdargparse-0.5.0}/cmdargparse.egg-info/PKG-INFO +43 -1
  6. {cmdargparse-0.3.1 → cmdargparse-0.5.0}/setup.py +1 -1
  7. {cmdargparse-0.3.1 → cmdargparse-0.5.0}/LICENSE +0 -0
  8. {cmdargparse-0.3.1 → cmdargparse-0.5.0}/cmdargparse/__init__.py +0 -0
  9. {cmdargparse-0.3.1 → cmdargparse-0.5.0}/cmdargparse/actions/__init__.py +0 -0
  10. {cmdargparse-0.3.1 → cmdargparse-0.5.0}/cmdargparse/actions/store.py +0 -0
  11. {cmdargparse-0.3.1 → cmdargparse-0.5.0}/cmdargparse/argument.py +0 -0
  12. {cmdargparse-0.3.1 → cmdargparse-0.5.0}/cmdargparse/custom.py +0 -0
  13. {cmdargparse-0.3.1 → cmdargparse-0.5.0}/cmdargparse/field.py +0 -0
  14. {cmdargparse-0.3.1 → cmdargparse-0.5.0}/cmdargparse/parser.py +0 -0
  15. {cmdargparse-0.3.1 → cmdargparse-0.5.0}/cmdargparse/unset.py +0 -0
  16. {cmdargparse-0.3.1 → cmdargparse-0.5.0}/cmdargparse.egg-info/SOURCES.txt +0 -0
  17. {cmdargparse-0.3.1 → cmdargparse-0.5.0}/cmdargparse.egg-info/dependency_links.txt +0 -0
  18. {cmdargparse-0.3.1 → cmdargparse-0.5.0}/cmdargparse.egg-info/requires.txt +0 -0
  19. {cmdargparse-0.3.1 → cmdargparse-0.5.0}/cmdargparse.egg-info/top_level.txt +0 -0
  20. {cmdargparse-0.3.1 → cmdargparse-0.5.0}/pyproject.toml +0 -0
  21. {cmdargparse-0.3.1 → cmdargparse-0.5.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.5.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,48 @@ 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
+ Subcommands can be called via `args.subcommand()` instead of
221
+ `args.cmd2_subcommand_func(args)`.
222
+
223
+ #### Arguments
224
+ Subcommands also get the arguments of their parent commands and the constants
225
+ of the root command.
226
+
227
+ Inheritance cannot be used to share options between subcommands.
228
+
229
+ #### Examples
230
+ ```py
231
+ @cmdcommand(_MyCommandArgs, subcommands=True)
232
+ def do_mycommand(self, args: _MyCommandArgs) -> None:
233
+ args.subcommand()
234
+
235
+ @cmdargument
236
+ class _MySubArgs(_MyCommandArgs): ...
237
+
238
+ @cmdcommand(_MySubArgs, subcommand_to="mycommand") # mycommand mysub
239
+ def mycommand_mysub(self, args: _MySubArgs) -> None: ...
240
+
241
+ @cmdcommand(_MySubArgs, subcommand_to=("mycommand", "my-sub")) # mycommand my-sub
242
+ def mycommand_my_sub(self, args: _MySubArgs) -> None: ...
243
+
244
+ @cmdcommand(_MyGroupArgs, subcommands=True, subcommand_to="mycommand") # mycommand mygroup
245
+ def mycommand_mygroup(self, args: _MyGroupArgs) -> None:
246
+ args.subcommand()
247
+
248
+ @cmdcommand(_MySubArgs, subcommand_to="mycommand mygroup") # mycommand mygroup mysub
249
+ def mycommand_mygroup_mysub(self, args: _MySubArgs) -> None: ...
250
+ ```
251
+
252
+
211
253
  ## Q&A
212
254
  <!----------------------------------------------------------------------------->
213
255
 
@@ -174,6 +174,48 @@ 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
+ Subcommands can be called via `args.subcommand()` instead of
187
+ `args.cmd2_subcommand_func(args)`.
188
+
189
+ #### Arguments
190
+ Subcommands also get the arguments of their parent commands and the constants
191
+ of the root command.
192
+
193
+ Inheritance cannot be used to share options between subcommands.
194
+
195
+ #### Examples
196
+ ```py
197
+ @cmdcommand(_MyCommandArgs, subcommands=True)
198
+ def do_mycommand(self, args: _MyCommandArgs) -> None:
199
+ args.subcommand()
200
+
201
+ @cmdargument
202
+ class _MySubArgs(_MyCommandArgs): ...
203
+
204
+ @cmdcommand(_MySubArgs, subcommand_to="mycommand") # mycommand mysub
205
+ def mycommand_mysub(self, args: _MySubArgs) -> None: ...
206
+
207
+ @cmdcommand(_MySubArgs, subcommand_to=("mycommand", "my-sub")) # mycommand my-sub
208
+ def mycommand_my_sub(self, args: _MySubArgs) -> None: ...
209
+
210
+ @cmdcommand(_MyGroupArgs, subcommands=True, subcommand_to="mycommand") # mycommand mygroup
211
+ def mycommand_mygroup(self, args: _MyGroupArgs) -> None:
212
+ args.subcommand()
213
+
214
+ @cmdcommand(_MySubArgs, subcommand_to="mycommand mygroup") # mycommand mygroup mysub
215
+ def mycommand_mygroup_mysub(self, args: _MySubArgs) -> None: ...
216
+ ```
217
+
218
+
177
219
  ## Q&A
178
220
  <!----------------------------------------------------------------------------->
179
221
 
@@ -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,55 @@ 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
+ if func.__doc__:
149
+ help = cmd2.utils.strip_doc_annotations(func.__doc__)
150
+ else:
151
+ help = None
152
+
153
+ return cmd2.as_subcommand_to(
154
+ command,
155
+ subcommand,
156
+ parser,
157
+ help=help,
158
+ )(func)
159
+
160
+ return _decorator
161
+
115
162
  @staticmethod
116
163
  def namespace_provider(
117
164
  argtype: Type[TARGUMENT],
@@ -1,4 +1,5 @@
1
1
  import argparse
2
+ from typing import Any
2
3
 
3
4
  import cmd2
4
5
 
@@ -8,6 +9,18 @@ import cmd2
8
9
 
9
10
  class Namespace(argparse.Namespace):
10
11
 
12
+ def subcommand(self) -> Any:
13
+ """Calls the selected subcommand."""
14
+
15
+ # The function is 'None' for commands without subcommands.
16
+ if self.cmd2_subcommand_func is None:
17
+ raise RuntimeError(
18
+ f"Command '{self.cmd2_statement.command}' does not have "
19
+ "subcommands"
20
+ )
21
+
22
+ return self.cmd2_subcommand_func(self)
23
+
11
24
  def pfields(self, cmd: cmd2.Cmd, /) -> None:
12
25
  """Prints all fields using the feedback output function."""
13
26
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cmdargparse
3
- Version: 0.3.1
3
+ Version: 0.5.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,48 @@ 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
+ Subcommands can be called via `args.subcommand()` instead of
221
+ `args.cmd2_subcommand_func(args)`.
222
+
223
+ #### Arguments
224
+ Subcommands also get the arguments of their parent commands and the constants
225
+ of the root command.
226
+
227
+ Inheritance cannot be used to share options between subcommands.
228
+
229
+ #### Examples
230
+ ```py
231
+ @cmdcommand(_MyCommandArgs, subcommands=True)
232
+ def do_mycommand(self, args: _MyCommandArgs) -> None:
233
+ args.subcommand()
234
+
235
+ @cmdargument
236
+ class _MySubArgs(_MyCommandArgs): ...
237
+
238
+ @cmdcommand(_MySubArgs, subcommand_to="mycommand") # mycommand mysub
239
+ def mycommand_mysub(self, args: _MySubArgs) -> None: ...
240
+
241
+ @cmdcommand(_MySubArgs, subcommand_to=("mycommand", "my-sub")) # mycommand my-sub
242
+ def mycommand_my_sub(self, args: _MySubArgs) -> None: ...
243
+
244
+ @cmdcommand(_MyGroupArgs, subcommands=True, subcommand_to="mycommand") # mycommand mygroup
245
+ def mycommand_mygroup(self, args: _MyGroupArgs) -> None:
246
+ args.subcommand()
247
+
248
+ @cmdcommand(_MySubArgs, subcommand_to="mycommand mygroup") # mycommand mygroup mysub
249
+ def mycommand_mygroup_mysub(self, args: _MySubArgs) -> None: ...
250
+ ```
251
+
252
+
211
253
  ## Q&A
212
254
  <!----------------------------------------------------------------------------->
213
255
 
@@ -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.5.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