cmdargparse 0.2.0__tar.gz → 0.3.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.2.0 → cmdargparse-0.3.0}/PKG-INFO +13 -1
  2. {cmdargparse-0.2.0 → cmdargparse-0.3.0}/README.md +12 -0
  3. {cmdargparse-0.2.0 → cmdargparse-0.3.0}/cmdargparse/command.py +16 -12
  4. {cmdargparse-0.2.0 → cmdargparse-0.3.0}/cmdargparse/field.py +12 -1
  5. {cmdargparse-0.2.0 → cmdargparse-0.3.0}/cmdargparse.egg-info/PKG-INFO +13 -1
  6. {cmdargparse-0.2.0 → cmdargparse-0.3.0}/setup.py +1 -1
  7. {cmdargparse-0.2.0 → cmdargparse-0.3.0}/LICENSE +0 -0
  8. {cmdargparse-0.2.0 → cmdargparse-0.3.0}/cmdargparse/__init__.py +0 -0
  9. {cmdargparse-0.2.0 → cmdargparse-0.3.0}/cmdargparse/actions/__init__.py +0 -0
  10. {cmdargparse-0.2.0 → cmdargparse-0.3.0}/cmdargparse/actions/store.py +0 -0
  11. {cmdargparse-0.2.0 → cmdargparse-0.3.0}/cmdargparse/argument.py +0 -0
  12. {cmdargparse-0.2.0 → cmdargparse-0.3.0}/cmdargparse/custom.py +0 -0
  13. {cmdargparse-0.2.0 → cmdargparse-0.3.0}/cmdargparse/namespace.py +0 -0
  14. {cmdargparse-0.2.0 → cmdargparse-0.3.0}/cmdargparse/parser.py +0 -0
  15. {cmdargparse-0.2.0 → cmdargparse-0.3.0}/cmdargparse/unset.py +0 -0
  16. {cmdargparse-0.2.0 → cmdargparse-0.3.0}/cmdargparse.egg-info/SOURCES.txt +0 -0
  17. {cmdargparse-0.2.0 → cmdargparse-0.3.0}/cmdargparse.egg-info/dependency_links.txt +0 -0
  18. {cmdargparse-0.2.0 → cmdargparse-0.3.0}/cmdargparse.egg-info/requires.txt +0 -0
  19. {cmdargparse-0.2.0 → cmdargparse-0.3.0}/cmdargparse.egg-info/top_level.txt +0 -0
  20. {cmdargparse-0.2.0 → cmdargparse-0.3.0}/pyproject.toml +0 -0
  21. {cmdargparse-0.2.0 → cmdargparse-0.3.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cmdargparse
3
- Version: 0.2.0
3
+ Version: 0.3.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)
@@ -76,6 +76,9 @@ class MyApplication(cmd2.Cmd):
76
76
  * `default` <br/>
77
77
  The arguments default value if not specified.
78
78
  <br/> (Providing a default value makes the argument optional.)
79
+ * `nargs` <br/>
80
+ The number of command-line values to be consumed.
81
+ <br/> https://docs.python.org/3/library/argparse.html#nargs
79
82
  * `type` <br/>
80
83
  Explicitly specifies the `type` to be used by `argparse`.
81
84
  <br/> https://docs.python.org/3/library/argparse.html#type
@@ -94,6 +97,8 @@ arg: str | None = cmdfield.argument(
94
97
  },
95
98
  default=None,
96
99
  )
100
+ args: list[str] = cmdfield.argument()
101
+ more: list[str] = cmdfield.argument(nargs="+")
97
102
  ```
98
103
 
99
104
 
@@ -116,6 +121,9 @@ arg: str | None = cmdfield.argument(
116
121
  Explicitly specifies all option declarations.
117
122
  * `more_decls` <br/>
118
123
  Specifies additional option declarations.
124
+ * `nargs` <br/>
125
+ The number of command-line values to be consumed.
126
+ <br/> https://docs.python.org/3/library/argparse.html#nargs
119
127
  * `type` <br/>
120
128
  Explicitly specifies the `type` to be used by `argparse`.
121
129
  <br/> https://docs.python.org/3/library/argparse.html#type
@@ -131,6 +139,8 @@ arg: str | None = cmdfield.argument(
131
139
  xx: str | None = cmdfield.option()
132
140
  yy: int | None = cmdfield.option([1, 2, 3])
133
141
  zz: str = cmdfield.option(required=True)
142
+ hh: list[str] | None = cmdfield.option(default=None)
143
+ qq: list[int] | None = cmdfield.option(nargs=2, default=None)
134
144
  ```
