cli-def 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 (102) hide show
  1. cli_def-0.2.0/PKG-INFO +187 -0
  2. cli_def-0.2.0/README.md +143 -0
  3. {cli_def-0.1.0 → cli_def-0.2.0}/pyproject.toml +1 -1
  4. cli_def-0.2.0/src/cli_def/__init__.py +12 -0
  5. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/backend/argparse/argparse_builder.py +32 -48
  6. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/backend/click/click_builder.py +35 -25
  7. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/backend/protocols.py +2 -2
  8. cli_def-0.2.0/src/cli_def/basic/__init__.py +2 -0
  9. cli_def-0.2.0/src/cli_def/basic/basic_types.py +10 -0
  10. cli_def-0.2.0/src/cli_def/core/__init__.py +2 -0
  11. cli_def-0.2.0/src/cli_def/core/models/__init__.py +34 -0
  12. cli_def-0.2.0/src/cli_def/core/models/common/__init__.py +8 -0
  13. cli_def-0.2.0/src/cli_def/core/models/common/mult_def.py +189 -0
  14. cli_def-0.2.0/src/cli_def/core/models/generic/__init__.py +17 -0
  15. cli_def-0.2.0/src/cli_def/core/models/generic/abstract_node.py +146 -0
  16. cli_def-0.2.0/src/cli_def/core/models/generic/argument_def.py +142 -0
  17. cli_def-0.2.0/src/cli_def/core/models/generic/cli_def.py +76 -0
  18. cli_def-0.2.0/src/cli_def/core/models/generic/command_def.py +81 -0
  19. cli_def-0.2.0/src/cli_def/core/models/generic/executable_node.py +57 -0
  20. cli_def-0.2.0/src/cli_def/core/models/protocols.py +151 -0
  21. {cli_def-0.1.0/src/cli_def/models → cli_def-0.2.0/src/cli_def/core/models/raw}/__init__.py +7 -8
  22. cli_def-0.2.0/src/cli_def/core/models/raw/argument_def.py +187 -0
  23. cli_def-0.2.0/src/cli_def/core/models/raw/cli_def.py +142 -0
  24. cli_def-0.2.0/src/cli_def/core/models/raw/command_def.py +161 -0
  25. cli_def-0.2.0/src/cli_def/core/models/raw/executable_node.py +61 -0
  26. cli_def-0.2.0/src/cli_def/core/models/raw/raw_node.py +142 -0
  27. cli_def-0.2.0/src/cli_def/core/models/raw/raw_protocols.py +96 -0
  28. cli_def-0.2.0/src/cli_def/core/models/resolved/__init__.py +16 -0
  29. cli_def-0.2.0/src/cli_def/core/models/resolved/argument_def.py +158 -0
  30. cli_def-0.2.0/src/cli_def/core/models/resolved/cli_def.py +79 -0
  31. cli_def-0.2.0/src/cli_def/core/models/resolved/command_def.py +89 -0
  32. cli_def-0.2.0/src/cli_def/core/models/resolved/executable_node.py +80 -0
  33. cli_def-0.2.0/src/cli_def/core/models/resolved/resolved_node.py +150 -0
  34. cli_def-0.2.0/src/cli_def/core/models/resolved/resolved_protocols.py +78 -0
  35. {cli_def-0.1.0/src/cli_def/parsers → cli_def-0.2.0/src/cli_def/core/parser}/__init__.py +1 -1
  36. cli_def-0.2.0/src/cli_def/core/parser/parser.py +137 -0
  37. cli_def-0.2.0/src/cli_def/core/resolver/__init__.py +10 -0
  38. cli_def-0.2.0/src/cli_def/core/resolver/resolver.py +247 -0
  39. cli_def-0.2.0/src/cli_def/core/validator/__init__.py +16 -0
  40. cli_def-0.2.0/src/cli_def/core/validator/validator.py +211 -0
  41. {cli_def-0.1.0/src/cli_def/script/handlers → cli_def-0.2.0/src/cli_def/demo}/dummy.py +3 -3
  42. {cli_def-0.1.0/src/cli_def/script/handlers → cli_def-0.2.0/src/cli_def/demo}/dummy2.py +2 -2
  43. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/demo/handlers.py +1 -1
  44. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/demo/profiles/beginner.toml +11 -1
  45. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/ops/dumper.py +92 -20
  46. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/ops/loader.py +3 -3
  47. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/ops/utils/pretty_renderer.py +336 -103
  48. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/ops/utils/renderer.py +43 -9
  49. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/ops/utils/renderer_ops.py +17 -0
  50. cli_def-0.2.0/src/cli_def/ops/utils/simple_pager.py +30 -0
  51. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/resources/cli_def.toml +12 -0
  52. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/runtime/dispatcher.py +24 -18
  53. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/runtime/event.py +2 -2
  54. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/runtime/runner.py +49 -17
  55. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/runtime/session.py +3 -4
  56. cli_def-0.2.0/src/cli_def/script/handlers/__init__.py +2 -0
  57. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/script/handlers/dump.py +39 -8
  58. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/script/handlers/run.py +2 -2
  59. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/script/handlers/scan.py +37 -52
  60. cli_def-0.2.0/src/cli_def/script/handlers/search.py +139 -0
  61. cli_def-0.2.0/src/cli_def/script/handlers/validate.py +120 -0
  62. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/script/main.py +5 -1
  63. cli_def-0.2.0/src/cli_def.egg-info/PKG-INFO +187 -0
  64. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def.egg-info/SOURCES.txt +38 -12
  65. cli_def-0.1.0/PKG-INFO +0 -400
  66. cli_def-0.1.0/README.md +0 -356
  67. cli_def-0.1.0/src/cli_def/__init__.py +0 -16
  68. cli_def-0.1.0/src/cli_def/models/abstract_node.py +0 -87
  69. cli_def-0.1.0/src/cli_def/models/argument_def.py +0 -84
  70. cli_def-0.1.0/src/cli_def/models/cli_def.py +0 -28
  71. cli_def-0.1.0/src/cli_def/models/command_def.py +0 -49
  72. cli_def-0.1.0/src/cli_def/models/mult_def.py +0 -81
  73. cli_def-0.1.0/src/cli_def/models/schema/cli_schema.py +0 -24
  74. cli_def-0.1.0/src/cli_def/parsers/parser.py +0 -101
  75. cli_def-0.1.0/src/cli_def/script/handlers/__init__.py +0 -14
  76. cli_def-0.1.0/src/cli_def/script/handlers/search.py +0 -116
  77. cli_def-0.1.0/src/cli_def.egg-info/PKG-INFO +0 -400
  78. {cli_def-0.1.0 → cli_def-0.2.0}/LICENSE +0 -0
  79. {cli_def-0.1.0 → cli_def-0.2.0}/setup.cfg +0 -0
  80. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/backend/__init__.py +0 -0
  81. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/backend/argparse/__init__.py +0 -0
  82. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/backend/click/__init__.py +0 -0
  83. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/backend/click/click_binder.py +0 -0
  84. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/demo/__init__.py +0 -0
  85. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/demo/profiles/advanced.toml +0 -0
  86. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/ops/__init__.py +0 -0
  87. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/ops/utils/__init__.py +0 -0
  88. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/runtime/__init__.py +0 -0
  89. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/runtime/_internal/__init__.py +0 -0
  90. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/runtime/_internal/scan_runner.py +0 -0
  91. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/runtime/context.py +0 -0
  92. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/runtime/handler_support.py +0 -0
  93. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/runtime/result.py +0 -0
  94. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/runtime/scanner.py +0 -0
  95. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/runtime/utils.py +0 -0
  96. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/script/__init__.py +0 -0
  97. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/script/common.py +0 -0
  98. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def/script/handlers/repl.py +0 -0
  99. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def.egg-info/dependency_links.txt +0 -0
  100. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def.egg-info/entry_points.txt +0 -0
  101. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def.egg-info/requires.txt +0 -0
  102. {cli_def-0.1.0 → cli_def-0.2.0}/src/cli_def.egg-info/top_level.txt +0 -0
