cmdargparse 0.1.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.
@@ -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,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,175 @@
1
+ # cmdargparse
2
+
3
+ [![pypi](https://img.shields.io/pypi/v/cmdargparse?style=for-the-badge)](https://pypi.org/project/cmdargparse)
4
+ [![license](https://img.shields.io/pypi/l/cmdargparse?style=for-the-badge)](https://github.com/krnd/cmdargparse/blob/main/LICENSE)
5
+
6
+ The package provides a declarative way to define `cmd2` argument parsers.
7
+
8
+
9
+ ## Usage
10
+ <!----------------------------------------------------------------------------->
11
+
12
+ The core functionality is driven by three components:
13
+ * `cmdargument` for defining an argument parser
14
+ * `cmdfield` for declaring the arguments
15
+ * `cmdcommand` for attatching an argument parser to a command
16
+
17
+ ```py
18
+
19
+ class MyApplication(cmd2.Cmd):
20
+
21
+ @cmdargument
22
+ class _MyCommandArgs(cmdargument):
23
+ my_argument: str = cmdfield.argument(...)
24
+ my_option: str = cmdfield.option(...)
25
+ my_flag: str = cmdfield.flag(...)
26
+
27
+ @cmdcommand(_MyCommandArgs)
28
+ def do_mycommand(self, args: _MyCommandArgs) -> None:
29
+ args.pfields(self)
30
+
31
+ ```
32
+
33
+ *[Example Application](./launch/__main__.py)*
34
+
35
+
36
+ ### Arguments (positional)
37
+
38
+ * `choices` <br/>
39
+ The restricted set of values allowed for the argument.
40
+ * `choicesmap` <br/>
41
+ Additional values allowed for the argument that each map to the
42
+ specified value in the restricted set of values.
43
+ * `default` <br/>
44
+ The arguments default value if not specified.
45
+ <br/> (Providing a default value makes the argument optional.)
46
+ * `type` <br/>
47
+ Explicitly specifies the `type` to be used by `argparse`.
48
+ <br/> https://docs.python.org/3/library/argparse.html#type
49
+ * `help` <br/>
50
+ Brief description of the argument.
51
+ * `helpvar` <br/>
52
+ Reference name of the argument value in the help message.
53
+
54
+ #### Examples
55
+ ```py
56
+ arg: str | None = cmdfield.argument(
57
+ ("alpha", "beta"),
58
+ {
59
+ "a": "alpha",
60
+ "b": "beta",
61
+ },
62
+ default=None,
63
+ )
64
+ ```
65
+
66
+
67
+ ### Options (non-positional, value-bound)
68
+
69
+ * `decl` <br/>
70
+ Specifies the primary option declaration.
71
+ * `altdecl` <br/>
72
+ Specifies the alternative option declaration.
73
+ * `choices` <br/>
74
+ The restricted set of values allowed for the option.
75
+ * `choicesmap` <br/>
76
+ Additional values allowed for the option that each map to the
77
+ specified value in the restricted set of values.
78
+ * `default` <br/>
79
+ The options default value if not specified.
80
+ * `form` <br/>
81
+ Specifies the default form of the option declaration.
82
+ * `decls` <br/>
83
+ Explicitly specifies all option declarations.
84
+ * `more_decls` <br/>
85
+ Specifies additional option declarations.
86
+ * `type` <br/>
87
+ Explicitly specifies the `type` to be used by `argparse`.
88
+ <br/> https://docs.python.org/3/library/argparse.html#type
89
+ * `required` <br/>
90
+ Marks the option as required.
91
+ * `help` <br/>
92
+ Brief description of the option.
93
+ * `helpvar` <br/>
94
+ Reference name of the option value in the help message.
95
+
96
+ #### Examples
97
+ ```py
98
+ xx: str | None = cmdfield.option()
99
+ yy: int | None = cmdfield.option([1, 2, 3])
100
+ zz: str = cmdfield.option(required=True)
101
+ ```
102
+
103
+ #### Declarations
104
+ ```py
105
+ aa: str | None = cmdfield.option() # -aa OR --aa
106
+ bb: str | None = cmdfield.option("--cc") # -bb, --cc OR --cc
107
+ dd: str | None = cmdfield.option("-ee") # -ee OR --dd, -ee
108
+ ff: str | None = cmdfield.option("--gg", "-ii") # --gg, -ii
109
+ jj: str | None = cmdfield.option(form="--") # --jj
110
+ kk: str | None = cmdfield.option(form="-") # -kk
111
+ mm: str | None = cmdfield.option(decls="--mm") # --mm
112
+ nn: str | None = cmdfield.option(decls="-oo") # -oo
113
+ pp: str | None = cmdfield.option(decls=("--rr", "-ss")) # --rr, -ss
114
+ tt: str | None = cmdfield.option(more_decls="-uu") # -tt, -uu
115
+ ```
116
+
117
+
118
+ ### Flags (non-positional, non-value)
119
+
120
+ * `decl` <br/>
121
+ Specifies the primary flag declaration.
122
+ * `altdecl` <br/>
123
+ Specifies the alternative flag declaration.
124
+ * `invert` <br/>
125
+ Sets the value `False` instead of `True` if the flag is specified.
126
+ * `const` <br/>
127
+ The value to set if the flag is specified.
128
+ * `form` <br/>
129
+ Specifies the default form of the flag declaration.
130
+ * `decls` <br/>
131
+ Explicitly specifies all flag declarations.
132
+ * `more_decls` <br/>
133
+ Specifies additional flag declarations.
134
+ * `type` <br/>
135
+ Explicitly specifies the type to be used by argparse.
136
+ <br/> https://docs.python.org/3/library/argparse.html#type
137
+ * `help` <br/>
138
+ Brief description of the flag.
139
+
140
+ #### Examples
141
+ ```py
142
+ vv: bool = cmdfield.flag()
143
+ ww: bool = cmdfield.flag(invert=True)
144
+ xx: float | None = cmdfield.flag(const=1.23)
145
+ yy: str | None = cmdfield.flag(const="text")
146
+ zz: Literal["ltr"] | None = cmdfield.flag(const="ltr")
147
+ ```
148
+
149
+ #### Declarations
150
+ ```py
151
+ aa: bool = cmdfield.flag() # -aa OR --aa
152
+ bb: bool = cmdfield.flag("--cc") # -bb, --cc OR --cc
153
+ dd: bool = cmdfield.flag("-ee") # -ee OR --dd, -ee
154
+ ff: bool = cmdfield.flag("--gg", "-ii") # --gg, -ii
155
+ jj: bool = cmdfield.flag(form="--") # --jj
156
+ kk: bool = cmdfield.flag(form="-") # -kk
157
+ mm: bool = cmdfield.flag(decls="--mm") # --mm
158
+ nn: bool = cmdfield.flag(decls="-oo") # -oo
159
+ pp: bool = cmdfield.flag(decls=("--rr", "-ss")) # --rr, -ss
160
+ tt: bool = cmdfield.flag(more_decls="-uu") # -tt, -uu
161
+ ```
162
+
163
+
164
+ ## Q&A
165
+ <!----------------------------------------------------------------------------->
166
+
167
+
168
+ #### Changing the default declaration form
169
+
170
+ The `cmdargument` decorator accepts a `form` argument to select the default
171
+ declaration form for an argument parser, which can be either *-name* (default)
172
+ or *--name*.
173
+
174
+ It is also possible to change the global default declaration form by using the
175
+ `cmdargument.default_form` function.
@@ -0,0 +1,10 @@
1
+ import cmd2 as _cmd2
2
+
3
+ from .argument import cmdargument
4
+ from .command import cmdcommand
5
+ from .field import cmdfield
6
+ from .namespace import Namespace
7
+ from .parser import ArgumentParser
8
+
9
+
10
+ _cmd2.set_default_argument_parser_type(ArgumentParser)
@@ -0,0 +1,99 @@
1
+ import dataclasses
2
+ import functools
3
+ from typing import (
4
+ TYPE_CHECKING,
5
+ Callable,
6
+ ClassVar,
7
+ Type,
8
+ TypeVar,
9
+ dataclass_transform,
10
+ overload,
11
+ )
12
+
13
+ from .field import _DeclFormSpecifier
14
+ from .namespace import Namespace
15
+
16
+
17
+ # ################################ TYPING ######################################
18
+
19
+
20
+ T = TypeVar("T")
21
+
22
+
23
+ # ################################ METACLASS ###################################
24
+
25
+
26
+ class _cmdargument(type):
27
+
28
+ # ################## DEFAULTS ##########################
29
+
30
+ _cmdargparse_default_form: ClassVar = "-"
31
+
32
+ @classmethod
33
+ def default_form(cls, form: _DeclFormSpecifier, /) -> None:
34
+ """Sets the global default declaration form specifier."""
35
+
36
+ cls._cmdargparse_default_form = form
37
+
38
+ # ################## DECORATOR #########################
39
+
40
+ @overload
41
+ @dataclass_transform(eq_default=False)
42
+ def __call__(
43
+ cls,
44
+ _cls: Type[T],
45
+ /,
46
+ ) -> Type[T]: ...
47
+
48
+ @overload
49
+ @dataclass_transform(eq_default=False)
50
+ def __call__(
51
+ cls,
52
+ /,
53
+ *,
54
+ form: _DeclFormSpecifier,
55
+ ) -> Callable[[Type[T]], Type[T]]: ...
56
+
57
+ @dataclass_transform(eq_default=False)
58
+ def __call__(
59
+ cls,
60
+ _cls: Type[T] | None = None,
61
+ /,
62
+ *,
63
+ form: _DeclFormSpecifier | None = None,
64
+ ) -> Callable[[Type[T]], Type[T]] | Type[T]:
65
+ """
66
+ Decorate as command argument object.
67
+
68
+ :param form:
69
+ The default declaration form specifier.
70
+ """
71
+
72
+ if _cls is None:
73
+ return functools.partial(
74
+ cls.__call__,
75
+ default=form,
76
+ ) # type: ignore
77
+
78
+ setattr(
79
+ _cls,
80
+ "_cmdargparse_default_form",
81
+ form or cls._cmdargparse_default_form,
82
+ )
83
+
84
+ return dataclasses.dataclass(
85
+ init=False,
86
+ repr=False,
87
+ eq=False,
88
+ )(_cls)
89
+
90
+
91
+ # ################################ CLASS #######################################
92
+
93
+
94
+ class cmdargument(
95
+ # Allow the type checker to find the namespace attributes.
96
+ (Namespace if TYPE_CHECKING else object),
97
+ metaclass=_cmdargument,
98
+ ):
99
+ pass