135
145
 
136
146
  #### Declarations
@@ -145,6 +155,7 @@ mm: str | None = cmdfield.option(decls="--mm") # --mm
145
155
  nn: str | None = cmdfield.option(decls="-oo") # -oo
146
156
  pp: str | None = cmdfield.option(decls=("--rr", "-ss")) # --rr, -ss
147
157
  tt: str | None = cmdfield.option(more_decls="-uu") # -tt, -uu
158
+ vv_ww: str | None = cmdfield.option() # -vv-ww OR --vv-ww
148
159
  ```
149
160
 
150
161
 
@@ -192,6 +203,7 @@ mm: bool = cmdfield.flag(decls="--mm") # --mm
192
203
  nn: bool = cmdfield.flag(decls="-oo") # -oo
193
204
  pp: bool = cmdfield.flag(decls=("--rr", "-ss")) # --rr, -ss
194
205
  tt: bool = cmdfield.flag(more_decls="-uu") # -tt, -uu
206
+ vv_ww: bool = cmdfield.flag() # -vv-ww OR --vv-ww
195
207
  ```
196
208
 
197
209
 
@@ -43,6 +43,9 @@ class MyApplication(cmd2.Cmd):
43
43
  * `default` <br/>
44
44
  The arguments default value if not specified.
45
45
  <br/> (Providing a default value makes the argument optional.)
46
+ * `nargs` <br/>
47
+ The number of command-line values to be consumed.
48
+ <br/> https://docs.python.org/3/library/argparse.html#nargs
46
49
  * `type` <br/>
47
50
  Explicitly specifies the `type` to be used by `argparse`.
48
51
  <br/> https://docs.python.org/3/library/argparse.html#type
@@ -61,6 +64,8 @@ arg: str | None = cmdfield.argument(
61
64
  },
62
65
  default=None,
63
66
  )
67
+ args: list[str] = cmdfield.argument()
68
+ more: list[str] = cmdfield.argument(nargs="+")
64
69
  ```
65
70
 
66
71
 
@@ -83,6 +88,9 @@ arg: str | None = cmdfield.argument(
83
88
  Explicitly specifies all option declarations.
84
89
  * `more_decls` <br/>
85
90
  Specifies additional option declarations.
91
+ * `nargs` <br/>
92
+ The number of command-line values to be consumed.
93
+ <br/> https://docs.python.org/3/library/argparse.html#nargs
86
94
  * `type` <br/>
87
95
  Explicitly specifies the `type` to be used by `argparse`.
88
96
  <br/> https://docs.python.org/3/library/argparse.html#type
@@ -98,6 +106,8 @@ arg: str | None = cmdfield.argument(
98
106
  xx: str | None = cmdfield.option()
99
107
  yy: int | None = cmdfield.option([1, 2, 3])
100
108
  zz: str = cmdfield.option(required=True)
109
+ hh: list[str] | None = cmdfield.option(default=None)
110
+ qq: list[int] | None = cmdfield.option(nargs=2, default=None)
101
111
  ```
102
112
 
103
113
  #### Declarations
@@ -112,6 +122,7 @@ mm: str | None = cmdfield.option(decls="--mm") # --mm
112
122
  nn: str | None = cmdfield.option(decls="-oo") # -oo
113
123
  pp: str | None = cmdfield.option(decls=("--rr", "-ss")) # --rr, -ss
114
124
  tt: str | None = cmdfield.option(more_decls="-uu") # -tt, -uu
125
+ vv_ww: str | None = cmdfield.option() # -vv-ww OR --vv-ww
115
126
  ```
116
127
 
117
128
 
@@ -159,6 +170,7 @@ mm: bool = cmdfield.flag(decls="--mm") # --mm
159
170
  nn: bool = cmdfield.flag(decls="-oo") # -oo
160
171
  pp: bool = cmdfield.flag(decls=("--rr", "-ss")) # --rr, -ss
161
172
  tt: bool = cmdfield.flag(more_decls="-uu") # -tt, -uu
173
+ vv_ww: bool = cmdfield.flag() # -vv-ww OR --vv-ww
162
174
  ```
163
175
 
164
176
 
@@ -36,14 +36,15 @@ class _cmdcommand(type):
36
36
  ]:
37
37
  """Decorate as command function."""
38
38
 
39
- if hasattr(argtype, "_cmdargparse_command_decorator"):
40
- return getattr(argtype, "_cmdargparse_command_decorator")
39
+ if "_cmdargparse_command_decorator" in argtype.__dict__:
40
+ return argtype.__dict__["_cmdargparse_command_decorator"]
41
41
 