cli_def-0.2.0/PKG-INFO ADDED
@@ -0,0 +1,187 @@
1
+ Metadata-Version: 2.4
2
+ Name: cli-def
3
+ Version: 0.2.0
4
+ Summary: A declarative DSL for composable CLI applications with runtime and REPL
5
+ Author: Tomesoft
6
+ License: MIT License
7
+
8
+ Copyright (c) 2026 Tomesoft
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to do so, subject to the
15
+ following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+ Project-URL: Homepage, https://cli-def.tomesoft.net
28
+ Project-URL: Repository, https://github.com/tomesoft/cli-def
29
+ Classifier: Programming Language :: Python :: 3
30
+ Classifier: Programming Language :: Python :: 3.10
31
+ Classifier: License :: OSI Approved :: MIT License
32
+ Classifier: Operating System :: OS Independent
33
+ Requires-Python: >=3.10
34
+ Description-Content-Type: text/markdown
35
+ License-File: LICENSE
36
+ Requires-Dist: wcwidth
37
+ Provides-Extra: click
38
+ Requires-Dist: click>=8.0; extra == "click"
39
+ Provides-Extra: repl
40
+ Requires-Dist: prompt_toolkit; extra == "repl"
41
+ Provides-Extra: dev
42
+ Requires-Dist: pytest; extra == "dev"
43
+ Dynamic: license-file
44
+
45
+ # cli-def
46
+
47
+ A declarative DSL for defining CLI structures with a unified runtime and REPL for argparse/click backends.
48
+
49
+ project page:
50
+ https://cli-def.tomesoft.net
51
+
52
+ source:
53
+ https://github.com/tomesoft/cli-def
54
+
55
+ ## What's new in 0.2.0
56
+
57
+ - Introduced Resolved model layer
58
+ - Added parameter binding (`bind`)
59
+ - Added `validate` command
60
+ - Improved inheritance (`inherit_from`)
61
+ - Added support for relative paths (`../`)
62
+ - Improved help and dump output
63
+ - Introduced `_early` parsing phase
64
+
65
+ ## ✨ Features
66
+
67
+ - Define CLI structure declaratively (TOML / Python models)
68
+ - Generate CLI implementations for:
69
+ - `argparse` (standard library, no dependencies)
70
+ - `click` (optional dependency)
71
+ - Separation of:
72
+ - CLI definition (DSL)
73
+ - Runtime implementation (builders)
74
+ - Built-in runtime system:
75
+ - Event model (`CliEvent`)
76
+ - Runner
77
+ - Dispatcher
78
+ - Entrypoint resolution (`module:function`)
79
+ - Result propagation
80
+ - Interactive REPL support
81
+ - CLI chaining (execute one CLI from another)
82
+
83
+ ## 📦 Installation
84
+
85
+ Core (argparse only)
86
+
87
+ ```bash
88
+ pip install cli-def
89
+ ```
90
+
91
+ With click support
92
+
93
+ ```bash
94
+ pip install cli-def[click]
95
+ ```
96
+
97
+ ## 🚀 Quick Example
98
+
99
+ ```toml
100
+ [cli]
101
+ key = "Your CLI"
102
+ help = "Help of your CLI"
103
+ prompt = "yourcli> "
104
+
105
+ [cli.echo]
106
+ args = [{key="message", mult="+"}]
107
+ ```
108
+
109
+ ```bash
110
+ cli-def run example.toml -- echo hello world
111
+
112
+ [run] forwarding args: ['echo', 'hello', 'world'], no_ctx_propagate: False
113
+ === fallback handler ===
114
+ PATH: ['Your CLI', 'echo']
115
+ PARAMS: {'message': ['hello', 'world']}
116
+ ```
117
+
118
+ ## 🚀 Quick Start (Demo)
119
+
120
+ ```bash
121
+ cli-def demo beginner
122
+ ```
123
+
124
+ beginner profile reads in beginner.toml (see GitHub repository for details)
125
+
126
+ ```text
127
+ Type 'help' to list commands, 'exit' to exit
128
+ demo[beginner]> echo a b c d
129
+ a b c d
130
+
131
+ -> out[0]
132
+ demo[beginner]> greet John --upper
133
+ HELLO, JOHN!
134
+
135
+ -> out[1]
136
+ ```
137
+
138
+ Advanced demo:
139
+
140
+ ```bash
141
+ cli-def demo advanced
142
+ ```
143
+
144
+ See profiles:
145
+ https://github.com/tomesoft/cli-def/tree/main/src/cli_def/demo/profiles
146
+
147
+ ## 🖥 Interactive Mode (REPL)
148
+
149
+ ```bash
150
+ cli-def repl --file your_cli.toml
151
+ yourcli> help
152
+ yourcli> command arg1 --option value
153
+ ```
154
+
155
+ ## 🔁 Run Another CLI (CLI chaining)
156
+
157
+ ```bash
158
+ cli-def run example.toml -- command arg1 arg2
159
+ ```
160
+
161
+ - `--` separates cli-def arguments from the target CLI arguments
162
+ - Remaining arguments are forwarded to the next CLI
163
+
164
+ ## 📚 Documentation
165
+
166
+ Full documentation and design details are available on GitHub:
167
+
168
+ https://github.com/tomesoft/cli-def
169
+
170
+ - DSL specification
171
+ - Architecture
172
+ - Examples
173
+ - Development notes
174
+
175
+ ## 📌 Roadmap
176
+
177
+ See TODO.md on GitHub:
178
+ https://github.com/tomesoft/cli-def/blob/main/TODO.md
179
+
180
+ ## 🤝 Contributing
181
+
182
+ Contributions are welcome!
183
+
184
+ ## 📄 License
185
+
186
+ MIT License
187
+
@@ -0,0 +1,143 @@
1
+ # cli-def
2
+
3
+ A declarative DSL for defining CLI structures with a unified runtime and REPL for argparse/click backends.
4
+
5
+ project page:
6
+ https://cli-def.tomesoft.net
7
+
8
+ source:
9
+ https://github.com/tomesoft/cli-def
10
+
11
+ ## What's new in 0.2.0
12
+
13
+ - Introduced Resolved model layer
14
+ - Added parameter binding (`bind`)
15
+ - Added `validate` command
16
+ - Improved inheritance (`inherit_from`)
17
+ - Added support for relative paths (`../`)
18
+ - Improved help and dump output
19
+ - Introduced `_early` parsing phase
20
+
21
+ ## ✨ Features
22
+
23
+ - Define CLI structure declaratively (TOML / Python models)
24
+ - Generate CLI implementations for:
25
+ - `argparse` (standard library, no dependencies)
26
+ - `click` (optional dependency)
27
+ - Separation of:
28
+ - CLI definition (DSL)
29
+ - Runtime implementation (builders)
30
+ - Built-in runtime system:
31
+ - Event model (`CliEvent`)
32
+ - Runner
33
+ - Dispatcher
34
+ - Entrypoint resolution (`module:function`)
35
+ - Result propagation
36
+ - Interactive REPL support
37
+ - CLI chaining (execute one CLI from another)
38
+
39
+ ## 📦 Installation
40
+
41
+ Core (argparse only)
42
+
43
+ ```bash
44
+ pip install cli-def
45
+ ```
46
+
47
+ With click support
48
+
49
+ ```bash
50
+ pip install cli-def[click]
51
+ ```
52
+
53
+ ## 🚀 Quick Example
54
+
55
+ ```toml
56
+ [cli]
57
+ key = "Your CLI"
58
+ help = "Help of your CLI"
59
+ prompt = "yourcli> "
60
+
61
+ [cli.echo]
62
+ args = [{key="message", mult="+"}]
63
+ ```
64
+
65
+ ```bash
66
+ cli-def run example.toml -- echo hello world
67
+
68
+ [run] forwarding args: ['echo', 'hello', 'world'], no_ctx_propagate: False
69
+ === fallback handler ===
70
+ PATH: ['Your CLI', 'echo']
71
+ PARAMS: {'message': ['hello', 'world']}
72
+ ```
73
+
74
+ ## 🚀 Quick Start (Demo)
75
+
76
+ ```bash
77
+ cli-def demo beginner
78
+ ```
79
+
80
+ beginner profile reads in beginner.toml (see GitHub repository for details)
81
+
82
+ ```text
83
+ Type 'help' to list commands, 'exit' to exit
84
+ demo[beginner]> echo a b c d
85
+ a b c d
86
+
87
+ -> out[0]
88
+ demo[beginner]> greet John --upper
89
+ HELLO, JOHN!
90
+
91
+ -> out[1]
92
+ ```
93
+
94
+ Advanced demo:
95
+
96
+ ```bash
97
+ cli-def demo advanced
98
+ ```
99
+
100
+ See profiles:
101
+ https://github.com/tomesoft/cli-def/tree/main/src/cli_def/demo/profiles
102
+
103
+ ## 🖥 Interactive Mode (REPL)
104
+
105
+ ```bash
106
+ cli-def repl --file your_cli.toml
107
+ yourcli> help
108
+ yourcli> command arg1 --option value
109
+ ```
110
+
111
+ ## 🔁 Run Another CLI (CLI chaining)
112
+
113
+ ```bash
114
+ cli-def run example.toml -- command arg1 arg2
115
+ ```
116
+
117
+ - `--` separates cli-def arguments from the target CLI arguments
118
+ - Remaining arguments are forwarded to the next CLI
119
+
120
+ ## 📚 Documentation
121
+
122
+ Full documentation and design details are available on GitHub:
123
+
124
+ https://github.com/tomesoft/cli-def
125
+
126
+ - DSL specification
127
+ - Architecture
128
+ - Examples
129
+ - Development notes
130
+
131
+ ## 📌 Roadmap
132
+
133
+ See TODO.md on GitHub:
134
+ https://github.com/tomesoft/cli-def/blob/main/TODO.md
135
+
136
+ ## 🤝 Contributing
137
+
138
+ Contributions are welcome!
139
+
140
+ ## 📄 License
141
+
142
+ MIT License
143
+
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "cli-def"
3
- version = "0.1.0"
3
+ version = "0.2.0"
4
4
  description = "A declarative DSL for composable CLI applications with runtime and REPL"
