typedargs 0.1.0
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.
- checksums.yaml +7 -0
- data/LICENSE +201 -0
- data/README.md +324 -0
- data/lib/typedargs.rb +6 -0
- data/mrblib/alias.rb +26 -0
- data/mrblib/impl.rb +178 -0
- data/mrblib/key_parser.rb +108 -0
- data/mrblib/lexer.rb +212 -0
- data/mrblib/script_check.rb +62 -0
- data/mrblib/token.rb +12 -0
- data/mrblib/typedargs.rb +35 -0
- data/mrblib/utf8.rb +46 -0
- data/mrblib/value_parser.rb +116 -0
- metadata +52 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: f905abf1feb54c54d778f3e7239251181f2945bfd8347207f8d7a1e4bb093c81
|
|
4
|
+
data.tar.gz: 2ecbbddd89b00e902fcf7963628831d6a16f5cdf2ea7e5aec52d2e7b164470eb
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 4edb87ba2b567ff67af4ef5a86e4a9d3318c181b2c326e0f99c40424980f0634311e039dff710c92f884eb171032313e019d47910ddb71b41c694cc32b97ae21
|
|
7
|
+
data.tar.gz: 41f77dafb3c330addda8b5b6189311f391eed4cdf069b5c13d1f8b2dadfd3ad20b523c6fe76229e4930dd5e5c4840c2301472867ea15cc61ae98b067ce3bfef8
|
data/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
data/README.md
ADDED
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
# TypedArgs
|
|
2
|
+
*A tiny operator‑typed CLI language for structured data.*
|
|
3
|
+
|
|
4
|
+
TypedArgs is not an option parser.
|
|
5
|
+
It is a **mini‑language** for expressing structured data on the command line — scalars, arrays, hashes, and arrays of hashes — using a small set of explicit, shell‑safe operators.
|
|
6
|
+
|
|
7
|
+
It runs anywhere MRuby runs: embedded systems, containers, CI runners, Windows, macOS, Linux, BusyBox, Alpine, and fully sandboxed MRuby VMs. No dependencies. No shell tricks. No heuristics. No guessing.
|
|
8
|
+
|
|
9
|
+
TypedArgs behaves the same everywhere.
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
# Why TypedArgs Exists
|
|
14
|
+
|
|
15
|
+
Most CLI parsers try to *guess* what the user meant. TypedArgs refuses.
|
|
16
|
+
Shells are inconsistent. Quoting rules differ. JSON on the command line is painful.
|
|
17
|
+
Suffix‑typed flags collide with shells. YAML is too heavy.
|
|
18
|
+
Users deserve a grammar that is:
|
|
19
|
+
|
|
20
|
+
- **Explicit** — the operator defines the shape
|
|
21
|
+
- **Portable** — works in every shell without quoting
|
|
22
|
+
- **Deterministic** — same input, same output, always
|
|
23
|
+
- **Minimal** — four operators, one mental model
|
|
24
|
+
- **Structured** — arrays and hashes are first‑class citizens
|
|
25
|
+
|
|
26
|
+
TypedArgs is the answer: a tiny algebra of flags.
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
# The Operator Model
|
|
31
|
+
|
|
32
|
+
TypedArgs is built on four operators.
|
|
33
|
+
They define the shape of the value — nothing else is needed.
|
|
34
|
+
|
|
35
|
+
| Operator | Meaning |
|
|
36
|
+
|----------|---------|
|
|
37
|
+
| `=` | scalar assignment |
|
|
38
|
+
| `+=` | append scalar to array |
|
|
39
|
+
| `:fields:=` | assign hash tuple |
|
|
40
|
+
| `+:fields:=` | append hash tuple to array |
|
|
41
|
+
|
|
42
|
+
This is the entire language.
|
|
43
|
+
|
|
44
|
+
No suffixes.
|
|
45
|
+
No brackets.
|
|
46
|
+
No type inference.
|
|
47
|
+
No shell‑sensitive characters.
|
|
48
|
+
Just operators.
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
# Installation
|
|
53
|
+
|
|
54
|
+
TypedArgs is pure Ruby and MRuby‑core‑friendly.
|
|
55
|
+
Drop the Ruby files into your MRuby build or load them into your VM.
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
# Basic Usage
|
|
60
|
+
|
|
61
|
+
```ruby
|
|
62
|
+
args = TypedArgs.opts("--mode=fast", "--debug=true")
|
|
63
|
+
|
|
64
|
+
args["mode"] # => "fast"
|
|
65
|
+
args["debug"] # => true
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
If no arguments are passed, `TypedArgs.opts` defaults to `ARGV`.
|
|
69
|
+
You must supply that array yourself in MRuby; see `tools/typedargs_test/test.c` for an example.
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
# Grammar Overview
|
|
74
|
+
|
|
75
|
+
TypedArgs defines a small, explicit grammar for keys and values.
|
|
76
|
+
Everything is driven by operators.
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
## Scalars
|
|
81
|
+
|
|
82
|
+
```
|
|
83
|
+
--mode=fast
|
|
84
|
+
--count=5
|
|
85
|
+
--debug=true
|
|
86
|
+
--foo=nil
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Values may be:
|
|
90
|
+
|
|
91
|
+
- strings
|
|
92
|
+
- integers
|
|
93
|
+
- floats
|
|
94
|
+
- booleans (`true` / `false`)
|
|
95
|
+
- `nil`
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## Dotted Keys
|
|
100
|
+
|
|
101
|
+
```
|
|
102
|
+
--db.user=root
|
|
103
|
+
--cache.redis.host=localhost
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Keys may contain:
|
|
107
|
+
|
|
108
|
+
- letters
|
|
109
|
+
- digits
|
|
110
|
+
- underscore
|
|
111
|
+
- dash
|
|
112
|
+
- dot
|
|
113
|
+
|
|
114
|
+
Keys may **not** start with a digit or dash.
|
|
115
|
+
Dotted keys are treated as **flat strings**, not nested hashes.
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
## Arrays (`+=`)
|
|
120
|
+
|
|
121
|
+
```
|
|
122
|
+
--item+=a
|
|
123
|
+
--item+=b
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Result:
|
|
127
|
+
|
|
128
|
+
```ruby
|
|
129
|
+
{ "item" => ["a", "b"] }
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
`+=` always appends.
|
|
133
|
+
If the key didn’t exist, an array is created.
|
|
134
|
+
|
|
135
|
+
---
|
|
136
|
+
|
|
137
|
+
## Hash Tuples (`:fields:=`)
|
|
138
|
+
|
|
139
|
+
```
|
|
140
|
+
--range:min,max:=5,10
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Result:
|
|
144
|
+
|
|
145
|
+
```ruby
|
|
146
|
+
{ "range" => { "min" => 5, "max" => 10 } }
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
Arity is enforced:
|
|
150
|
+
If you declare two fields, you must supply two values.
|
|
151
|
+
|
|
152
|
+
---
|
|
153
|
+
|
|
154
|
+
## Arrays of Hashes (`+:fields:=`)
|
|
155
|
+
|
|
156
|
+
```
|
|
157
|
+
--servers+:name,port:=alpha,80
|
|
158
|
+
--servers+:name,port:=beta,443
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Result:
|
|
162
|
+
|
|
163
|
+
```ruby
|
|
164
|
+
{
|
|
165
|
+
"servers" => [
|
|
166
|
+
{ "name" => "alpha", "port" => 80 },
|
|
167
|
+
{ "name" => "beta", "port" => 443 }
|
|
168
|
+
]
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
`+:` always appends a hash to an array.
|
|
173
|
+
|
|
174
|
+
---
|
|
175
|
+
|
|
176
|
+
## Short‑Flag Aliases
|
|
177
|
+
|
|
178
|
+
```ruby
|
|
179
|
+
TypedArgs.alias("-v", "--verbose")
|
|
180
|
+
TypedArgs.opts("-v")
|
|
181
|
+
# => { "verbose" => true }
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
Aliases expand before parsing.
|
|
185
|
+
They can target dotted keys and any operator form.
|
|
186
|
+
|
|
187
|
+
---
|
|
188
|
+
|
|
189
|
+
# Error Reporting
|
|
190
|
+
|
|
191
|
+
TypedArgs provides compiler‑style diagnostics with caret indicators.
|
|
192
|
+
|
|
193
|
+
Example:
|
|
194
|
+
|
|
195
|
+
```
|
|
196
|
+
--range:min,max:=5
|
|
197
|
+
^
|
|
198
|
+
Syntax error: Arity mismatch: expected 2, got 1
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Every error includes:
|
|
202
|
+
|
|
203
|
+
- the original argument
|
|
204
|
+
- a caret pointing to the exact byte
|
|
205
|
+
- a clear error class
|
|
206
|
+
|
|
207
|
+
TypedArgs is self‑teaching.
|
|
208
|
+
|
|
209
|
+
---
|
|
210
|
+
|
|
211
|
+
# Operator Semantics
|
|
212
|
+
|
|
213
|
+
TypedArgs applies flags **in order**.
|
|
214
|
+
Later flags overwrite earlier ones unless using accumulation operators.
|
|
215
|
+
|
|
216
|
+
---
|
|
217
|
+
|
|
218
|
+
## Scalar Assignment (`=`)
|
|
219
|
+
|
|
220
|
+
| Syntax | Meaning |
|
|
221
|
+
|--------|---------|
|
|
222
|
+
| `--key=value` | assign scalar |
|
|
223
|
+
|
|
224
|
+
Overwrites previous value.
|
|
225
|
+
|
|
226
|
+
---
|
|
227
|
+
|
|
228
|
+
## Scalar Accumulation (`+=`)
|
|
229
|
+
|
|
230
|
+
| Syntax | Meaning |
|
|
231
|
+
|--------|---------|
|
|
232
|
+
| `--key+=value` | append scalar to array |
|
|
233
|
+
|
|
234
|
+
Creates array if missing.
|
|
235
|
+
Overwrites previous non‑array values.
|
|
236
|
+
|
|
237
|
+
---
|
|
238
|
+
|
|
239
|
+
## Hash Tuple Assignment (`:fields:=`)
|
|
240
|
+
|
|
241
|
+
| Syntax | Meaning |
|
|
242
|
+
|--------|---------|
|
|
243
|
+
| `--key:field1,field2:=v1,v2` | assign hash |
|
|
244
|
+
|
|
245
|
+
Overwrites previous value.
|
|
246
|
+
|
|
247
|
+
---
|
|
248
|
+
|
|
249
|
+
## Array of Hashes (`+:fields:=`)
|
|
250
|
+
|
|
251
|
+
| Syntax | Meaning |
|
|
252
|
+
|--------|---------|
|
|
253
|
+
| `--key+:field1,field2:=v1,v2` | append hash to array |
|
|
254
|
+
|
|
255
|
+
Creates array if missing.
|
|
256
|
+
|
|
257
|
+
---
|
|
258
|
+
|
|
259
|
+
# Sequential Override Rules
|
|
260
|
+
|
|
261
|
+
| Sequence | Result |
|
|
262
|
+
|----------|--------|
|
|
263
|
+
| `--foo=1` → `--foo+=2` | `[2]` |
|
|
264
|
+
| `--foo+=1` → `--foo+=2` | `[1,2]` |
|
|
265
|
+
| `--foo:min,max:=1,2` → `--foo:min,max:=3,4` | `{ "min"=>3,"max"=>4 }` |
|
|
266
|
+
| `--foo+:min,max:=1,2` → `--foo+:min,max:=3,4` | `[{"min"=>1,"max"=>2},{"min"=>3,"max"=>4}]` |
|
|
267
|
+
| `--foo=1` → `--foo+=2` → `--foo:name:=alpha` → `--foo=bar` | `"bar"` |
|
|
268
|
+
|
|
269
|
+
TypedArgs is explicit:
|
|
270
|
+
the operator determines the shape.
|
|
271
|
+
|
|
272
|
+
---
|
|
273
|
+
|
|
274
|
+
# Conformance Suite
|
|
275
|
+
|
|
276
|
+
TypedArgs ships with a full conformance suite covering:
|
|
277
|
+
|
|
278
|
+
- scalars
|
|
279
|
+
- arrays
|
|
280
|
+
- hashes
|
|
281
|
+
- arrays of hashes
|
|
282
|
+
- dotted keys
|
|
283
|
+
- alias expansion
|
|
284
|
+
- invalid characters
|
|
285
|
+
- invalid suffix placement
|
|
286
|
+
- invalid field lists
|
|
287
|
+
- tuple arity
|
|
288
|
+
- invalid numbers
|
|
289
|
+
- unterminated strings
|
|
290
|
+
- invalid short flags
|
|
291
|
+
- invalid dotted paths
|
|
292
|
+
- empty keys
|
|
293
|
+
- alias expansion to invalid keys
|
|
294
|
+
|
|
295
|
+
The suite **is the specification**.
|
|
296
|
+
If an implementation passes the suite, it is TypedArgs.
|
|
297
|
+
|
|
298
|
+
---
|
|
299
|
+
|
|
300
|
+
# Design Philosophy
|
|
301
|
+
|
|
302
|
+
TypedArgs is intentionally:
|
|
303
|
+
|
|
304
|
+
- **Explicit** — no guessing
|
|
305
|
+
- **Portable** — no shell dependencies
|
|
306
|
+
- **Minimal** — four operators, one grammar
|
|
307
|
+
- **Deterministic** — predictable and stable
|
|
308
|
+
- **Structured** — arrays and hashes are first‑class
|
|
309
|
+
|
|
310
|
+
TypedArgs does **not** depend on:
|
|
311
|
+
|
|
312
|
+
- shell brace expansion
|
|
313
|
+
- shell quoting rules
|
|
314
|
+
- environment‑specific behavior
|
|
315
|
+
- Bash‑only features
|
|
316
|
+
|
|
317
|
+
The shell’s only job is to pass raw strings.
|
|
318
|
+
TypedArgs does everything else.
|
|
319
|
+
|
|
320
|
+
---
|
|
321
|
+
|
|
322
|
+
# License
|
|
323
|
+
|
|
324
|
+
Apache‑2
|
data/lib/typedargs.rb
ADDED
data/mrblib/alias.rb
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
module TypedArgs
|
|
2
|
+
module Internal
|
|
3
|
+
@alias_map = {}
|
|
4
|
+
|
|
5
|
+
class << self
|
|
6
|
+
def register_alias(short, long)
|
|
7
|
+
@alias_map[short] = long
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def resolve_name(raw)
|
|
11
|
+
mapped = @alias_map[raw]
|
|
12
|
+
mapped ? strip_leading_dashes(mapped) : strip_leading_dashes(raw)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
private
|
|
16
|
+
def strip_leading_dashes(str)
|
|
17
|
+
i = 0
|
|
18
|
+
n = str.bytesize
|
|
19
|
+
while i < n && str[i,1] == "-"
|
|
20
|
+
i += 1
|
|
21
|
+
end
|
|
22
|
+
str[i, n - i]
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
data/mrblib/impl.rb
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
module TypedArgs
|
|
2
|
+
module Internal
|
|
3
|
+
module Impl
|
|
4
|
+
class << self
|
|
5
|
+
def parse(argv)
|
|
6
|
+
out = {}
|
|
7
|
+
i = 0
|
|
8
|
+
n = argv.size
|
|
9
|
+
while i < n
|
|
10
|
+
a = argv[i]
|
|
11
|
+
if a && a.length > 0
|
|
12
|
+
if long_flag?(a)
|
|
13
|
+
parse_long(out, a)
|
|
14
|
+
elsif short_flag?(a)
|
|
15
|
+
parse_short(out, a)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
i += 1
|
|
19
|
+
end
|
|
20
|
+
out
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
def long_flag?(arg)
|
|
25
|
+
arg.length >= 2 &&
|
|
26
|
+
arg[0,1] == "-" &&
|
|
27
|
+
arg[1,1] == "-"
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def short_flag?(arg)
|
|
31
|
+
arg.length >= 1 &&
|
|
32
|
+
arg[0,1] == "-" &&
|
|
33
|
+
!(arg.length >= 2 && arg[1,1] == "-")
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
# In impl.rb, replace parse_long or parse_long-like logic with:
|
|
38
|
+
|
|
39
|
+
def parse_long(out, arg)
|
|
40
|
+
# body as character substring (character-mode)
|
|
41
|
+
body = arg[2, arg.length - 2] # "--" removed, character-based
|
|
42
|
+
|
|
43
|
+
# find '=' in character mode
|
|
44
|
+
eq_idx = body.index("=")
|
|
45
|
+
|
|
46
|
+
if eq_idx
|
|
47
|
+
key_str = body[0, eq_idx] # character substring for key
|
|
48
|
+
val_str = body[(eq_idx + 1), body.length - (eq_idx + 1)] # character substring for value
|
|
49
|
+
else
|
|
50
|
+
key_str = body
|
|
51
|
+
val_str = nil
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# parse key in character mode
|
|
55
|
+
key_lex = Lexer.new(key_str, 0, key_str.length, true)
|
|
56
|
+
key_ast = KeyParser.new(key_lex).parse
|
|
57
|
+
|
|
58
|
+
name = Internal.resolve_name(key_ast[:name])
|
|
59
|
+
|
|
60
|
+
# script check (character indices)
|
|
61
|
+
ScriptCheck.validate_key(key_str)
|
|
62
|
+
|
|
63
|
+
if val_str
|
|
64
|
+
# parse value in character mode (value lexer now also character-based)
|
|
65
|
+
val_lex = Lexer.new(val_str, 0, val_str.length, false)
|
|
66
|
+
vp = ValueParser.new(val_lex)
|
|
67
|
+
|
|
68
|
+
case key_ast[:kind]
|
|
69
|
+
when :scalar, :array_scalar
|
|
70
|
+
value = vp.parse_scalar
|
|
71
|
+
when :hash, :array_hash
|
|
72
|
+
tuple = vp.parse_tuple(key_ast[:fields].size)
|
|
73
|
+
value = build_hash(key_ast[:fields], tuple)
|
|
74
|
+
end
|
|
75
|
+
else
|
|
76
|
+
value = true
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
assign(out, name, key_ast, value)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def parse_short(out, arg)
|
|
84
|
+
raw = arg[0,2]
|
|
85
|
+
name = Internal.resolve_name(raw)
|
|
86
|
+
|
|
87
|
+
if name.nil? || name.length == 0
|
|
88
|
+
raise InvalidKeyStartError.new(
|
|
89
|
+
"Invalid key start",
|
|
90
|
+
1,
|
|
91
|
+
arg
|
|
92
|
+
)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# first character validation (character-mode)
|
|
96
|
+
c0 = name[0,1]
|
|
97
|
+
unless c0 == "_" ||
|
|
98
|
+
(c0 >= "A" && c0 <= "Z") ||
|
|
99
|
+
(c0 >= "a" && c0 <= "z") ||
|
|
100
|
+
(c0 > "\u007F") # treat non-ASCII single-char as letter candidate
|
|
101
|
+
raise InvalidCharacterError.new(
|
|
102
|
+
"Illegal character in short flag",
|
|
103
|
+
1,
|
|
104
|
+
arg
|
|
105
|
+
)
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# remaining characters validation (character-mode)
|
|
109
|
+
j = 1
|
|
110
|
+
while j < name.length
|
|
111
|
+
ch = name[j,1]
|
|
112
|
+
valid =
|
|
113
|
+
ch == "_" ||
|
|
114
|
+
(ch >= "A" && ch <= "Z") ||
|
|
115
|
+
(ch >= "a" && ch <= "z") ||
|
|
116
|
+
(ch >= "0" && ch <= "9") ||
|
|
117
|
+
ch == "-" ||
|
|
118
|
+
ch == "." ||
|
|
119
|
+
(ch > "\u007F") # allow non-ASCII letters
|
|
120
|
+
unless valid
|
|
121
|
+
raise InvalidCharacterError.new(
|
|
122
|
+
"Illegal character in short flag",
|
|
123
|
+
1,
|
|
124
|
+
arg
|
|
125
|
+
)
|
|
126
|
+
end
|
|
127
|
+
j += 1
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# attached value (character-mode)
|
|
131
|
+
if arg.length > 2
|
|
132
|
+
val_str = arg[2, arg.length - 2]
|
|
133
|
+
val_lex = Lexer.new(val_str, 0, val_str.length, false)
|
|
134
|
+
vp = ValueParser.new(val_lex)
|
|
135
|
+
value = vp.parse_scalar
|
|
136
|
+
else
|
|
137
|
+
value = true
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
out[name] = value
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def build_hash(fields, vals)
|
|
145
|
+
h = {}
|
|
146
|
+
i = 0
|
|
147
|
+
while i < fields.size
|
|
148
|
+
h[fields[i]] = vals[i]
|
|
149
|
+
i += 1
|
|
150
|
+
end
|
|
151
|
+
h
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def assign(out, name, spec, value)
|
|
155
|
+
case spec[:kind]
|
|
156
|
+
when :scalar
|
|
157
|
+
out[name] = value
|
|
158
|
+
when :hash
|
|
159
|
+
existing = out[name]
|
|
160
|
+
h = existing.is_a?(Hash) ? existing : {}
|
|
161
|
+
value.each { |k, v| h[k] = v }
|
|
162
|
+
out[name] = h
|
|
163
|
+
when :array_scalar
|
|
164
|
+
existing = out[name]
|
|
165
|
+
arr = existing.is_a?(Array) ? existing : []
|
|
166
|
+
arr.push(value)
|
|
167
|
+
out[name] = arr
|
|
168
|
+
when :array_hash
|
|
169
|
+
existing = out[name]
|
|
170
|
+
arr = existing.is_a?(Array) ? existing : []
|
|
171
|
+
arr.push(value)
|
|
172
|
+
out[name] = arr
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
end
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# key_parser.rb
|
|
2
|
+
module TypedArgs
|
|
3
|
+
module Internal
|
|
4
|
+
class KeyParser
|
|
5
|
+
def initialize(lexer)
|
|
6
|
+
@lx = lexer
|
|
7
|
+
@tok = @lx.next_token
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def parse
|
|
11
|
+
key = parse_key_string
|
|
12
|
+
array = false
|
|
13
|
+
fields = nil
|
|
14
|
+
|
|
15
|
+
if key.length > 0 && key[key.length - 1,1] == "."
|
|
16
|
+
raise UnexpectedTokenError.new("Expected IDENT after DOT", @tok.pos, @lx.str)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
i = 0
|
|
20
|
+
while i + 1 < key.length
|
|
21
|
+
if key[i,1] == "." && key[i + 1,1] == "."
|
|
22
|
+
raise UnexpectedTokenError.new("Unexpected DOT", @tok.pos, @lx.str)
|
|
23
|
+
end
|
|
24
|
+
i += 1
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
if key.index(".") && @tok.type == :PLUS
|
|
28
|
+
raise InvalidSuffixPositionError.new("Suffix must be at end of key", @tok.pos, @lx.str)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
if @tok.type == :PLUS
|
|
32
|
+
consume(:PLUS)
|
|
33
|
+
array = true
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
if @tok.type == :COLON
|
|
37
|
+
consume(:COLON)
|
|
38
|
+
fields = parse_ident_list
|
|
39
|
+
consume(:COLON)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
if @tok.type == :PLUS || @tok.type == :COLON || @tok.type == :IDENT || @tok.type == :DOT
|
|
43
|
+
raise InvalidSuffixPositionError.new("Suffix must be at end of key", @tok.pos, @lx.str)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
kind =
|
|
47
|
+
if array && fields then :array_hash
|
|
48
|
+
elsif array then :array_scalar
|
|
49
|
+
elsif fields then :hash
|
|
50
|
+
else :scalar
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
{ name: key, kind: kind, fields: fields }
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
private
|
|
57
|
+
|
|
58
|
+
def parse_key_string
|
|
59
|
+
if @tok.type != :IDENT
|
|
60
|
+
raise InvalidKeyStartError.new("Invalid key start", @tok.pos, @lx.str)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
first = expect(:IDENT).value
|
|
64
|
+
buf = first
|
|
65
|
+
|
|
66
|
+
while @tok.type == :DOT
|
|
67
|
+
consume(:DOT)
|
|
68
|
+
if @tok.type != :IDENT
|
|
69
|
+
raise UnexpectedTokenError.new("Expected IDENT, got " + @tok.type.to_s, @tok.pos, @lx.str)
|
|
70
|
+
end
|
|
71
|
+
part = expect(:IDENT).value
|
|
72
|
+
buf = buf + "." + part
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
buf
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def parse_ident_list
|
|
79
|
+
list = []
|
|
80
|
+
if @tok.type != :IDENT
|
|
81
|
+
raise InvalidFieldListError.new("Expected IDENT", @tok.pos, @lx.str)
|
|
82
|
+
end
|
|
83
|
+
list.push(expect(:IDENT).value)
|
|
84
|
+
while @tok.type == :COMMA
|
|
85
|
+
consume(:COMMA)
|
|
86
|
+
if @tok.type != :IDENT
|
|
87
|
+
raise InvalidFieldListError.new("Expected IDENT", @tok.pos, @lx.str)
|
|
88
|
+
end
|
|
89
|
+
list.push(expect(:IDENT).value)
|
|
90
|
+
end
|
|
91
|
+
list
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def expect(type)
|
|
95
|
+
if @tok.type != type
|
|
96
|
+
raise UnexpectedTokenError.new("Expected #{type}, got #{@tok.type}", @tok.pos, @lx.str)
|
|
97
|
+
end
|
|
98
|
+
tok = @tok
|
|
99
|
+
@tok = @lx.next_token
|
|
100
|
+
tok
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def consume(type)
|
|
104
|
+
expect(type)
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
data/mrblib/lexer.rb
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
# lexer.rb (optimized)
|
|
2
|
+
module TypedArgs
|
|
3
|
+
module Internal
|
|
4
|
+
class Lexer
|
|
5
|
+
attr_reader :str
|
|
6
|
+
|
|
7
|
+
def initialize(str, start_pos, char_length, parsing_key = false)
|
|
8
|
+
@str = str || ""
|
|
9
|
+
@parsing_key = !!parsing_key
|
|
10
|
+
@i = start_pos || 0
|
|
11
|
+
@start = @i
|
|
12
|
+
@end = [@start + (char_length || 0), @str.length].min
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def parsing_key?
|
|
16
|
+
@parsing_key
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def next_token
|
|
20
|
+
s = @str
|
|
21
|
+
i = @i
|
|
22
|
+
end_i = @end
|
|
23
|
+
|
|
24
|
+
# skip whitespace
|
|
25
|
+
while i < end_i
|
|
26
|
+
ch = s[i,1]
|
|
27
|
+
break unless ch <= " "
|
|
28
|
+
i += 1
|
|
29
|
+
end
|
|
30
|
+
@i = i
|
|
31
|
+
return Token.new(:EOF, nil, i) if i >= end_i
|
|
32
|
+
|
|
33
|
+
ch = s[i,1]
|
|
34
|
+
|
|
35
|
+
# single-char tokens
|
|
36
|
+
case ch
|
|
37
|
+
when "," then @i = i + 1; return Token.new(:COMMA, nil, i)
|
|
38
|
+
when "=" then @i = i + 1; return Token.new(:EQUAL, nil, i)
|
|
39
|
+
when "." then @i = i + 1; return Token.new(:DOT, nil, i)
|
|
40
|
+
when "+" then @i = i + 1; return Token.new(:PLUS, nil, i)
|
|
41
|
+
when ":" then @i = i + 1; return Token.new(:COLON, nil, i)
|
|
42
|
+
when "\""
|
|
43
|
+
start = i
|
|
44
|
+
i += 1
|
|
45
|
+
|
|
46
|
+
# scan UTF‑8 codepoints until closing quote
|
|
47
|
+
while i < end_i
|
|
48
|
+
cp, ni = Internal.utf8_next(s, i)
|
|
49
|
+
|
|
50
|
+
if cp == 0x22 # '"'
|
|
51
|
+
val = s[start + 1, i - (start + 1)]
|
|
52
|
+
@i = ni
|
|
53
|
+
return Token.new(:STRING, val, start)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
i = ni
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
raise UnterminatedStringError.new("Unterminated string", start, s)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# parsing_key fast path
|
|
63
|
+
if @parsing_key
|
|
64
|
+
return ident_token_fast(s, i, end_i)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# value side: number, ident-like, or raw
|
|
68
|
+
if ascii_digit?(ch) || (ch == "-" && peek_ascii_digit?(s, i, end_i))
|
|
69
|
+
return number_token_fast(s, i, end_i)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
if ident_start_char?(ch)
|
|
73
|
+
# try to scan an ident-like token; if any char fails ident_continue, fall back to raw
|
|
74
|
+
j = i
|
|
75
|
+
all_ident = true
|
|
76
|
+
while j < end_i
|
|
77
|
+
cc = s[j,1]
|
|
78
|
+
break if cc == ","
|
|
79
|
+
unless ident_continue_char?(cc)
|
|
80
|
+
all_ident = false
|
|
81
|
+
break
|
|
82
|
+
end
|
|
83
|
+
j += 1
|
|
84
|
+
end
|
|
85
|
+
if all_ident
|
|
86
|
+
# produce IDENT token
|
|
87
|
+
val = s[i, j - i]
|
|
88
|
+
@i = j
|
|
89
|
+
return Token.new(:IDENT, val, i)
|
|
90
|
+
else
|
|
91
|
+
# raw value: scan until whitespace or comma
|
|
92
|
+
start = i
|
|
93
|
+
while i < end_i
|
|
94
|
+
cc = s[i,1]
|
|
95
|
+
break if cc == ","
|
|
96
|
+
i += 1
|
|
97
|
+
end
|
|
98
|
+
val = s[start, i - start]
|
|
99
|
+
@i = i
|
|
100
|
+
# detect all-dash invalid number
|
|
101
|
+
if all_dashes?(val)
|
|
102
|
+
raise InvalidCharacterError.new("Illegal number", start, s)
|
|
103
|
+
end
|
|
104
|
+
return Token.new(:STRING, val, start)
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# fallback raw value
|
|
109
|
+
start = i
|
|
110
|
+
while i < end_i
|
|
111
|
+
cc = s[i,1]
|
|
112
|
+
break if cc == ","
|
|
113
|
+
i += 1
|
|
114
|
+
end
|
|
115
|
+
val = s[start, i - start]
|
|
116
|
+
@i = i
|
|
117
|
+
if all_dashes?(val)
|
|
118
|
+
raise InvalidCharacterError.new("Illegal number", start, s)
|
|
119
|
+
end
|
|
120
|
+
Token.new(:STRING, val, start)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
private
|
|
124
|
+
|
|
125
|
+
# IDENT token when parsing_key true
|
|
126
|
+
def ident_token_fast(s, i, end_i)
|
|
127
|
+
start = i
|
|
128
|
+
ch = s[i,1]
|
|
129
|
+
unless ident_start_char?(ch)
|
|
130
|
+
if ascii_digit?(ch)
|
|
131
|
+
raise InvalidKeyStartError.new("Invalid key start", i, s)
|
|
132
|
+
else
|
|
133
|
+
raise InvalidCharacterError.new("Illegal character in key", i, s)
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
i += 1
|
|
137
|
+
while i < end_i
|
|
138
|
+
c = s[i,1]
|
|
139
|
+
break unless ident_continue_char?(c)
|
|
140
|
+
i += 1
|
|
141
|
+
end
|
|
142
|
+
val = s[start, i - start]
|
|
143
|
+
@i = i
|
|
144
|
+
Token.new(:IDENT, val, start)
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def number_token_fast(s, i, end_i)
|
|
148
|
+
start = i
|
|
149
|
+
digits = 0
|
|
150
|
+
dot = false
|
|
151
|
+
|
|
152
|
+
if s[i,1] == "-"
|
|
153
|
+
i += 1
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
while i < end_i
|
|
157
|
+
c = s[i,1]
|
|
158
|
+
if ascii_digit?(c)
|
|
159
|
+
digits += 1
|
|
160
|
+
i += 1
|
|
161
|
+
elsif c == "."
|
|
162
|
+
raise InvalidNumberError.new("Invalid number format", start, s) if dot
|
|
163
|
+
dot = true
|
|
164
|
+
i += 1
|
|
165
|
+
else
|
|
166
|
+
break
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
raise InvalidCharacterError.new("Illegal number", start, s) if digits == 0
|
|
171
|
+
|
|
172
|
+
val = s[start, i - start]
|
|
173
|
+
@i = i
|
|
174
|
+
Token.new(:NUMBER, val, start)
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
# helpers (inlined ASCII fast paths)
|
|
178
|
+
def ascii_digit?(ch)
|
|
179
|
+
ch >= "0" && ch <= "9"
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def all_dashes?(str)
|
|
183
|
+
k = 0
|
|
184
|
+
while k < str.length
|
|
185
|
+
return false if str[k,1] != "-"
|
|
186
|
+
k += 1
|
|
187
|
+
end
|
|
188
|
+
true
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
def ident_start_char?(ch)
|
|
192
|
+
return true if ch == "_"
|
|
193
|
+
return true if (ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z")
|
|
194
|
+
# non-ASCII single-char candidate
|
|
195
|
+
ch.length == 1 && ch > "\u007F"
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
def ident_continue_char?(ch)
|
|
199
|
+
return true if ident_start_char?(ch)
|
|
200
|
+
return true if ascii_digit?(ch)
|
|
201
|
+
return true if ch == "-" || ch == "."
|
|
202
|
+
false
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
def peek_ascii_digit?(s, i, end_i)
|
|
206
|
+
return false if (i + 1) >= end_i
|
|
207
|
+
c = s[i + 1,1]
|
|
208
|
+
c >= "0" && c <= "9"
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# script_check.rb (optimized)
|
|
2
|
+
module TypedArgs
|
|
3
|
+
module Internal
|
|
4
|
+
module ScriptCheck
|
|
5
|
+
LATIN_RANGES = [
|
|
6
|
+
["\u00C0", "\u024F"]
|
|
7
|
+
]
|
|
8
|
+
GREEK_RANGES = [
|
|
9
|
+
["\u0370", "\u03FF"]
|
|
10
|
+
]
|
|
11
|
+
CYRILLIC_RANGES = [
|
|
12
|
+
["\u0400", "\u04FF"]
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
def self.in_ranges?(ch, ranges)
|
|
16
|
+
j = 0
|
|
17
|
+
while j < ranges.length
|
|
18
|
+
start_ch, end_ch = ranges[j]
|
|
19
|
+
return true if ch >= start_ch && ch <= end_ch
|
|
20
|
+
j += 1
|
|
21
|
+
end
|
|
22
|
+
false
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def self.char_bucket(ch)
|
|
26
|
+
return :latin if (ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z") || in_ranges?(ch, LATIN_RANGES)
|
|
27
|
+
return :greek if in_ranges?(ch, GREEK_RANGES)
|
|
28
|
+
return :cyrillic if in_ranges?(ch, CYRILLIC_RANGES)
|
|
29
|
+
:other
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def self.validate_key(str)
|
|
33
|
+
seen = nil
|
|
34
|
+
i = 0
|
|
35
|
+
n = str.length
|
|
36
|
+
while i < n
|
|
37
|
+
ch = str[i,1]
|
|
38
|
+
# skip punctuation
|
|
39
|
+
if ch == "." || ch == "+" || ch == ":" || ch == ","
|
|
40
|
+
i += 1
|
|
41
|
+
next
|
|
42
|
+
end
|
|
43
|
+
# underscore and digits ignored for bucket
|
|
44
|
+
if ch == "_" || (ch >= "0" && ch <= "9")
|
|
45
|
+
i += 1
|
|
46
|
+
next
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
bucket = char_bucket(ch)
|
|
50
|
+
if bucket != :other
|
|
51
|
+
if seen && seen != bucket
|
|
52
|
+
raise InvalidCharacterError.new("Invalid or mixed-script key", i, str)
|
|
53
|
+
end
|
|
54
|
+
seen ||= bucket
|
|
55
|
+
end
|
|
56
|
+
i += 1
|
|
57
|
+
end
|
|
58
|
+
true
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
data/mrblib/token.rb
ADDED
data/mrblib/typedargs.rb
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
module TypedArgs
|
|
2
|
+
class Error < StandardError; end
|
|
3
|
+
class SyntaxError < Error
|
|
4
|
+
attr_reader :pos, :source
|
|
5
|
+
|
|
6
|
+
def initialize(msg, pos, source)
|
|
7
|
+
@pos = pos
|
|
8
|
+
@source = source || ""
|
|
9
|
+
line = @source
|
|
10
|
+
pointer = " " * @pos + "^"
|
|
11
|
+
pretty = "\n" + line + "\n" + pointer + "\nSyntax error: " + msg
|
|
12
|
+
super(pretty)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
class InvalidCharacterError < SyntaxError; end
|
|
17
|
+
class InvalidKeyStartError < SyntaxError; end
|
|
18
|
+
class UnterminatedStringError < SyntaxError; end
|
|
19
|
+
class ArityMismatchError < SyntaxError; end
|
|
20
|
+
class UnexpectedTokenError < SyntaxError; end
|
|
21
|
+
class InvalidSuffixPositionError < SyntaxError; end
|
|
22
|
+
class InvalidFieldListError < SyntaxError; end
|
|
23
|
+
class InvalidNumberError < SyntaxError; end
|
|
24
|
+
|
|
25
|
+
class << self
|
|
26
|
+
def alias(short, long)
|
|
27
|
+
Internal.register_alias(short, long)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def opts(*argv)
|
|
31
|
+
args = argv.empty? ? ::ARGV : argv
|
|
32
|
+
Internal::Impl.parse(args)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
data/mrblib/utf8.rb
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
module TypedArgs
|
|
2
|
+
module Internal
|
|
3
|
+
SCRIPT_ASCII = 1
|
|
4
|
+
SCRIPT_LATIN = 1
|
|
5
|
+
SCRIPT_GREEK = 3
|
|
6
|
+
SCRIPT_CYRILLIC = 4
|
|
7
|
+
SCRIPT_OTHER = 5
|
|
8
|
+
|
|
9
|
+
def self.printable_byte?(b)
|
|
10
|
+
return true if b >= 0x20 && b <= 0x7E
|
|
11
|
+
return false if b >= 0x80 && b <= 0x9F
|
|
12
|
+
b >= 0xA0
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def self.utf8_next(str, i)
|
|
16
|
+
b0 = str.getbyte(i)
|
|
17
|
+
return [nil, i + 1] if b0.nil?
|
|
18
|
+
|
|
19
|
+
if b0 < 0x80
|
|
20
|
+
return [b0, i + 1]
|
|
21
|
+
elsif (b0 & 0xE0) == 0xC0
|
|
22
|
+
b1 = str.getbyte(i + 1)
|
|
23
|
+
return [nil, i + 1] if b1.nil? || (b1 & 0xC0) != 0x80
|
|
24
|
+
cp = ((b0 & 0x1F) << 6) | (b1 & 0x3F)
|
|
25
|
+
return [cp, i + 2]
|
|
26
|
+
elsif (b0 & 0xF0) == 0xE0
|
|
27
|
+
b1 = str.getbyte(i + 1)
|
|
28
|
+
b2 = str.getbyte(i + 2)
|
|
29
|
+
return [nil, i + 1] if b1.nil? || b2.nil?
|
|
30
|
+
return [nil, i + 1] if (b1 & 0xC0) != 0x80 || (b2 & 0xC0) != 0x80
|
|
31
|
+
cp = ((b0 & 0x0F) << 12) | ((b1 & 0x3F) << 6) | (b2 & 0x3F)
|
|
32
|
+
return [cp, i + 3]
|
|
33
|
+
elsif (b0 & 0xF8) == 0xF0
|
|
34
|
+
b1 = str.getbyte(i + 1)
|
|
35
|
+
b2 = str.getbyte(i + 2)
|
|
36
|
+
b3 = str.getbyte(i + 3)
|
|
37
|
+
return [nil, i + 1] if b1.nil? || b2.nil? || b3.nil?
|
|
38
|
+
return [nil, i + 1] if (b1 & 0xC0) != 0x80 || (b2 & 0xC0) != 0x80 || (b3 & 0xC0) != 0x80
|
|
39
|
+
cp = ((b0 & 0x07) << 18) | ((b1 & 0x3F) << 12) | ((b2 & 0x3F) << 6) | (b3 & 0x3F)
|
|
40
|
+
return [cp, i + 4]
|
|
41
|
+
else
|
|
42
|
+
return [nil, i + 1]
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# value_parser.rb
|
|
2
|
+
module TypedArgs
|
|
3
|
+
module Internal
|
|
4
|
+
class ValueParser
|
|
5
|
+
def initialize(lexer)
|
|
6
|
+
@lx = lexer
|
|
7
|
+
@tok = @lx.next_token
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def parse_tuple(expected)
|
|
11
|
+
vals = []
|
|
12
|
+
vals.push(parse_scalar)
|
|
13
|
+
while @tok.type == :COMMA
|
|
14
|
+
consume(:COMMA)
|
|
15
|
+
vals.push(parse_scalar)
|
|
16
|
+
end
|
|
17
|
+
if vals.size != expected
|
|
18
|
+
raise ArityMismatchError.new("Arity mismatch: expected #{expected}, got #{vals.size}", @tok.pos, @lx.str)
|
|
19
|
+
end
|
|
20
|
+
vals
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def parse_scalar
|
|
24
|
+
if @tok.type == :EOF
|
|
25
|
+
raise UnexpectedTokenError.new("Unexpected EOF", @tok.pos, @lx.str)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
case @tok.type
|
|
29
|
+
when :STRING
|
|
30
|
+
v = @tok.value
|
|
31
|
+
consume(:STRING)
|
|
32
|
+
v
|
|
33
|
+
when :NUMBER
|
|
34
|
+
v = @tok.value
|
|
35
|
+
consume(:NUMBER)
|
|
36
|
+
convert_number_token(v)
|
|
37
|
+
when :IDENT
|
|
38
|
+
v = @tok.value
|
|
39
|
+
consume(:IDENT)
|
|
40
|
+
case v
|
|
41
|
+
when "true" then true
|
|
42
|
+
when "false" then false
|
|
43
|
+
when "nil" then nil
|
|
44
|
+
else
|
|
45
|
+
v
|
|
46
|
+
end
|
|
47
|
+
else
|
|
48
|
+
buf = ""
|
|
49
|
+
while @tok.type != :COMMA && @tok.type != :EOF
|
|
50
|
+
buf += @tok.value.to_s
|
|
51
|
+
@tok = @lx.next_token
|
|
52
|
+
end
|
|
53
|
+
buf
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
def convert_number_token(s)
|
|
60
|
+
return s.to_i if integer_string?(s)
|
|
61
|
+
if Object.const_defined?(:Float)
|
|
62
|
+
return s.to_f if float_string?(s)
|
|
63
|
+
end
|
|
64
|
+
raise InvalidNumberError.new("Invalid number", @tok.pos, s)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def integer_string?(s)
|
|
68
|
+
i = 0
|
|
69
|
+
n = s.length
|
|
70
|
+
return false if n == 0
|
|
71
|
+
if s[0,1] == "-"
|
|
72
|
+
return false if n == 1
|
|
73
|
+
i = 1
|
|
74
|
+
end
|
|
75
|
+
while i < n
|
|
76
|
+
ch = s[i,1]
|
|
77
|
+
return false unless ch >= "0" && ch <= "9"
|
|
78
|
+
i += 1
|
|
79
|
+
end
|
|
80
|
+
true
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def float_string?(s)
|
|
84
|
+
i = 0
|
|
85
|
+
n = s.length
|
|
86
|
+
return false if n == 0
|
|
87
|
+
if s[0,1] == "-"
|
|
88
|
+
return false if n == 1
|
|
89
|
+
i = 1
|
|
90
|
+
end
|
|
91
|
+
seen_dot = false
|
|
92
|
+
digits = 0
|
|
93
|
+
while i < n
|
|
94
|
+
ch = s[i,1]
|
|
95
|
+
if ch == "."
|
|
96
|
+
return false if seen_dot
|
|
97
|
+
seen_dot = true
|
|
98
|
+
elsif ch >= "0" && ch <= "9"
|
|
99
|
+
digits += 1
|
|
100
|
+
else
|
|
101
|
+
return false
|
|
102
|
+
end
|
|
103
|
+
i += 1
|
|
104
|
+
end
|
|
105
|
+
seen_dot && digits > 0
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def consume(type)
|
|
109
|
+
if @tok.type != type
|
|
110
|
+
raise UnexpectedTokenError.new("Expected #{type}, got #{@tok.type}", @tok.pos, @lx.str)
|
|
111
|
+
end
|
|
112
|
+
@tok = @lx.next_token
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: typedargs
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Asmod4n
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: Pure Ruby implementation of TypedArgs, a small explicit grammar for structured
|
|
13
|
+
CLI argument parsing. Works on both MRuby and CRuby.
|
|
14
|
+
executables: []
|
|
15
|
+
extensions: []
|
|
16
|
+
extra_rdoc_files: []
|
|
17
|
+
files:
|
|
18
|
+
- LICENSE
|
|
19
|
+
- README.md
|
|
20
|
+
- lib/typedargs.rb
|
|
21
|
+
- mrblib/alias.rb
|
|
22
|
+
- mrblib/impl.rb
|
|
23
|
+
- mrblib/key_parser.rb
|
|
24
|
+
- mrblib/lexer.rb
|
|
25
|
+
- mrblib/script_check.rb
|
|
26
|
+
- mrblib/token.rb
|
|
27
|
+
- mrblib/typedargs.rb
|
|
28
|
+
- mrblib/utf8.rb
|
|
29
|
+
- mrblib/value_parser.rb
|
|
30
|
+
homepage: https://github.com/Asmod4n/mruby-typedargs
|
|
31
|
+
licenses:
|
|
32
|
+
- Apache-2.0
|
|
33
|
+
metadata: {}
|
|
34
|
+
rdoc_options: []
|
|
35
|
+
require_paths:
|
|
36
|
+
- lib
|
|
37
|
+
- mrblib
|
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
39
|
+
requirements:
|
|
40
|
+
- - ">="
|
|
41
|
+
- !ruby/object:Gem::Version
|
|
42
|
+
version: '2.5'
|
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0'
|
|
48
|
+
requirements: []
|
|
49
|
+
rubygems_version: 4.0.3
|
|
50
|
+
specification_version: 4
|
|
51
|
+
summary: A tiny, deterministic operator-typed CLI language.
|
|
52
|
+
test_files: []
|