cmdargparse 0.1.0__py3-none-any.whl

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.
@@ -0,0 +1,37 @@
1
+ import argparse
2
+
3
+ import cmd2
4
+
5
+
6
+ # ################################ NAMESPACE ###################################
7
+
8
+
9
+ class Namespace(argparse.Namespace):
10
+
11
+ def pfields(self, cmd: cmd2.Cmd, /) -> None:
12
+ """Prints all fields using the feedback output function."""
13
+
14
+ fields = tuple(
15
+ attr
16
+ for attr in self.__dict__
17
+ if (
18
+ # Skip all internalized attributes.
19
+ not attr.startswith("_")
20
+ # Skip all cmd2-related attributes.
21
+ and not attr.startswith("cmd2")
22
+ )
23
+ )
24
+
25
+ maxlen = max(
26
+ len(attr)
27
+ for attr in fields
28
+ # <format-break>
29
+ )
30
+
31
+ cmd.pfeedback("[Namespace]")
32
+ for name in fields:
33
+ cmd.pfeedback(
34
+ f" {str.ljust(name, maxlen)!s} : {self.__dict__[name]!r}"
35
+ )
36
+ if not fields:
37
+ cmd.pfeedback(" (no fields)")
cmdargparse/parser.py ADDED
@@ -0,0 +1,40 @@
1
+ from typing import Annotated, Any
2
+
3
+ import cmd2
4
+
5
+
6
+ # ################################ PARSER ######################################
7
+
8
+
9
+ class ArgumentParser(cmd2.Cmd2ArgumentParser):
10
+
11
+ # Registers a custom argparse argument parameter.
12
+ # https://cmd2.readthedocs.io/en/stable/api/argparse_custom/#cmd2.argparse_custom.register_argparse_argument_parameter
13
+ # ```
14
+ # def register_argparse_argument_parameter(
15
+ # param_name: str,
16
+ # param_type: Optional[Type[Any]],
17
+ # ) -> None: ...
18
+ # ```
19
+
20
+ def __init__(
21
+ self,
22
+ *args: Annotated[Any, "passthrough"],
23
+ **kwargs: Annotated[Any, "passthrough"],
24
+ ) -> None:
25
+ super(cmd2.Cmd2ArgumentParser, self).__init__(*args, **kwargs)
26
+
27
+ from .actions import StoreAction
28
+
29
+ self.register("action", None, StoreAction)
30
+ self.register("action", "store", StoreAction)
31
+ # self.register('action', 'store_const', _StoreConstAction)
32
+ # self.register('action', 'store_true', _StoreTrueAction)
33
+ # self.register('action', 'store_false', _StoreFalseAction)
34
+ # self.register('action', 'append', _AppendAction)
35
+ # self.register('action', 'append_const', _AppendConstAction)
36
+ # self.register('action', 'count', _CountAction)
37
+ # self.register('action', 'help', _HelpAction)
38
+ # self.register('action', 'version', _VersionAction)
39
+ # self.register('action', 'parsers', _SubParsersAction)
40
+ # self.register('action', 'extend', _ExtendAction)
cmdargparse/unset.py ADDED
@@ -0,0 +1,79 @@
1
+ from typing import Any, Final, TypeAlias, TypeIs, TypeVar
2
+
3
+
4
+ # ################################ PACKAGE #####################################
5
+
6
+
7
+ __sname__ = "unset"
8
+ __version__ = "1.3"
9
+ __description__ = ...
10
+
11
+ __requires__ = ()
12
+
13
+
14
+ __all__ = (
15
+ # fmt: off
16
+ "UnsetType", "UNSET", "Unset",
17
+ "isunset", "on_unset",
18
+ # fmt: on
19
+ )
20
+
21
+
22
+ # ################################ TYPING ######################################
23
+
24
+
25
+ T = TypeVar("T")
26
+
27
+ TONCE: TypeAlias = Any
28
+
29
+
30
+ # ################################ UNSET #######################################
31
+
32
+
33
+ class _UnsetType(type):
34
+
35
+ def __invert__(self) -> "UnsetType":
36
+ """Returns the unset value."""
37
+ global UNSET
38
+ return UNSET
39
+
40
+
41
+ class UnsetType(object, metaclass=_UnsetType):
42
+ """Type of the unset value."""
43
+
44
+ pass
45
+
46
+
47
+ UNSET: Final = UnsetType()
48
+ """Value to use if unset."""
49
+
50
+
51
+ # ###################### CONVENIENCE #######################
52
+
53
+
54
+ Unset: TypeAlias = UnsetType
55
+ """
56
+ The `Unset` attribute is a convenience alias to simplify the usage of the
57
+ `UnsetType` and `UNSET` attributes.
58
+
59
+ It can be used as follows:
60
+ ```
61
+ def function(arg: Type | Unset = ~Unset):
62
+ arg = arg if not isunset(arg) else VALUE
63
+ arg = on_unset(arg, VALUE)
64
+ ```
65
+ """
66
+
67
+
68
+ # ################################ FUNCTIONS ###################################
69
+
70
+
71
+ def isunset(obj: Any, /) -> TypeIs[UnsetType]:
72
+ """Returns true if the object is unset."""
73
+ return obj is UNSET
74
+
75
+
76
+ def on_unset(obj: T | Unset, value: TONCE, /) -> T | TONCE:
77
+ """Returns either the object itself or the specified value if the object is
78
+ unset."""
79
+ return value if obj is UNSET else obj
@@ -0,0 +1,209 @@
1
+ Metadata-Version: 2.4
2
+ Name: cmdargparse
3
+ Version: 0.1.0
4
+ Summary: A declarative way to define `cmd2` argument parsers.
5
+ Home-page: https://github.com/krnd/cmdargparse
6
+ Author: Kilian Kaiping (krnd)
7
+ License: MIT
8
+ Keywords: CLI,cmd,command,interactive,prompt,Python
9
+ Classifier: Development Status :: 3 - Alpha
10
+ Classifier: Environment :: Console
11
+ Classifier: Environment :: Plugins
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Intended Audience :: System Administrators
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Programming Language :: Python :: 3 :: Only
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
19
+ Requires-Python: >=3.13
20
+ Description-Content-Type: text/markdown
21
+ License-File: LICENSE
22
+ Requires-Dist: cmd2~=2.7
23
+ Dynamic: author
24
+ Dynamic: classifier
25
+ Dynamic: description
26
+ Dynamic: description-content-type
27
+ Dynamic: home-page
28
+ Dynamic: keywords
29
+ Dynamic: license
30
+ Dynamic: license-file
31
+ Dynamic: requires-dist
32
+ Dynamic: requires-python
33
+ Dynamic: summary
34
+
35
+ # cmdargparse
36
+
37
+ [![pypi](https://img.shields.io/pypi/v/cmdargparse?style=for-the-badge)](https://pypi.org/project/cmdargparse)
38
+ [![license](https://img.shields.io/pypi/l/cmdargparse?style=for-the-badge)](https://github.com/krnd/cmdargparse/blob/main/LICENSE)
39
+
40
+ The package provides a declarative way to define `cmd2` argument parsers.
41
+
42
+
43
+ ## Usage
44
+ <!----------------------------------------------------------------------------->
45
+
46
+ The core functionality is driven by three components:
47
+ * `cmdargument` for defining an argument parser
48
+ * `cmdfield` for declaring the arguments
49
+ * `cmdcommand` for attatching an argument parser to a command
50
+
51
+ ```py
52
+
53
+ class MyApplication(cmd2.Cmd):
54
+
55
+ @cmdargument
56
+ class _MyCommandArgs(cmdargument):
57
+ my_argument: str = cmdfield.argument(...)
58
+ my_option: str = cmdfield.option(...)
59
+ my_flag: str = cmdfield.flag(...)
60
+
61
+ @cmdcommand(_MyCommandArgs)
62
+ def do_mycommand(self, args: _MyCommandArgs) -> None:
63
+ args.pfields(self)
64
+
65
+ ```
66
+
67
+ *[Example Application](./launch/__main__.py)*
68
+
69
+
70
+ ### Arguments (positional)
71
+
72
+ * `choices` <br/>
73
+ The restricted set of values allowed for the argument.
74
+ * `choicesmap` <br/>
75
+ Additional values allowed for the argument that each map to the
76
+ specified value in the restricted set of values.
77
+ * `default` <br/>
78
+ The arguments default value if not specified.
79
+ <br/> (Providing a default value makes the argument optional.)
80
+ * `type` <br/>
81
+ Explicitly specifies the `type` to be used by `argparse`.
82
+ <br/> https://docs.python.org/3/library/argparse.html#type
83
+ * `help` <br/>
84
+ Brief description of the argument.
85
+ * `helpvar` <br/>
86
+ Reference name of the argument value in the help message.
87
+
88
+ #### Examples
89
+ ```py
90
+ arg: str | None = cmdfield.argument(
91
+ ("alpha", "beta"),
92
+ {
93
+ "a": "alpha",
94
+ "b": "beta",
95
+ },
96
+ default=None,
97
+ )
98
+ ```
99
+
100
+
101
+ ### Options (non-positional, value-bound)
102
+
103
+ * `decl` <br/>
104
+ Specifies the primary option declaration.
105
+ * `altdecl` <br/>
106
+ Specifies the alternative option declaration.
107
+ * `choices` <br/>
108
+ The restricted set of values allowed for the option.
109
+ * `choicesmap` <br/>
110
+ Additional values allowed for the option that each map to the
111
+ specified value in the restricted set of values.
112
+ * `default` <br/>
113
+ The options default value if not specified.
114
+ * `form` <br/>
115
+ Specifies the default form of the option declaration.
116
+ * `decls` <br/>
117
+ Explicitly specifies all option declarations.
118
+ * `more_decls` <br/>
119
+ Specifies additional option declarations.
120
+ * `type` <br/>
121
+ Explicitly specifies the `type` to be used by `argparse`.
122
+ <br/> https://docs.python.org/3/library/argparse.html#type
123
+ * `required` <br/>
124
+ Marks the option as required.
125
+ * `help` <br/>
126
+ Brief description of the option.
127
+ * `helpvar` <br/>
128
+ Reference name of the option value in the help message.
129
+
130
+ #### Examples
131
+ ```py
132
+ xx: str | None = cmdfield.option()
133
+ yy: int | None = cmdfield.option([1, 2, 3])
134
+ zz: str = cmdfield.option(required=True)
135
+ ```
136
+
137
+ #### Declarations
138
+ ```py
139
+ aa: str | None = cmdfield.option() # -aa OR --aa
140
+ bb: str | None = cmdfield.option("--cc") # -bb, --cc OR --cc
141
+ dd: str | None = cmdfield.option("-ee") # -ee OR --dd, -ee
142
+ ff: str | None = cmdfield.option("--gg", "-ii") # --gg, -ii
143
+ jj: str | None = cmdfield.option(form="--") # --jj
144
+ kk: str | None = cmdfield.option(form="-") # -kk
145
+ mm: str | None = cmdfield.option(decls="--mm") # --mm
146
+ nn: str | None = cmdfield.option(decls="-oo") # -oo
147
+ pp: str | None = cmdfield.option(decls=("--rr", "-ss")) # --rr, -ss
148
+ tt: str | None = cmdfield.option(more_decls="-uu") # -tt, -uu
149
+ ```
150
+
151
+
152
+ ### Flags (non-positional, non-value)
153
+
154
+ * `decl` <br/>
155
+ Specifies the primary flag declaration.
156
+ * `altdecl` <br/>
157
+ Specifies the alternative flag declaration.
158
+ * `invert` <br/>
159
+ Sets the value `False` instead of `True` if the flag is specified.
160
+ * `const` <br/>
161
+ The value to set if the flag is specified.
162
+ * `form` <br/>
163
+ Specifies the default form of the flag declaration.
164
+ * `decls` <br/>
165
+ Explicitly specifies all flag declarations.
166
+ * `more_decls` <br/>
167
+ Specifies additional flag declarations.
168
+ * `type` <br/>
169
+ Explicitly specifies the type to be used by argparse.
170
+ <br/> https://docs.python.org/3/library/argparse.html#type
171
+ * `help` <br/>
172
+ Brief description of the flag.
173
+
174
+ #### Examples
175
+ ```py
176
+ vv: bool = cmdfield.flag()
177
+ ww: bool = cmdfield.flag(invert=True)
178
+ xx: float | None = cmdfield.flag(const=1.23)
179
+ yy: str | None = cmdfield.flag(const="text")
180
+ zz: Literal["ltr"] | None = cmdfield.flag(const="ltr")
181
+ ```
182
+
183
+ #### Declarations
184
+ ```py
185
+ aa: bool = cmdfield.flag() # -aa OR --aa
186
+ bb: bool = cmdfield.flag("--cc") # -bb, --cc OR --cc
187
+ dd: bool = cmdfield.flag("-ee") # -ee OR --dd, -ee
188
+ ff: bool = cmdfield.flag("--gg", "-ii") # --gg, -ii
189
+ jj: bool = cmdfield.flag(form="--") # --jj
190
+ kk: bool = cmdfield.flag(form="-") # -kk
191
+ mm: bool = cmdfield.flag(decls="--mm") # --mm
192
+ nn: bool = cmdfield.flag(decls="-oo") # -oo
193
+ pp: bool = cmdfield.flag(decls=("--rr", "-ss")) # --rr, -ss
194
+ tt: bool = cmdfield.flag(more_decls="-uu") # -tt, -uu
195
+ ```
196
+
197
+
198
+ ## Q&A
199
+ <!----------------------------------------------------------------------------->
200
+
201
+
202
+ #### Changing the default declaration form
203
+
204
+ The `cmdargument` decorator accepts a `form` argument to select the default
205
+ declaration form for an argument parser, which can be either *-name* (default)
206
+ or *--name*.
207
+
208
+ It is also possible to change the global default declaration form by using the
209
+ `cmdargument.default_form` function.
@@ -0,0 +1,13 @@
1
+ cmdargparse/__init__.py,sha256=weTQyPCVDyw9PWtStk9-nwvgO6qJinGC2dX2aXjlHUs,251
2
+ cmdargparse/argument.py,sha256=vx658i7Qgb6m_JhMddNeIuH47xe8OyfGiyGxziWDn_0,2294
3
+ cmdargparse/command.py,sha256=RhSla4WddgfGXhy50CfYVu_ic-rkji7cZOk2EaO800Y,5926
4
+ cmdargparse/custom.py,sha256=3uJ5mdsJGhkmsOU1IYO69h4sEHgyhDh40lYm_EdqSus,3124
5
+ cmdargparse/field.py,sha256=SMcPRYZ3XJeHCT-LPQF8P4G91fMPXldm2_Hb0irpkG8,21203
6
+ cmdargparse/namespace.py,sha256=SLeaH-49fm3qv8MNZo0HzjFHnr1SsRzLk_00pPn5PXo,984
7
+ cmdargparse/parser.py,sha256=UYFHXrQcOokuqxRnoP5iVvwbE6pMQQVIcGXRLInNrgg,1567
8
+ cmdargparse/unset.py,sha256=QWNkd6t7c3-8K7EPnZJIt0QAPcVKg4rRqiNVPjVCdB8,1687
9
+ cmdargparse-0.1.0.dist-info/licenses/LICENSE,sha256=ZbjvSbpbnaBM1xeQH0Ox4GsHxGMA7KhWUVsO3owGe94,1099
10
+ cmdargparse-0.1.0.dist-info/METADATA,sha256=ebZ06w12jHGyyAWOsKm4yXH-IN_pPkQM0KGiLXXn_GY,7002
11
+ cmdargparse-0.1.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
12
+ cmdargparse-0.1.0.dist-info/top_level.txt,sha256=afbiyImfO4prsVte-5tYouF4u6dLVAfj7W-JfDtsz7E,12
13
+ cmdargparse-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.9.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Kilian Kaiping (krnd)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ cmdargparse