5
5
  readme = {file = "README.md", content-type = "text/markdown"}
6
6
  requires-python = ">=3.10"
@@ -0,0 +1,12 @@
1
+ # cli_def/init.py
2
+ # package marker
3
+
4
+ from .core.models import (
5
+ CliDef,
6
+ )
7
+ from .core.parser import CliDefParser
8
+
9
+ __all__ = [
10
+ "CliDef",
11
+ "CliDefParser",
12
+ ]
@@ -3,12 +3,18 @@ from typing import Iterable, Sequence, Any, Mapping
3
3
  import argparse
4
4
  import logging
5
5
 
6
- from ...models import (
6
+ from ...core.models import (
7
7
  CliDefNode,
8
8
  CliDef,
9
- CommandDef,
10
- ExecutableNode,
11
9
  ArgumentDef,
10
+ CommandDef,
11
+ )
12
+ from ...core.models import (
13
+ ResolvedCliDefNode,
14
+ ResolvedCliDef,
15
+ ResolvedCommandDef,
16
+ ResolvedExecutableNode,
17
+ ResolvedArgumentDef,
12
18
  MultDef,
13
19
  )
14
20
 
@@ -34,23 +40,21 @@ class ArgparseBuilder(BuilderProtocol):
34
40
  return self._defpath_mapping
35
41
 
36
42
 
37
- def build(self, cliDef: CliDef) -> argparse.ArgumentParser:
43
+ def build(self, cliDef: ResolvedCliDef) -> argparse.ArgumentParser:
38
44
  return self.build_argparse(cliDef)
39
45
 
40
46
 
41
- def _register(self, node: CliDefNode, obj: Any):
47
+ def _register(self, node: ResolvedCliDefNode, obj: Any):
42
48
  self._defpath_mapping[node.defpath] = obj
43
49
 
44
50
 
45
- def _register_subparsers(self, node: CliDefNode, obj: Any):
51
+ def _register_subparsers(self, node: ResolvedCliDefNode, obj: Any):
46
52
  special_defpath = "/".join([node.defpath, "__subparsers__"])
47
53
  self._defpath_mapping[special_defpath] = obj
48
54
 
49
55
 
50
56
  def build_early_argparse(self, cliDef: CliDef, prog: str|None = None) -> argparse.ArgumentParser|None:
51
57
  early_node = cliDef.select_first(lambda n: n.key == CommandDef.EARLY)
52
- if early_node is None:
53
- return None
54
58
 
55
59
  parser = argparse.ArgumentParser(
56
60
  prog=prog or cliDef.key,
@@ -61,7 +65,7 @@ class ArgparseBuilder(BuilderProtocol):
61
65
  return parser
62
66
 
63
67
 
64
- def build_argparse(self, cliDef: CliDef, prog: str|None = None) -> argparse.ArgumentParser:
68
+ def build_argparse(self, cliDef: ResolvedCliDef, prog: str|None = None) -> argparse.ArgumentParser:
65
69
  parser = argparse.ArgumentParser(
66
70
  prog=prog or cliDef.key
67
71
  )
@@ -79,13 +83,16 @@ class ArgparseBuilder(BuilderProtocol):
79
83
 
80
84
  def build_arguments(
81
85
  self,
82
- argumentDefs: Iterable[ArgumentDef],
86
+ argumentDefs: Iterable[ResolvedArgumentDef]|Iterable[ArgumentDef],
83
87
  parser: argparse.ArgumentParser,
84
88
  ) -> Iterable[argparse.Action] | None:
85
89
  if argumentDefs is None:
86
90
  return None
87
91
  actions = []
88
92
  for argDef in argumentDefs:
93
+ if hasattr(argDef, "has_bound_value") and getattr(argDef, "has_bound_value"):
94
+ continue # skip bound parameters
95
+
89
96
  logging.debug(f"{argDef.defpath} mult={argDef.mult} nargs={self.to_nargs(argDef.mult)}")
90
97
  if argDef.is_positional:
91
98
  # positional paremter
@@ -122,32 +129,22 @@ class ArgparseBuilder(BuilderProtocol):
122
129
  )
123
130
 
124
131
  actions.append(action)
125
- self._register(argDef, action)
132
+ if isinstance(argDef, ResolvedArgumentDef):
133
+ self._register(argDef, action)
126
134
 
127
135
  return actions
128
136
 
129
137
 
130
138
  def build_commands(
131
139
  self,
132
- commandDefs: Sequence[CommandDef],
140
+ commandDefs: Sequence[ResolvedCommandDef],
133
141
  parser: argparse.ArgumentParser,
134
142
  ) -> Iterable[argparse.ArgumentParser] | None:
135
143
  if commandDefs is None or len(commandDefs) == 0:
136
144
  return None
137
145
 
138
- # build parent parsers from defs of templates
139
- parent_parsers_map = {}
140
- for cmdDef in commandDefs:
141
- if not cmdDef.is_template or cmdDef.key == CommandDef.EARLY:
142
- continue
143
- cmd_templ_parser = self.build_single_command(cmdDef)
144
- parent_parsers_map[cmdDef.key] = cmd_templ_parser
145
- if cmdDef.key == CommandDef.EARLY:
146
- #print(f"@@@ early_parser found :{cmdDef.key}")
147
- self._early_parser = cmd_templ_parser
148
-
149
- firstCmdDef: CommandDef = commandDefs[0]
150
- assert isinstance(firstCmdDef.parent, ExecutableNode)
146
+ firstCmdDef: ResolvedCommandDef = commandDefs[0]
147
+ assert isinstance(firstCmdDef.parent, ResolvedExecutableNode)
151
148
  group = (firstCmdDef.parent.group or
152
149
  ("sub" * firstCmdDef.parent.deflevel) + "command"
153
150
  )
@@ -156,21 +153,7 @@ class ArgparseBuilder(BuilderProtocol):
156
153
  self._register_subparsers(firstCmdDef.parent, subparsers)
157
154
  cmd_parsers = []
158
155
  for cmdDef in commandDefs:
159
- if cmdDef.is_template or cmdDef.key == CommandDef.EARLY:
160
- continue
161
- # select parent_parsers
162
- parent_parsers = []
163
- for tmpl in cmdDef.get_templates():
164
- if tmpl.key in parent_parsers_map:
165
- #print(f"@@@ parent_parser :{tmpl.key}")
166
- parent_parsers.append(parent_parsers_map[tmpl.key])
167
- # # TODO consider forcibly add _early to parents
168
- # #print(f"@@@ self_early_parser = {"None" if self._early_parser is None else "Not None"}")
169
- # if self._early_parser and self._early_parser not in parent_parsers:
170
- # #print(f"@@@ add _early parser to {cmdDef.defpath}")
171
- # parent_parsers.append(self._early_parser)
172
-
173
- cmd_parser = self.build_single_command(cmdDef, subparsers, parent_parsers)
156
+ cmd_parser = self.build_single_command(cmdDef, subparsers)
174
157
  cmd_parsers.append(cmd_parser)
175
158
 
176
159
  return cmd_parsers
@@ -178,7 +161,7 @@ class ArgparseBuilder(BuilderProtocol):
178
161
 
179
162
  def build_single_command(
180
163
  self,
181
- cmdDef: CommandDef,
164
+ cmdDef: ResolvedCommandDef,
182
165
  subparsers: argparse._SubParsersAction|None = None,
183
166
  parent_parsers: Sequence[argparse.ArgumentParser]|None = None
184
167
  ) -> argparse.ArgumentParser:
@@ -191,14 +174,15 @@ class ArgparseBuilder(BuilderProtocol):
191
174
  parents=parent_parsers,
192
175
  )