42
42
  parser = ArgumentParser()
43
43
 
44
44
  _dcfields = dataclasses.fields(
45
45
  argtype # pyright: ignore[reportArgumentType]
46
46
  )
47
+ _fieldtypes = typing.get_type_hints(argtype, include_extras=True)
47
48
 
48
49
  default_form: _DeclFormSpecifier
49
50
  default_form = getattr(argtype, "_cmdargparse_default_form")
@@ -63,7 +64,7 @@ class _cmdcommand(type):
63
64
  )
64
65
 
65
66
  args: dict[str, Any]
66
- args = field.metadata["args"]
67
+ args = dict(field.metadata["args"])
67
68
 
68
69
  assert "dest" not in args, (
69
70
  "Specifying 'dest' directly is not supported."
@@ -86,8 +87,11 @@ class _cmdcommand(type):
86
87
  continue
87
88
 
88
89
  # Resolve the type from the fields type annotation.
90
+ (_type, _nargs) = argparse_type(_fieldtypes[field.name])
89
91
  if args.get("type", None) is None:
90
- args["type"] = argparse_type(field.type)
92
+ args["type"] = _type
93
+ if args.get("nargs", None) is None and _nargs is not None:
94
+ args["nargs"] = _nargs
91
95
 
92
96
  # Remove custom unspecified arguments.
93
97
  if args.get("choicesmap", None) is None:
@@ -168,7 +172,7 @@ def argparse_name_or_flags(
168
172
  return tuple(
169
173
  _decl
170
174
  for _decl in (
171
- decl or (form + field),
175
+ decl or (form + field.replace("_", "-")),
172
176
  altdecl,
173
177
  *more_decls,
174
178
  )
@@ -176,13 +180,10 @@ def argparse_name_or_flags(
176
180
  )
177
181
 
178
182
 
179
- def argparse_type(type: Any, /) -> Type[Any]:
180
- if isinstance(type, str):
181
- raise TypeError("forward reference type annotations are not supported")
182
-
183
+ def argparse_type(type: Any, /) -> Tuple[Type[Any], str | None]:
183
184
  origin = typing.get_origin(type)
184
185
  if origin is None:
185
- return type
186
+ return (type, None)
186
187
 
187
188
  if origin is typing.Annotated:
188
189
  (argtype, *_) = typing.get_args(type)
@@ -192,9 +193,12 @@ def argparse_type(type: Any, /) -> Type[Any]:
192
193
  # should be passed to the `argparse` module.
193
194
  if isinstance(type, types.UnionType):
194
195
  (argtype, *_) = typing.get_args(type)
195
- return argtype
196
+ return argparse_type(argtype)
196
197
  elif origin is typing.Union:
197
198
  (argtype, *_) = typing.get_args(type)
198
- return argtype
199
+ return argparse_type(argtype)
200
+ elif origin is list:
201
+ (argtype, *_) = typing.get_args(type)
202
+ return (argparse_type(argtype)[0], "*")
199
203
 
200
204
  raise TypeError("unsupported type annotation")
@@ -112,6 +112,8 @@ class _cmdfield(type):
112
112
 
113
113
  class _ArgumentArgs(TypedDict, total=False):
114
114
 
115
+ nargs: Annotated[int, "N"] | Literal["?", "*", "+"]
116
+
115
117
  type: Annotated[Any, Callable[[str], Annotated[Any, "FieldType"]]]
116
118
 
117
119
  help: str
@@ -180,6 +182,9 @@ class _cmdfield(type):
180
182
  The arguments default value if not specified.
181
183
  (Providing a default value makes the argument optional.)
182
184
 
185
+ :param nargs:
186
+ The number of command-line values to be consumed.
187
+ <br/> https://docs.python.org/3/library/argparse.html#nargs
183
188
  :param type:
184
189
  Explicitly specifies the `type` to be used by `argparse`.
185
190
  <br/> https://docs.python.org/3/library/argparse.html#type
@@ -192,10 +197,10 @@ class _cmdfield(type):
192
197
  """
193
198
  return _argparse(
194
199
  None,
195
- nargs=("?" if not isunset(default) else None),
196
200
  choices=choices,
197
201
  choicesmap=choicesmap,
198
202
  default=on_unset(default, None),
203
+ nargs=args.get("nargs", ("?" if not isunset(default) else None)),
199
204
  type=args.get("type", None),
200
205
  help=args.get("help", None),
201
206
  metavar=args.get("helpvar", None),
@@ -209,6 +214,8 @@ class _cmdfield(type):
209
214
  decls: Tuple[str, str] | str
210
215
  more_decls: Sequence[str] | str
211
216
 
217
+ nargs: Annotated[int, "N"] | Literal["?", "*", "+"]
218
+
212
219
  type: Annotated[Any, Callable[[str], Annotated[Any, "FieldType"]]]
213
220
 
214
221
  required: bool
@@ -388,6 +395,9 @@ class _cmdfield(type):
388
395
  :param more_decls:
389
396
  Specifies additional option declarations.
390
397
 
398
+ :param nargs:
399
+ The number of command-line values to be consumed.
400
+ <br/> https://docs.python.org/3/library/argparse.html#nargs
391
401
  :param type:
392
402
  Explicitly specifies the `type` to be used by `argparse`.
393
403
  <br/> https://docs.python.org/3/library/argparse.html#type
@@ -431,6 +441,7 @@ class _cmdfield(type):
431
441
  choicesmap=choicesmap,
432
442
  default=on_unset(default, None),
433
443
  required=args.get("required", False),
444
+ nargs=args.get("nargs", None),
434
445
  type=args.get("type", None),
435
446
  help=args.get("help", None),
436
447
  metavar=args.get("helpvar", None),
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cmdargparse
3
- Version: 0.2.0
3
+ Version: 0.3.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)
@@ -76,6 +76,9 @@ class MyApplication(cmd2.Cmd):
76
76
  * `default` <br/>
77
77
  The arguments default value if not specified.
78
78
  <br/> (Providing a default value makes the argument optional.)
79
+ * `nargs` <br/>
80
+ The number of command-line values to be consumed.
81
+ <br/> https://docs.python.org/3/library/argparse.html#nargs
79
82
  * `type` <br/>
80
83
  Explicitly specifies the `type` to be used by `argparse`.
81
84
  <br/> https://docs.python.org/3/library/argparse.html#type
@@ -94,6 +97,8 @@ arg: str | None = cmdfield.argument(
94
97
  },
95
98
  default=None,
96
99
  )
100
+ args: list[str] = cmdfield.argument()
101
+ more: list[str] = cmdfield.argument(nargs="+")
97
102
  ```
98
103
 
99
104
 
@@ -116,6 +121,9 @@ arg: str | None = cmdfield.argument(
116
121
  Explicitly specifies all option declarations.
117
122
  * `more_decls` <br/>
118
123
  Specifies additional option declarations.
124
+ * `nargs` <br/>
125
+ The number of command-line values to be consumed.
126
+ <br/> https://docs.python.org/3/library/argparse.html#nargs
119
127
  * `type` <br/>
120
128
  Explicitly specifies the `type` to be used by `argparse`.
121
129
  <br/> https://docs.python.org/3/library/argparse.html#type
@@ -131,6 +139,8 @@ arg: str | None = cmdfield.argument(
131
139
  xx: str | None = cmdfield.option()
132
140
  yy: int | None = cmdfield.option([1, 2, 3])
133
141
  zz: str = cmdfield.option(required=True)
142
+ hh: list[str] | None = cmdfield.option(default=None)
143
+ qq: list[int] | None = cmdfield.option(nargs=2, default=None)
134
144
  ```
135
145
 
136
146
  #### Declarations
@@ -145,6 +155,7 @@ mm: str | None = cmdfield.option(decls="--mm") # --mm
145
155
  nn: str | None = cmdfield.option(decls="-oo") # -oo
146
156
  pp: str | None = cmdfield.option(decls=("--rr", "-ss")) # --rr, -ss
147
157
  tt: str | None = cmdfield.option(more_decls="-uu") # -tt, -uu
158
+ vv_ww: str | None = cmdfield.option() # -vv-ww OR --vv-ww
148
159
  ```
149
160
 
150
161
 
@@ -192,6 +203,7 @@ mm: bool = cmdfield.flag(decls="--mm") # --mm
192
203
  nn: bool = cmdfield.flag(decls="-oo") # -oo
193
204
  pp: bool = cmdfield.flag(decls=("--rr", "-ss")) # --rr, -ss
194
205
  tt: bool = cmdfield.flag(more_decls="-uu") # -tt, -uu
206
+ vv_ww: bool = cmdfield.flag() # -vv-ww OR --vv-ww
195
207
  ```
196
208
 
197
209
 
@@ -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.2.0",
10
+ version="0.3.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