cmdargparse 0.1.0__tar.gz → 0.2.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 (22) hide show
  1. {cmdargparse-0.1.0 → cmdargparse-0.2.0}/PKG-INFO +7 -7
  2. {cmdargparse-0.1.0 → cmdargparse-0.2.0}/README.md +3 -2
  3. {cmdargparse-0.1.0 → cmdargparse-0.2.0}/cmdargparse/__init__.py +1 -1
  4. cmdargparse-0.2.0/cmdargparse/actions/__init__.py +1 -0
  5. cmdargparse-0.2.0/cmdargparse/actions/store.py +58 -0
  6. {cmdargparse-0.1.0 → cmdargparse-0.2.0}/cmdargparse/argument.py +1 -1
  7. {cmdargparse-0.1.0 → cmdargparse-0.2.0}/cmdargparse/field.py +19 -20
  8. {cmdargparse-0.1.0 → cmdargparse-0.2.0}/cmdargparse/namespace.py +6 -3
  9. {cmdargparse-0.1.0 → cmdargparse-0.2.0}/cmdargparse/parser.py +2 -2
  10. {cmdargparse-0.1.0 → cmdargparse-0.2.0}/cmdargparse/unset.py +24 -9
  11. {cmdargparse-0.1.0 → cmdargparse-0.2.0}/cmdargparse.egg-info/PKG-INFO +7 -7
  12. {cmdargparse-0.1.0 → cmdargparse-0.2.0}/cmdargparse.egg-info/SOURCES.txt +3 -1
  13. cmdargparse-0.2.0/cmdargparse.egg-info/requires.txt +1 -0
  14. {cmdargparse-0.1.0 → cmdargparse-0.2.0}/setup.py +42 -43
  15. cmdargparse-0.1.0/cmdargparse.egg-info/requires.txt +0 -1
  16. {cmdargparse-0.1.0 → cmdargparse-0.2.0}/LICENSE +0 -0
  17. {cmdargparse-0.1.0 → cmdargparse-0.2.0}/cmdargparse/command.py +0 -0
  18. {cmdargparse-0.1.0 → cmdargparse-0.2.0}/cmdargparse/custom.py +0 -0
  19. {cmdargparse-0.1.0 → cmdargparse-0.2.0}/cmdargparse.egg-info/dependency_links.txt +0 -0
  20. {cmdargparse-0.1.0 → cmdargparse-0.2.0}/cmdargparse.egg-info/top_level.txt +0 -0
  21. {cmdargparse-0.1.0 → cmdargparse-0.2.0}/pyproject.toml +0 -0
  22. {cmdargparse-0.1.0 → cmdargparse-0.2.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cmdargparse
3
- Version: 0.1.0
3
+ Version: 0.2.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)
@@ -11,15 +11,14 @@ Classifier: Environment :: Console
11
11
  Classifier: Environment :: Plugins
12
12
  Classifier: Intended Audience :: Developers
13
13
  Classifier: Intended Audience :: System Administrators
14
- Classifier: License :: OSI Approved :: MIT License
15
14
  Classifier: Operating System :: OS Independent
16
15
  Classifier: Programming Language :: Python :: 3 :: Only
17
- Classifier: Programming Language :: Python :: 3.13
16
+ Classifier: Programming Language :: Python :: 3.14
18
17
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
19
- Requires-Python: >=3.13
18
+ Requires-Python: >=3.14
20
19
  Description-Content-Type: text/markdown
21
20
  License-File: LICENSE
22
- Requires-Dist: cmd2~=2.7
21
+ Requires-Dist: cmd2~=4.2
23
22
  Dynamic: author
24
23
  Dynamic: classifier
25
24
  Dynamic: description
@@ -46,7 +45,7 @@ The package provides a declarative way to define `cmd2` argument parsers.
46
45
  The core functionality is driven by three components:
47
46
  * `cmdargument` for defining an argument parser
48
47
  * `cmdfield` for declaring the arguments
49
- * `cmdcommand` for attatching an argument parser to a command
48
+ * `cmdcommand` for attaching an argument parser to a command
50
49
 