193
176
  else:
194
- kwargs = ({"add_help":True, "help":cmdDef.help}
195
- if cmdDef.help is not None else
196
- {"add_help":False}
197
- )
177
+ # kwargs = ({"add_help":True:cmdDef.help}
178
+ # if cmdDef.help is not None else
179
+ # {"add_help":False}
180
+ # )
198
181
  parser = argparse.ArgumentParser(
199
182
  cmdDef.key,
200
183
  parents=parent_parsers,
201
- **kwargs
184
+ add_help=True if cmdDef.help is not None else False,
185
+ # **kwargs
202
186
  )
203
187
  self.build_arguments(cmdDef.arguments, parser)
204
188
  if cmdDef.subcommands:
@@ -210,10 +194,10 @@ class ArgparseBuilder(BuilderProtocol):
210
194
  return parser
211
195
 
212
196
 
213
- def _attach_metadata(self, parser, command: CommandDef|CliDef, path):
197
+ def _attach_metadata(self, parser, command: ResolvedCommandDef|ResolvedCliDef, path):
214
198
  parser.set_defaults(
215
199
  _path=path,
216
- _command=command, # ← これも入れる(重要)
200
+ _command=command,
217
201
  )
218
202
 
219
203
 
@@ -1,5 +1,5 @@
1
1
  # cli_def/backend/click/click_builder.py
