paramspecli 0.2.1__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) 2026 jkmnt
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,88 @@
1
+ Metadata-Version: 2.4
2
+ Name: paramspecli
3
+ Version: 0.2.1
4
+ Summary: Type-safe facade for the venerable argparse
5
+ Author: jkmnt
6
+ Requires-Python: >=3.12
7
+ Description-Content-Type: text/markdown
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ License-File: LICENSE
12
+ Project-URL: Documentation, https://jkmnt.github.io/paramspecli
13
+ Project-URL: Source, https://github.com/jkmnt/paramspecli
14
+
15
+ # paramspecli
16
+
17
+ **paramspecli** is a facade for the venerable [argparse](https://docs.python.org/3/library/argparse.html).
18
+ It's simple, composable, and fun. But, most important, it's type-safe thanks to the [ParamSpec](https://docs.python.org/3/library/typing.html#typing.ParamSpec) magic.
19
+
20
+ ## Quick start
21
+
22
+ ```python
23
+ from paramspecli import Command, argument, option, flag
24
+
25
+
26
+ def ping(addr: str, *, until_stopped: bool, count: int | None):
27
+ print(f"Pinging {addr} ...")
28
+
29
+
30
+ cli = Command(ping, prog="ping")
31
+ cli.bind(
32
+ -argument("IP"),
33
+ until_stopped=-flag("-t"),
34
+ count=-option("-n", type=int),
35
+ )
36
+
37
+ result = cli.parse()
38
+ result()
39
+ ```
40
+
41
+ Running it:
42
+
43
+ ```
44
+ $ python ping.py -t -n 10 127.0.0.1
45
+ Pinging 127.0.0.1 ...
46
+ ```
47
+
48
+ Type checker catches common errors:
49
+
50
+ ```python
51
+ cli.bind(
52
+ # "bool" is not assignable to "str"
53
+ -argument("IP", type=bool),
54
+ # Type "bool | None" is not assignable to type "bool"
55
+ until_stopped=-flag("-t", default=None),
56
+ #
57
+ # Argument missing for parameter "count"
58
+ )
59
+ ```
60
+
61
+ IDE suggestions work too:
62
+
63
+ <pre>
64
+ cli.bind(⏎
65
+ <small>(<strong>addr: str</strong>, *, until_stopped: bool, count: int | None) -> None</small>
66
+ </pre>
67
+
68
+ ## Installation
69
+
70
+ ```shell
71
+ pip install paramspecli
72
+ ```
73
+
74
+ ## Key points
75
+
76
+ **paramspecli** builds hierarchical CLIs with groups and commands.
77
+
78
+ - Commands match handler functions parameters with arguments and options.
79
+ - Arguments are bound to the handler's positional parameters.
80
+ - Options are bound to the handler's keyword parameters.
81
+ - Groups organize the commands. Groups may be nested. They could act like like an intermediate commands, i.e. have own handlers, options and arguments.
82
+ - CLI 'compiles' to the `argparse`, runs it, then outputs the parse result.
83
+ - Parse result is a callable. Calling it invokes handlers along the route.
84
+
85
+ **paramspecli** supports several advanced `argparse` features: help sections, mutually exclusive
86
+ options, options with the same destination. And adds a few own, like const options and passing contexts. It also includes a markdown documentation generator.
87
+
88
+ [Read the documentation](https://jkmnt.github.io/paramspecli)
@@ -0,0 +1,74 @@
1
+ # paramspecli
2
+
3
+ **paramspecli** is a facade for the venerable [argparse](https://docs.python.org/3/library/argparse.html).
4
+ It's simple, composable, and fun. But, most important, it's type-safe thanks to the [ParamSpec](https://docs.python.org/3/library/typing.html#typing.ParamSpec) magic.
5
+
6
+ ## Quick start
7
+
8
+ ```python
9
+ from paramspecli import Command, argument, option, flag
10
+
11
+
12
+ def ping(addr: str, *, until_stopped: bool, count: int | None):
13
+ print(f"Pinging {addr} ...")
14
+
15
+
16
+ cli = Command(ping, prog="ping")
17
+ cli.bind(
18
+ -argument("IP"),
19
+ until_stopped=-flag("-t"),
20
+ count=-option("-n", type=int),
21
+ )
22
+
23
+ result = cli.parse()
24
+ result()
25
+ ```
26
+
27
+ Running it:
28
+
29
+ ```
30
+ $ python ping.py -t -n 10 127.0.0.1
31
+ Pinging 127.0.0.1 ...
32
+ ```
33
+
34
+ Type checker catches common errors:
35
+
36
+ ```python
37
+ cli.bind(
38
+ # "bool" is not assignable to "str"
39
+ -argument("IP", type=bool),
40
+ # Type "bool | None" is not assignable to type "bool"
41
+ until_stopped=-flag("-t", default=None),
42
+ #
43
+ # Argument missing for parameter "count"
44
+ )
45
+ ```
46
+
47
+ IDE suggestions work too:
48
+
49
+ <pre>
50
+ cli.bind(⏎
51
+ <small>(<strong>addr: str</strong>, *, until_stopped: bool, count: int | None) -> None</small>
52
+ </pre>
53
+
54
+ ## Installation
55
+
56
+ ```shell
57
+ pip install paramspecli
58
+ ```
59
+
60
+ ## Key points
61
+
62
+ **paramspecli** builds hierarchical CLIs with groups and commands.
63
+
64
+ - Commands match handler functions parameters with arguments and options.
65
+ - Arguments are bound to the handler's positional parameters.
66
+ - Options are bound to the handler's keyword parameters.
67
+ - Groups organize the commands. Groups may be nested. They could act like like an intermediate commands, i.e. have own handlers, options and arguments.
68
+ - CLI 'compiles' to the `argparse`, runs it, then outputs the parse result.
69
+ - Parse result is a callable. Calling it invokes handlers along the route.
70
+
71
+ **paramspecli** supports several advanced `argparse` features: help sections, mutually exclusive
72
+ options, options with the same destination. And adds a few own, like const options and passing contexts. It also includes a markdown documentation generator.
73
+
74
+ [Read the documentation](https://jkmnt.github.io/paramspecli)
@@ -0,0 +1,88 @@
1
+ [build-system]
2
+ requires = ["flit_core >=3.2,<4"]
3
+ build-backend = "flit_core.buildapi"
4
+
5
+ [project]
6
+ name = "paramspecli"
7
+ authors = [{ name = "jkmnt" }]
8
+ readme = "README.md"
9
+ requires-python = ">=3.12"
10
+ license = { file = "LICENSE" }
11
+ classifiers = [
12
+ "Programming Language :: Python :: 3",
13
+ "License :: OSI Approved :: MIT License",
14
+ "Operating System :: OS Independent",
15
+ ]
16
+ dynamic = ["version", "description"]
17
+
18
+ [project.urls]
19
+ Source = "https://github.com/jkmnt/paramspecli"
20
+ Documentation = "https://jkmnt.github.io/paramspecli"
21
+
22
+ [tool.flit.sdist]
23
+ exclude = ["tests/"]
24
+
25
+
26
+ [tool.ruff]
27
+ line-length = 120
28
+
29
+ [tool.black]
30
+ line-length = 120
31
+
32
+ [tool.ruff.lint]
33
+ select = [
34
+ "E",
35
+ "F",
36
+ "UP",
37
+ "B",
38
+ "ANN0",
39
+ "I",
40
+ "SLOT",
41
+ "ICN",
42
+ "FBT",
43
+ "C4",
44
+ "LOG",
45
+ "RET",
46
+ "INT",
47
+ "RUF",
48
+ "PLE",
49
+ "T20",
50
+ "PT",
51
+ ]
52
+
53
+ ignore = [
54
+ "SIM108",
55
+ "FBT003",
56
+ "RET504",
57
+ "RET505",
58
+ "E501",
59
+ "RUF015",
60
+ "UP035",
61
+ "C408",
62
+ "B009",
63
+ "B010",
64
+ ]
65
+
66
+ [tool.pyright]
67
+ # typeCheckingMode = "strict"
68
+ typeCheckingMode = "standard"
69
+ useLibraryCodeForTypes = true
70
+ strictListInference = true
71
+ strictSetInference = true
72
+ strictDictionaryInference = true
73
+ reportMissingImports = "error"
74
+ # reportMissingTypeStubs = "none"
75
+ reportMissingParameterType = "warning"
76
+ reportUnnecessaryComparison = "information"
77
+ reportUnnecessaryContains = "information"
78
+ reportPrivateUsage = false
79
+
80
+ [tool.ty.analysis]
81
+ respect-type-ignore-comments = false
82
+
83
+ [tool.ty.rules]
84
+ assert-type-unspellable-subtype = "ignore"
85
+ # ty type assertions are not compatible with mypy/pyright
86
+ type-assertion-failure = "ignore"
87
+ # fails to find pytest
88
+ unresolved-import = "ignore"
@@ -0,0 +1,27 @@
1
+ """Type-safe facade for the venerable argparse"""
2
+
3
+ __version__ = "0.2.1"
4
+
5
+
6
+ from .args import argument as argument
7
+ from .cli import Action as Action
8
+ from .cli import CallableGroup as CallableGroup
9
+ from .cli import Command as Command
10
+ from .cli import Config as Config
11
+ from .cli import Group as Group
12
+ from .cli import Handler as Handler
13
+ from .cli import Route as Route
14
+ from .cli import help_action as help_action
15
+ from .cli import version_action as version_action
16
+ from .conv import PathConv as PathConv
17
+ from .fake import Const as Const
18
+ from .fake import Context as Context
19
+ from .fake import deprecated as deprecated
20
+ from .fake import required as required
21
+ from .fake import t as t
22
+ from .flags import count as count
23
+ from .flags import flag as flag
24
+ from .flags import repeated_flag as repeated_flag
25
+ from .flags import switch as switch
26
+ from .opts import option as option
27
+ from .opts import repeated_option as repeated_option
@@ -0,0 +1,84 @@
1
+ # Protocols are used since some of the needed argparse classes are internal, e.g. _SubParsersAction
2
+ # Based on a typeshed with a small changes and omissions.
3
+ import argparse
4
+ from typing import Any, Callable, Iterable, NoReturn, Protocol
5
+
6
+ type TypeConverter[T] = Callable[[str], T]
7
+
8
+
9
+ class SupportsAddArgument(Protocol):
10
+ def add_argument(
11
+ self,
12
+ *names: str,
13
+ nargs: int | str | None = ...,
14
+ const: Any = ...,
15
+ default: Any = ...,
16
+ type: TypeConverter[Any] = ...,
17
+ choices: Iterable[Any] | None = ...,
18
+ action: str | type[argparse.Action] = ...,
19
+ required: bool = ...,
20
+ help: str | None = ...,
21
+ metavar: str | tuple[str, ...] | None = ...,
22
+ dest: str | None = ...,
23
+ version: str = ...,
24
+ **kwargs: Any,
25
+ ) -> argparse.Action: ...
26
+
27
+
28
+ class SupportsSetDefaults(Protocol):
29
+ def set_defaults(self, **kwargs: Any) -> None: ...
30
+
31
+
32
+ class SupportsAddOneofGroup(Protocol):
33
+ def add_mutually_exclusive_group(self, *, required: bool = ...) -> SupportsAddArgument: ...
34
+
35
+
36
+ class SupportsAddParser(Protocol):
37
+ def add_parser(
38
+ self,
39
+ name: str,
40
+ *,
41
+ # own
42
+ help: str | None = ...,
43
+ aliases: tuple[str, ...] = ...,
44
+ # of ArgumentParser
45
+ usage: str | None = ...,
46
+ description: str | None = ...,
47
+ epilog: str | None = ...,
48
+ formatter_class: type[argparse.HelpFormatter] = ...,
49
+ allow_abbrev: bool = ...,
50
+ # NOTE: exit_on_error is broken in argparse. in same cases it's exit anyway
51
+ exit_on_error: bool = ...,
52
+ prog: str | None = ...,
53
+ add_help: bool = ...,
54
+ # untyped rest
55
+ **kwargs: Any,
56
+ ) -> "ArgumentParserLike": ...
57
+
58
+
59
+ class SupportsAddArgumentGroup(Protocol):
60
+ def add_argument_group(self, title: str | None = ..., description: str | None = ...) -> "ArgumentGroupLike": ...
61
+
62
+
63
+ class ArgumentParserLike(
64
+ SupportsAddArgument, SupportsAddArgumentGroup, SupportsAddOneofGroup, SupportsSetDefaults, Protocol
65
+ ):
66
+ def add_subparsers(
67
+ self,
68
+ *,
69
+ title: str = ...,
70
+ prog: str | None = ...,
71
+ # parser_class: type[argparse.ArgumentParser],
72
+ parser_class: type[Any],
73
+ required: bool = ...,
74
+ help: str | None = ...,
75
+ metavar: str | None = ...,
76
+ description: str | None = ...,
77
+ dest: str | None = None,
78
+ ) -> SupportsAddParser: ...
79
+
80
+ def print_help(self) -> None: ...
81
+ def exit(self, status: int = ..., message: str | None = ...) -> NoReturn: ...
82
+
83
+
84
+ class ArgumentGroupLike(SupportsAddArgument, SupportsAddOneofGroup, Protocol): ...
@@ -0,0 +1,110 @@
1
+ from typing import Any, Iterable, Literal, overload
2
+
3
+ from .apstub import TypeConverter
4
+ from .fake import Argument
5
+
6
+
7
+ ##
8
+ @overload
9
+ def argument(
10
+ metavar: str,
11
+ *,
12
+ help: str | Literal[False] | None = None,
13
+ choices: Iterable[str] | None = None,
14
+ ) -> Argument[str, str]: ...
15
+
16
+
17
+ ## nargs
18
+ @overload
19
+ def argument(
20
+ metavar: str,
21
+ *,
22
+ nargs: int | Literal["*", "+"],
23
+ help: str | Literal[False] | None = None,
24
+ choices: Iterable[str] | None = None,
25
+ ) -> Argument[list[str], list[str]]: ...
26
+
27
+
28
+ ## optional
29
+ @overload
30
+ def argument(
31
+ metavar: str,
32
+ *,
33
+ nargs: Literal["?"],
34
+ help: str | Literal[False] | None = None,
35
+ choices: Iterable[str] | None = None,
36
+ ) -> Argument[str, None]: ...
37
+
38
+
39
+ ## optional, default: D
40
+ @overload
41
+ def argument[D](
42
+ metavar: str,
43
+ *,
44
+ nargs: Literal["?"],
45
+ default: D,
46
+ help: str | Literal[False] | None = None,
47
+ choices: Iterable[str] | None = None,
48
+ ) -> Argument[str, D]: ...
49
+
50
+
51
+ ## type: T
52
+ @overload
53
+ def argument[T](
54
+ metavar: str,
55
+ *,
56
+ type: TypeConverter[T],
57
+ help: str | Literal[False] | None = None,
58
+ choices: Iterable[T] | None = None,
59
+ ) -> Argument[T, T]: ...
60
+
61
+
62
+ ## type: T, nargs
63
+ @overload
64
+ def argument[T](
65
+ metavar: str,
66
+ *,
67
+ type: TypeConverter[T],
68
+ nargs: int | Literal["*", "+"],
69
+ help: str | Literal[False] | None = None,
70
+ choices: Iterable[T] | None = None,
71
+ ) -> Argument[list[T], list[T]]: ...
72
+
73
+
74
+ ## type: T, optional
75
+ @overload
76
+ def argument[T](
77
+ metavar: str,
78
+ *,
79
+ type: TypeConverter[T],
80
+ nargs: Literal["?"],
81
+ help: str | Literal[False] | None = None,
82
+ choices: Iterable[T] | None = None,
83
+ ) -> Argument[T, None]: ...
84
+
85
+
86
+ ## type: T, optional, default: str | D
87
+ # default is parsed if string
88
+ @overload
89
+ def argument[T, D](
90
+ metavar: str,
91
+ *,
92
+ type: TypeConverter[T],
93
+ nargs: Literal["?"],
94
+ help: str | Literal[False] | None = None,
95
+ default: str | D,
96
+ choices: Iterable[T] | None = None,
97
+ ) -> Argument[T, T | D]: ...
98
+
99
+
100
+ def argument(
101
+ metavar: str,
102
+ *,
103
+ type: TypeConverter[Any] | None = None,
104
+ help: str | Literal[False] | None = None,
105
+ nargs: int | Literal["*", "+", "?"] | None = None,
106
+ default: Any | None = None,
107
+ choices: Iterable[Any] | None = None,
108
+ ) -> Argument[Any, Any]:
109
+ """Positional argument. Always required, unless made optional via `nargs="?"`."""
110
+ return Argument(metavar, help=help, type=type, choices=choices, nargs=nargs, default=default)