51
50
  ```py
52
51
 
@@ -56,7 +55,7 @@ class MyApplication(cmd2.Cmd):
56
55
  class _MyCommandArgs(cmdargument):
57
56
  my_argument: str = cmdfield.argument(...)
58
57
  my_option: str = cmdfield.option(...)
59
- my_flag: str = cmdfield.flag(...)
58
+ my_flag: bool = cmdfield.flag(...)
60
59
 
61
60
  @cmdcommand(_MyCommandArgs)
62
61
  def do_mycommand(self, args: _MyCommandArgs) -> None:
@@ -167,6 +166,7 @@ tt: str | None = cmdfield.option(more_decls="-uu") # -tt, -uu
167
166
  Specifies additional flag declarations.
168
167
  * `type` <br/>
169
168
  Explicitly specifies the type to be used by argparse.
169
+ <br/> (Has no effect, as flag values are not type converted.)
170
170
  <br/> https://docs.python.org/3/library/argparse.html#type
171
171
  * `help` <br/>
172
172
  Brief description of the flag.
@@ -12,7 +12,7 @@ The package provides a declarative way to define `cmd2` argument parsers.
12
12
  The core functionality is driven by three components:
13
13
  * `cmdargument` for defining an argument parser
14
14
  * `cmdfield` for declaring the arguments
15
- * `cmdcommand` for attatching an argument parser to a command
15
+ * `cmdcommand` for attaching an argument parser to a command
16
16
 
17
17
  ```py
18
18
 
@@ -22,7 +22,7 @@ class MyApplication(cmd2.Cmd):
22
22
  class _MyCommandArgs(cmdargument):
23
23
  my_argument: str = cmdfield.argument(...)
24
24
  my_option: str = cmdfield.option(...)
25
- my_flag: str = cmdfield.flag(...)
25
+ my_flag: bool = cmdfield.flag(...)
26
26
 
27
27
  @cmdcommand(_MyCommandArgs)
28
28
  def do_mycommand(self, args: _MyCommandArgs) -> None:
@@ -133,6 +133,7 @@ tt: str | None = cmdfield.option(more_decls="-uu") # -tt, -uu
133
133
  Specifies additional flag declarations.
134
134
  * `type` <br/>
135
135
  Explicitly specifies the type to be used by argparse.
136
+ <br/> (Has no effect, as flag values are not type converted.)
136
137
  <br/> https://docs.python.org/3/library/argparse.html#type
137
138
  * `help` <br/>
138
139
  Brief description of the flag.
@@ -7,4 +7,4 @@ from .namespace import Namespace
7
7
  from .parser import ArgumentParser
8
8
 
9
9
 
10
- _cmd2.set_default_argument_parser_type(ArgumentParser)
10
+ _cmd2.set_default_argument_parser(ArgumentParser)
@@ -0,0 +1 @@
1
+ from .store import StoreAction
@@ -0,0 +1,58 @@
1
+ import argparse
2
+ from typing import Annotated, Any, Mapping, Sequence
3
+
4
+ from cmdargparse.custom import (
5
+ TANY,
6
+ ArgumentParser,
7
+ Namespace,
8
+ decorator,
9
+ is_multiple_values,
10
+ )
11
+
12
+
13
+ # ################################ ACTION ######################################
14
+
15
+
16
+ class StoreAction(argparse._StoreAction):
17
+
18
+ def __init__(
19
+ self,
20
+ *args: Annotated[Any, "passthrough"],
21
+ choicesmap: Mapping[TANY, TANY] | None = None,
22
+ **kwargs: Annotated[Any, "passthrough"],
23
+ ) -> None:
24
+ assert not args, (
25
+ "There should be no positional arguments if not explicitly "
26
+ "specified. (The `argparse` module passes by keyword.)"
27
+ )
28
+
29
+ if "choices" in kwargs and choicesmap is not None:
30
+ if any(s not in kwargs["choices"] for s in choicesmap.values()):
31
+ raise ValueError(
32
+ "All mapped values in the choices mapping must be present "
33
+ "in the specified choices."
34
+ )
35
+ kwargs["choices"] = (
36
+ *kwargs["choices"],
37
+ *choicesmap.keys(),
38
+ )
39
+
40
+ super().__init__(*args, **kwargs)
41
+
42
+ self.choicesmap = choicesmap
43
+
44
+ @decorator.call
45
+ def __call__(
46
+ self,
47
+ parser: ArgumentParser,
48
+ namespace: Namespace,
49
+ values: Sequence[TANY] | TANY,
50
+ option_string: str | None = None,
51
+ ) -> None:
52
+ if self.choicesmap is not None:
53
+ values = (
54
+ [self.choicesmap.get(v, v) for v in values]
55
+ if is_multiple_values(values)
56
+ else self.choicesmap.get(values, values)
57
+ )
58
+ setattr(namespace, self.dest, values)
@@ -72,7 +72,7 @@ class _cmdargument(type):
72
72
  if _cls is None:
73
73
  return functools.partial(
74
74
  cls.__call__,
75
- default=form,
75
+ form=form,
76
76
  ) # type: ignore
77
77
 
78
78
  setattr(
@@ -401,33 +401,32 @@ class _cmdfield(type):
401
401
  Reference name of the option value in the help message.
402
402
 
403
403
  """
404
- if decl is None:
405
- decl, altdecl, choices = (
406
- # 0 arguments
407
- (None, None, None)
404
+ if (
405
+ choices is None
406
+ and (decl is None or isinstance(decl, str))
407
+ and (altdecl is None or isinstance(altdecl, str))
408
+ ):
409
+ decl, altdecl, choices, choicesmap = (
410
+ # option
411
+ (decl, altdecl, None, None)
408
412
  ) # type: ignore
409
- elif altdecl is None:
410
- decl, altdecl, choices = (
411
- # 1 argument
412
- (decl, None, None)
413
- if isinstance(decl, str)
414
- else (None, None, decl)
413
+ elif decl is not None and not isinstance(decl, str):
414
+ decl, altdecl, choices, choicesmap = (
415
+ # choice option [derived]
416
+ (None, None, decl, altdecl)
415
417
  ) # type: ignore
416
- elif choices is None:
417
- decl, altdecl, choices = (
418
- # 2 arguments
419
- (decl, altdecl, None)
420
- if isinstance(decl, str)
421
- else (decl, None, altdecl)
418
+ elif altdecl is not None and not isinstance(altdecl, str):
419
+ decl, altdecl, choices, choicesmap = (
420
+ # choice option [augmented]
421
+ (decl, None, altdecl, choices)
422
422
  ) # type: ignore
423
423
  else:
424
- decl, altdecl, choices = (
425
- # 3 arguments
426
- (decl, altdecl, choices)
424
+ decl, altdecl, choices, choicesmap = (
425
+ # choice option [augmented]
426
+ (decl, altdecl, choices, choicesmap)
427
427
  ) # type: ignore
428
428
  return _argparse(
429
429
  _decls(decl, altdecl, args),
430
- nargs=("?" if not isunset(default) else None),
431
430
  choices=choices,
432
431
  choicesmap=choicesmap,
433
432
  default=on_unset(default, None),
@@ -22,16 +22,19 @@ class Namespace(argparse.Namespace):
22
22
  )
23
23
  )
24
24
 
25
+ cmd.pfeedback("[Namespace]")
26
+
27
+ if not fields:
28
+ cmd.pfeedback(" (no fields)")
29
+ return
30
+
25
31
  maxlen = max(
26
32
  len(attr)
27
33
  for attr in fields
28
34
  # <format-break>
29
35
  )
30
36
 
31
- cmd.pfeedback("[Namespace]")
32
37
  for name in fields:
33
38
  cmd.pfeedback(
34
39
  f" {str.ljust(name, maxlen)!s} : {self.__dict__[name]!r}"
35
40
  )
36
- if not fields:
37
- cmd.pfeedback(" (no fields)")
@@ -9,7 +9,7 @@ import cmd2
9
9
  class ArgumentParser(cmd2.Cmd2ArgumentParser):
10
10
 
11
11
  # Registers a custom argparse argument parameter.
12
- # https://cmd2.readthedocs.io/en/stable/api/argparse_custom/#cmd2.argparse_custom.register_argparse_argument_parameter
12
+ # https://cmd2.readthedocs.io/en/stable/api/argparse_utils/#cmd2.argparse_utils.register_argparse_argument_parameter
13
13
  # ```
14
14
  # def register_argparse_argument_parameter(
15
15
  # param_name: str,
@@ -22,7 +22,7 @@ class ArgumentParser(cmd2.Cmd2ArgumentParser):
22
22
  *args: Annotated[Any, "passthrough"],
23
23
  **kwargs: Annotated[Any, "passthrough"],
24
24
  ) -> None:
25
- super(cmd2.Cmd2ArgumentParser, self).__init__(*args, **kwargs)
25
+ super().__init__(*args, **kwargs)
26
26
 
27
27
  from .actions import StoreAction
28
28
 
@@ -1,11 +1,11 @@
1
- from typing import Any, Final, TypeAlias, TypeIs, TypeVar
1
+ from typing import Any, Final, TypeAlias, TypeIs, TypeVar, cast
2
2
 
3
3
 
4
- # ################################ PACKAGE #####################################
4
+ # ################################ COMPONENT ###################################
5
5
 
6
6
 
7
- __sname__ = "unset"
8
- __version__ = "1.3"
7
+ __component__ = "unset"
8
+ __version__ = "1.5"
9
9
  __description__ = ...
10
10
 
11
11
  __requires__ = ()
@@ -13,7 +13,7 @@ __requires__ = ()
13
13
 
14
14
  __all__ = (
15
15
  # fmt: off
16
- "UnsetType", "UNSET", "Unset",
16
+ "UnsetType", "UNSET", "Unset", "MayUnset",
17
17
  "isunset", "on_unset",
18
18
  # fmt: on
19
19
  )
@@ -24,7 +24,7 @@ __all__ = (
24
24
 
25
25
  T = TypeVar("T")
26
26
 
27
- TONCE: TypeAlias = Any
27
+ TONCE = TypeVar("TONCE")
28
28
 
29
29
 
30
30
  # ################################ UNSET #######################################
@@ -34,7 +34,6 @@ class _UnsetType(type):
34
34
 
35
35
  def __invert__(self) -> "UnsetType":
36
36
  """Returns the unset value."""
37
- global UNSET
38
37
  return UNSET
39
38
 
40
39
 
@@ -60,6 +59,22 @@ It can be used as follows:
60
59
  ```
61
60
  def function(arg: Type | Unset = ~Unset):
62
61
  arg = arg if not isunset(arg) else VALUE
62
+ # or
63
+ arg = on_unset(arg, VALUE)
64
+ ```
65
+ """
66
+
67
+
68
+ MayUnset: TypeAlias = T | UnsetType
69
+ """
70
+ The `MayUnset` attribute is a convenience alias for the union of a generic type
71
+ and the `UnsetType`.
72
+
73
+ It can be used as follows:
74
+ ```
75
+ def function(arg: MayUnset[Type] = ~Unset):
76
+ arg = arg if not isunset(arg) else VALUE
77
+ # or
63
78
  arg = on_unset(arg, VALUE)
64
79
  ```
65
80
  """
@@ -73,7 +88,7 @@ def isunset(obj: Any, /) -> TypeIs[UnsetType]:
73
88
  return obj is UNSET
74
89
 
75
90
 
76
- def on_unset(obj: T | Unset, value: TONCE, /) -> T | TONCE:
91
+ def on_unset(obj: T | UnsetType, value: TONCE, /) -> T | TONCE:
77
92
  """Returns either the object itself or the specified value if the object is
78
93
  unset."""
79
- return value if obj is UNSET else obj
94
+ return value if obj is UNSET else cast(T, obj)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cmdargparse
3
- Version: 0.1.0
3
+ Version: 0.2.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)
@@ -11,15 +11,14 @@ Classifier: Environment :: Console
11
11
  Classifier: Environment :: Plugins