2
- from typing import Any, Mapping, Iterable
2
+ from typing import Any, Mapping, Iterable, Sequence
3
3
  try:
4
4
  import click
5
5
  except ImportError:
@@ -7,11 +7,12 @@ except ImportError:
7
7
  "click is required for click builder. Install with `cli-def[click]`"
8
8
  )
9
9
 
10
- from ...models import (
11
- CliDefNode,
12
- CliDef,
13
- CommandDef,
14
- ArgumentDef,
10
+ from ...core.models import (
11
+ ResolvedCliDefNode,
12
+ ResolvedCliDef,
13
+ ResolvedCommandDef,
14
+ ResolvedExecutableNode,
15
+ ResolvedArgumentDef,
15
16
  MultDef
16
17
  )
17
18
 
@@ -42,7 +43,7 @@ class ClickBuilder(BuilderProtocol):
42
43
  return self._defpath_mapping
43
44
 
44
45
 
45
- def build(self, cliDef: CliDef) -> GroupOrCommand:
46
+ def build(self, cliDef: ResolvedCliDef) -> GroupOrCommand:
46
47
  return self.build_click(cliDef)
47
48
 
48
49
 
@@ -50,14 +51,14 @@ class ClickBuilder(BuilderProtocol):
50
51
  self._defpath_mapping[node.defpath] = obj
51
52
 
52
53
 
53
- def build_click(self, cliDef: CliDef) -> GroupOrCommand:
54
+ def build_click(self, cliDef: ResolvedCliDef) -> GroupOrCommand:
54
55
  if not cliDef.commands:
55
56
  return self._build_root_command(cliDef)
56
57
 
57
58
  return self._build_root_group(cliDef)
58
59
 
59
60
 
60
- def _build_root_command(self, cliDef: CliDef) -> click.Command:
61
+ def _build_root_command(self, cliDef: ResolvedCliDef) -> click.Command:
61
62
  params = [self._build_param(arg) for arg in cliDef.arguments]
62
63
 
63
64
  def callback(**kwargs):
@@ -79,26 +80,25 @@ class ClickBuilder(BuilderProtocol):
79
80
  return cmd
80
81
 
81
82
 
82
- def _build_root_group(self, cliDef: CliDef) -> click.Group:
83
+ def _build_root_group(self, cliDef: ResolvedCliDef) -> click.Group:
83
84
  root = click.Group(name=cliDef.key, help=cliDef.help)
84
85
 
85
86
  self._register(cliDef, root)
86
87
 
87
- # 引数
88
88
  self._attach_params(root, cliDef.arguments)
89
89
 
90
- # コマンド
91
90
  if cliDef.commands:
92
91
  self._attach_commands(root, cliDef.commands)
93
92
 
94
93
  return root
95
94
 
96
95
 
97
- def _build_command(self, cmdDef: CommandDef) -> click.Command:
98
- params = [self._build_param(arg) for arg in cmdDef.arguments]
99
- # collect arguments from template
100
- for tmpl_cmd in cmdDef.get_templates():
101
- params.extend([self._build_param(arg) for arg in tmpl_cmd.arguments])
96
+ def _build_command(self, cmdDef: ResolvedCommandDef) -> click.Command:
97
+ params = [
98
+ self._build_param(arg)
99
+ for arg in cmdDef.arguments
100
+ if not arg.has_bound_value
101
+ ]
102
102
 
103
103
  if cmdDef.subcommands:
104
104
  grp = click.Group(name=cmdDef.key, help=cmdDef.help, params=params)
@@ -119,7 +119,6 @@ class ClickBuilder(BuilderProtocol):
119
119
  help=cmdDef.help,
120
120
  params=params,
121
121
  callback=callback,
122
- hidden=cmdDef.is_template,
123
122
  context_settings={
124
123
  "ignore_unknown_options": True,
125
124
  "allow_extra_args": True,
@@ -131,24 +130,24 @@ class ClickBuilder(BuilderProtocol):
131
130
  return cmd
132
131
 
133
132
 
134
- def _build_param(self, arg: ArgumentDef) -> click.Parameter:
133
+ def _build_param(self, arg: ResolvedArgumentDef) -> click.Parameter:
135
134
  if arg.option:
136
135
  # option
137
136
  param = click.Option(
138
- #[arg.option, arg.key], # works but ...
139
- [arg.option] + (arg.aliases or []),
137
+ [arg.option] + (list(arg.aliases) or []),
140
138
  help=arg.help,
141
139
  default=arg.default,
142
140
  is_flag=arg.is_flag or False,
143
141
  )
144
142
  param.name = arg.key
143
+ self.apply_mult(param, arg.mult)
145
144
  else:
146
145
  # positional
146
+ kwargs = self.to_kwargs_for_positional(arg.mult)
147
147
  param = click.Argument(
148
148
  param_decls=[arg.key],
149
- #nargs=self.to_nargs(arg.mult),
149
+ **kwargs
150
150
  )
151
- self.apply_mult(param, arg.mult)
152
151
 
153
152
  self._register(arg, param)
154
153
 
@@ -158,7 +157,7 @@ class ClickBuilder(BuilderProtocol):
158
157
  def _attach_commands(
159
158
  self,
160
159
  parent: click.Group,
161
- commands: Iterable[CommandDef]
160
+ commands: Iterable[ResolvedCommandDef]
162
161
  ):
163
162
  for cmd in commands or []:
164
163
  parent.add_command(self._build_command(cmd))
@@ -167,9 +166,11 @@ class ClickBuilder(BuilderProtocol):
167
166
  def _attach_params(
168
167
  self,
169
168
  cmd: click.Command,
170
- arguments: Iterable[ArgumentDef]
169
+ arguments: Iterable[ResolvedArgumentDef]
171
170
  ):
172
171
  for arg in arguments or []:
172
+ if arg.has_bound_value:
173
+ continue
173
174
  cmd.params.append(self._build_param(arg))
174
175
 
175
176
 
@@ -180,3 +181,12 @@ class ClickBuilder(BuilderProtocol):
180
181
  param.multiple = True
181
182
  elif mult.is_optional:
182
183
  param.nargs = 1
184
+
185
+ def to_kwargs_for_positional(self, mult: MultDef) -> dict[str, Any]:
186
+ if mult.is_fixed:
187
+ return {"nargs": mult.lower, "required": True}
188
+ elif mult.is_optional:
189
+ return {"nargs": 1, "required": False}
190
+ elif mult.is_unbounded:
191
+ return {"nargs": -1, "required": mult.lower != 0}
192
+ return {"nargs": 1} # TODO
@@ -2,7 +2,7 @@
2
2
 
3
3
  from typing import Protocol, Mapping
4
4
 
5
- from ..models import CliDef
5
+ from ..core.models import ResolvedCliDef
6
6
 
7
7
 
8
8
  class BuilderProtocol(Protocol):
@@ -11,7 +11,7 @@ class BuilderProtocol(Protocol):
11
11
  def defpath_mapping(self) -> Mapping[str, object]:
12
12
  ...
13
13
 
14
- def build(self, cliDef: CliDef) -> object:
14
+ def build(self, cliDef: ResolvedCliDef) -> object:
15
15
  ...
16
16
 
17
17