12
12
  Classifier: Intended Audience :: Developers
13
13
  Classifier: Intended Audience :: System Administrators
14
- Classifier: License :: OSI Approved :: MIT License
15
14
  Classifier: Operating System :: OS Independent
16
15
  Classifier: Programming Language :: Python :: 3 :: Only
17
- Classifier: Programming Language :: Python :: 3.13
16
+ Classifier: Programming Language :: Python :: 3.14
18
17
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
19
- Requires-Python: >=3.13
18
+ Requires-Python: >=3.14
20
19
  Description-Content-Type: text/markdown
21
20
  License-File: LICENSE
22
- Requires-Dist: cmd2~=2.7
21
+ Requires-Dist: cmd2~=4.2
23
22
  Dynamic: author
24
23
  Dynamic: classifier
25
24
  Dynamic: description
@@ -46,7 +45,7 @@ The package provides a declarative way to define `cmd2` argument parsers.
46
45
  The core functionality is driven by three components:
47
46
  * `cmdargument` for defining an argument parser
48
47
  * `cmdfield` for declaring the arguments
49
- * `cmdcommand` for attatching an argument parser to a command
48
+ * `cmdcommand` for attaching an argument parser to a command
50
49
 
51
50
  ```py
52
51
 
@@ -56,7 +55,7 @@ class MyApplication(cmd2.Cmd):
56
55
  class _MyCommandArgs(cmdargument):
57
56
  my_argument: str = cmdfield.argument(...)
58
57
  my_option: str = cmdfield.option(...)
59
- my_flag: str = cmdfield.flag(...)
58
+ my_flag: bool = cmdfield.flag(...)
60
59
 
61
60
  @cmdcommand(_MyCommandArgs)
62
61
  def do_mycommand(self, args: _MyCommandArgs) -> None:
@@ -167,6 +166,7 @@ tt: str | None = cmdfield.option(more_decls="-uu") # -tt, -uu
167
166
  Specifies additional flag declarations.
168
167
  * `type` <br/>
169
168
  Explicitly specifies the type to be used by argparse.
169
+ <br/> (Has no effect, as flag values are not type converted.)
170
170
  <br/> https://docs.python.org/3/library/argparse.html#type
171
171
  * `help` <br/>
172
172
  Brief description of the flag.
@@ -14,4 +14,6 @@ cmdargparse.egg-info/PKG-INFO
14
14
  cmdargparse.egg-info/SOURCES.txt
15
15
  cmdargparse.egg-info/dependency_links.txt
16
16
  cmdargparse.egg-info/requires.txt
17
- cmdargparse.egg-info/top_level.txt
17
+ cmdargparse.egg-info/top_level.txt
18
+ cmdargparse/actions/__init__.py
19
+ cmdargparse/actions/store.py
@@ -0,0 +1 @@
1
+ cmd2~=4.2
@@ -1,43 +1,42 @@
1
- from setuptools import find_packages, setup
2
-
3
-
4
- with open("README.md", "r", encoding="utf-8") as file:
5
- README = file.read()
6
-
7
-
8
- setup(
9
- name="cmdargparse",
10
- version="0.1.0",
11
- description="A declarative way to define `cmd2` argument parsers.",
12
- long_description=README,
13
- long_description_content_type="text/markdown",
14
- author="Kilian Kaiping (krnd)",
15
- url="https://github.com/krnd/cmdargparse",
16
- license="MIT",
17
- packages=find_packages(include=["cmdargparse"]),
18
- # Tested with Python 3.13 only.
19
- python_requires=">=3.13",
20
- install_requires=[
21
- ("cmd2" "~=2.7"),
22
- ],
23
- keywords=[
24
- "CLI",
25
- "cmd",
26
- "command",
27
- "interactive",
28
- "prompt",
29
- "Python",
30
- ],
31
- classifiers=[
32
- "Development Status :: 3 - Alpha",
33
- "Environment :: Console",
34
- "Environment :: Plugins",
35
- "Intended Audience :: Developers",
36
- "Intended Audience :: System Administrators",
37
- "License :: OSI Approved :: MIT License",
38
- "Operating System :: OS Independent",
39
- "Programming Language :: Python :: 3 :: Only",
40
- "Programming Language :: Python :: 3.13",
41
- "Topic :: Software Development :: Libraries :: Python Modules",
42
- ],
43
- )
1
+ from setuptools import find_packages, setup
2
+
3
+
4
+ with open("README.md", "r", encoding="utf-8") as file:
5
+ README = file.read()
6
+
7
+
8
+ setup(
9
+ name="cmdargparse",
10
+ version="0.2.0",
11
+ description="A declarative way to define `cmd2` argument parsers.",
12
+ long_description=README,
13
+ long_description_content_type="text/markdown",
14
+ author="Kilian Kaiping (krnd)",
15
+ url="https://github.com/krnd/cmdargparse",
16
+ license="MIT",
17
+ packages=find_packages(include=["cmdargparse", "cmdargparse.*"]),
18
+ # Tested with Python 3.14 only.
19
+ python_requires=">=3.14",
20
+ install_requires=[
21
+ ("cmd2" "~=4.2"),
22
+ ],
23
+ keywords=[
24
+ "CLI",
25
+ "cmd",
26
+ "command",
27
+ "interactive",
28
+ "prompt",
29
+ "Python",
30
+ ],
31
+ classifiers=[
32
+ "Development Status :: 3 - Alpha",
33
+ "Environment :: Console",
34
+ "Environment :: Plugins",
35
+ "Intended Audience :: Developers",
36
+ "Intended Audience :: System Administrators",
37
+ "Operating System :: OS Independent",
38
+ "Programming Language :: Python :: 3 :: Only",
39
+ "Programming Language :: Python :: 3.14",
40
+ "Topic :: Software Development :: Libraries :: Python Modules",
41
+ ],
42
+ )
@@ -1 +0,0 @@
1
- cmd2~=2.7
File without changes
File without changes
File without changes