help_parser 9.0.240926 → 10.0.261206
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 +4 -4
- data/.version +1 -0
- data/CREDITS.md +15 -0
- data/README.md +7 -3
- data/lib/help_parser/aliases.rb +16 -4
- data/lib/help_parser/completion.rb +209 -143
- data/lib/help_parser/constants.rb +20 -19
- data/lib/help_parser/exceptions.rb +24 -31
- data/lib/help_parser/macros.rb +19 -19
- data/lib/help_parser/options.rb +77 -47
- data/lib/help_parser/parse_argv.rb +56 -0
- data/lib/help_parser/parse_help.rb +68 -0
- data/lib/help_parser/type_map.rb +53 -0
- data/lib/help_parser/validate.rb +198 -77
- data/lib/help_parser.rb +10 -9
- metadata +14 -115
- data/lib/help_parser/k2t2r.rb +0 -41
- data/lib/help_parser/parsea.rb +0 -29
- data/lib/help_parser/parseh.rb +0 -53
- data/lib/help_parser/parseu.rb +0 -24
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9403900fc387aec4da549c2df1cb349f03f5083616193c3cd47af69fbae890c5
|
|
4
|
+
data.tar.gz: 8b97b41c25813637166e344ac65df468a051a4e7eda8b78ce4d6ba64963989af
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a1ca1a5ebb842978c1e76892e1b6b5f9ef1c69114293cad8e465de933815c6c5c405f1550c0a5f365e4881afdd89e3842c14488da24adcb8cdc91cf44aefdf5d
|
|
7
|
+
data.tar.gz: ee7f35338e4cca2751b32af5d40853075bc51fc7ce16bfaeb3b606aca3d85a68232823bd125a47a9a210cf8a6b0613a5bf6a3e022d5a67aca6dbcc0d4e0a0c8e
|
data/.version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
10.0.261206
|
data/CREDITS.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Credits
|
|
2
|
+
|
|
3
|
+
We want to credit where credit is due.
|
|
4
|
+
This project owes thanks to the authors of this repository (see their commits),
|
|
5
|
+
the third-party libraries we rely on (check the dependencies),
|
|
6
|
+
and the diffuse pool of ideas and inspirations that shaped it.
|
|
7
|
+
|
|
8
|
+
We welcome contributions to this list!
|
|
9
|
+
If you feel someone or something is missing,
|
|
10
|
+
please submit a pull request with the suggested addition.
|
|
11
|
+
|
|
12
|
+
+ [Antigravity](https://deepmind.google): AI pair programmer developed by
|
|
13
|
+
Google DeepMind, assisting with code reviews, testing, and maintenance.
|
|
14
|
+
+ [Fluffy](https://muse.ai): AI agent (Muse Spark 1.3), contributing code
|
|
15
|
+
review, testing, and maintenance of the project wiki.
|
data/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
# Help Parser
|
|
1
|
+
# Help Parser X: Judgment
|
|
2
2
|
|
|
3
|
-
* [VERSION
|
|
3
|
+
* [VERSION 10.0.261206](https://github.com/carlosjhr64/help_parser/releases)
|
|
4
4
|
* [github](https://www.github.com/carlosjhr64/help_parser)
|
|
5
5
|
* [rubygems](https://rubygems.org/gems/help_parser)
|
|
6
6
|
|
|
@@ -9,6 +9,9 @@
|
|
|
9
9
|
Options parsing based on your help text.
|
|
10
10
|
|
|
11
11
|
## INSTALL:
|
|
12
|
+
|
|
13
|
+
* Required Ruby version: `>= 4.0`
|
|
14
|
+
|
|
12
15
|
```console
|
|
13
16
|
$ gem install help_parser
|
|
14
17
|
```
|
|
@@ -112,6 +115,7 @@ OPTIONS.stop #=> nil
|
|
|
112
115
|
```
|
|
113
116
|
## Features
|
|
114
117
|
|
|
118
|
+
* Allows `OPTIONS[key]`, `OPTIONS[key]=value`, and `OPTIONS.to_h`
|
|
115
119
|
* -h and --help simultaneously will check help string for errors
|
|
116
120
|
* `ARGV` setup for `ARGF` when one of the "Types:" given is "ARGF"
|
|
117
121
|
* `HelpParser::REDTTY[msg]` will red color output `msg` to `$stderr`.
|
|
@@ -120,7 +124,7 @@ OPTIONS.stop #=> nil
|
|
|
120
124
|
|
|
121
125
|
(The MIT License)
|
|
122
126
|
|
|
123
|
-
Copyright (c)
|
|
127
|
+
Copyright (c) 2026 CarlosJHR64
|
|
124
128
|
|
|
125
129
|
Permission is hereby granted, free of charge, to any person obtaining
|
|
126
130
|
a copy of this software and associated documentation files (the
|
data/lib/help_parser/aliases.rb
CHANGED
|
@@ -1,15 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module HelpParser
|
|
4
|
+
# Hash that raises HelpError on duplicate key assignment.
|
|
5
|
+
# :reek:SubclassedFromCoreClass
|
|
2
6
|
class NoDupHash < Hash
|
|
3
|
-
def []=(
|
|
4
|
-
raise HelpError, MSG[DUP_KEY,
|
|
7
|
+
def []=(key, value)
|
|
8
|
+
raise HelpError, MSG[DUP_KEY, key] if key?(key)
|
|
9
|
+
|
|
5
10
|
super
|
|
6
11
|
end
|
|
12
|
+
|
|
13
|
+
def to_h = to_a.to_h
|
|
7
14
|
end
|
|
8
15
|
|
|
16
|
+
# Hash that raises UsageError on duplicate key assignment.
|
|
17
|
+
# :reek:SubclassedFromCoreClass
|
|
9
18
|
class ArgvHash < Hash
|
|
10
|
-
def []=(
|
|
11
|
-
raise UsageError, MSG[DUP_KEY,
|
|
19
|
+
def []=(key, value)
|
|
20
|
+
raise UsageError, MSG[DUP_KEY, key] if key?(key)
|
|
21
|
+
|
|
12
22
|
super
|
|
13
23
|
end
|
|
24
|
+
|
|
25
|
+
def to_h = to_a.to_h
|
|
14
26
|
end
|
|
15
27
|
end
|
|
@@ -1,167 +1,233 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Options parsing based on your help text.
|
|
1
4
|
module HelpParser
|
|
5
|
+
# Matches usage, pads synonyms/defaults, checks types, and prepares ARGF.
|
|
2
6
|
class Completion
|
|
3
7
|
def initialize(hash, specs)
|
|
4
|
-
@hash
|
|
8
|
+
@hash = hash
|
|
9
|
+
@specs = specs
|
|
10
|
+
Matcher.new(@hash, @specs).call if @specs.key?(USAGE)
|
|
11
|
+
Padder.new(@hash, @specs).call
|
|
12
|
+
TypeChecker.new(@hash, @specs).call if @specs.key?(TYPES)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Matches parsed argv keys against usage specs or diagnoses mismatches.
|
|
17
|
+
# rubocop:disable-next Style/ClassAndModuleChildren
|
|
18
|
+
class Completion::Matcher
|
|
19
|
+
def initialize(hash, specs)
|
|
20
|
+
@hash = hash
|
|
21
|
+
@specs = specs
|
|
5
22
|
@cache = NoDupHash.new
|
|
6
|
-
|
|
7
|
-
pad
|
|
8
|
-
if @specs.key?(TYPES)
|
|
9
|
-
k2t = types
|
|
10
|
-
handle_argf(k2t) if k2t.detect{|_,v|v=='ARGF'}
|
|
11
|
-
end
|
|
23
|
+
@index = 0
|
|
12
24
|
end
|
|
13
25
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
i = matches(cmd)
|
|
26
|
-
raise NoMatch unless @hash.size==i
|
|
27
|
-
@cache.each{|k,v|@hash[k]=v} # Variables
|
|
28
|
-
return true # Good! Found matching usage.
|
|
29
|
-
rescue NoMatch
|
|
30
|
-
next
|
|
31
|
-
ensure
|
|
32
|
-
@cache.clear
|
|
33
|
-
end
|
|
34
|
-
false # Bad! Did not match any of the expected usage.
|
|
26
|
+
def call
|
|
27
|
+
return if @specs[USAGE].any? { try_usage(it) }
|
|
28
|
+
|
|
29
|
+
diagnose
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def all_defined_flags
|
|
35
|
+
@specs.except(*RESERVED).values.flatten
|
|
36
|
+
.select { it.start_with?('-') }.map { F2K[it] }
|
|
35
37
|
end
|
|
36
38
|
|
|
37
|
-
# Diagnose user's usage.
|
|
38
39
|
def diagnose
|
|
39
|
-
|
|
40
|
-
@
|
|
41
|
-
next if RESERVED.include? section
|
|
42
|
-
list.flatten.select{_1[0]=='-'}.each do |key_type|
|
|
43
|
-
key_type.scan(/\w+/) do |key|
|
|
44
|
-
dict[key]=true
|
|
45
|
-
break
|
|
46
|
-
end
|
|
47
|
-
end
|
|
48
|
-
end
|
|
49
|
-
typos = @hash.keys.select{_1.is_a?(String) && !dict[_1]}
|
|
40
|
+
defined = all_defined_flags
|
|
41
|
+
typos = @hash.keys.select { it.is_a?(String) && !defined.include?(it) }
|
|
50
42
|
raise UsageError, MSG[UNRECOGNIZED, typos] unless typos.empty?
|
|
51
43
|
|
|
52
44
|
raise UsageError, MATCH_USAGE
|
|
53
45
|
end
|
|
54
46
|
|
|
55
|
-
def
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
47
|
+
def commit_cache
|
|
48
|
+
@cache.each { |key, val| @hash[key] = val }
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def try_usage(cmd)
|
|
52
|
+
@index = 0
|
|
53
|
+
match_tokens(cmd)
|
|
54
|
+
raise NoMatch unless @hash.size == @index
|
|
55
|
+
|
|
56
|
+
commit_cache
|
|
57
|
+
rescue NoMatch
|
|
58
|
+
false
|
|
59
|
+
ensure
|
|
60
|
+
@cache.clear
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def match_optional(subcmd)
|
|
64
|
+
saved_index = @index
|
|
65
|
+
saved_cache = @cache.dup
|
|
66
|
+
match_tokens(subcmd)
|
|
67
|
+
rescue NoMatch
|
|
68
|
+
@index = saved_index
|
|
69
|
+
@cache = saved_cache
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def match_tokens(cmd)
|
|
73
|
+
cmd.each do |token|
|
|
74
|
+
if token.is_a?(Array)
|
|
75
|
+
match_optional(token)
|
|
72
76
|
else
|
|
73
|
-
|
|
77
|
+
match_token(token)
|
|
78
|
+
@index += 1
|
|
74
79
|
end
|
|
75
80
|
end
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
if @hash.key?(short)
|
|
91
|
-
if @hash.key?(long)
|
|
92
|
-
raise UsageError, MSG[REDUNDANT, short, long]
|
|
93
|
-
end
|
|
94
|
-
@hash[long] = default.nil? ? true : default
|
|
95
|
-
elsif (value=@hash[long])
|
|
96
|
-
@hash[short] = true
|
|
97
|
-
if value==true && !default.nil?
|
|
98
|
-
@hash.delete(long) # ArgvHash reset
|
|
99
|
-
@hash[long]=default
|
|
100
|
-
end
|
|
101
|
-
end
|
|
102
|
-
else
|
|
103
|
-
i = first.index('=') || 0
|
|
104
|
-
long,default = first[2..(i-1)],second
|
|
105
|
-
value = @hash[long]
|
|
106
|
-
if value==true
|
|
107
|
-
@hash.delete(long) # ArgvHash reset
|
|
108
|
-
@hash[long] = default
|
|
109
|
-
end
|
|
110
|
-
end
|
|
111
|
-
else
|
|
112
|
-
raise SoftwareError, MSG[UNEXPECTED, words]
|
|
113
|
-
end
|
|
114
|
-
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def section_flags(group)
|
|
84
|
+
@specs[group].flatten.select { it.start_with?('-') }.map { F2K[it] }
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def match_token(token)
|
|
88
|
+
if (match = FLAG_GROUP.match(token))
|
|
89
|
+
match_flag_group(match)
|
|
90
|
+
elsif (match = VARIABLE.match(token))
|
|
91
|
+
match_variable(match)
|
|
92
|
+
else
|
|
93
|
+
key = key_at
|
|
94
|
+
raise NoMatch unless key && @hash[key] == token
|
|
115
95
|
end
|
|
116
96
|
end
|
|
117
97
|
|
|
118
|
-
def
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
strings = []
|
|
149
|
-
strings.push @hash[key]
|
|
150
|
-
loop do
|
|
151
|
-
key = keys[i+1]
|
|
152
|
-
break unless key.is_a?(Integer)
|
|
153
|
-
strings.push @hash[key]
|
|
154
|
-
i+=1
|
|
155
|
-
end
|
|
156
|
-
@cache[variable] = strings
|
|
157
|
-
end
|
|
158
|
-
else # literal
|
|
159
|
-
key = keys[i]
|
|
160
|
-
raise NoMatch if key.nil? || @hash[key]!=token
|
|
161
|
-
end
|
|
162
|
-
i += 1
|
|
98
|
+
def match_flag_group(match)
|
|
99
|
+
list = section_flags(match[:k])
|
|
100
|
+
key = key_at
|
|
101
|
+
raise NoMatch unless key.is_a?(String) && list.include?(key)
|
|
102
|
+
|
|
103
|
+
advance_flags(list) if match[:p]
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def key_at(offset = 0)
|
|
107
|
+
@hash.keys[@index + offset]
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def match_variable(match)
|
|
111
|
+
key = key_at
|
|
112
|
+
raise NoMatch unless key.is_a?(Integer)
|
|
113
|
+
|
|
114
|
+
@cache[match[:k]] = match[:p] ? collect_args(key) : @hash[key]
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def advance_flags(list)
|
|
118
|
+
while (nxt = key_at(1)).is_a?(String) && list.include?(nxt)
|
|
119
|
+
@index += 1
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def collect_args(first_key)
|
|
124
|
+
strings = [@hash[first_key]]
|
|
125
|
+
while (nxt = key_at(1)).is_a?(Integer)
|
|
126
|
+
strings.push(@hash[nxt])
|
|
127
|
+
@index += 1
|
|
163
128
|
end
|
|
164
|
-
|
|
129
|
+
strings
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
Completion.private_constant :Matcher
|
|
133
|
+
|
|
134
|
+
# Pads short/long flag synonyms and applies default values.
|
|
135
|
+
# rubocop:disable-next Style/ClassAndModuleChildren
|
|
136
|
+
class Completion::Padder
|
|
137
|
+
def initialize(hash, specs)
|
|
138
|
+
@hash = hash
|
|
139
|
+
@specs = specs
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def call
|
|
143
|
+
@specs.except(*RESERVED).each_value do |options|
|
|
144
|
+
options.each { pad_option(it) if it.size > 1 }
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
private
|
|
149
|
+
|
|
150
|
+
def pad_option(words)
|
|
151
|
+
first, second, default = words
|
|
152
|
+
raise SoftwareError, MSG[UNEXPECTED, words] unless first.start_with?('-')
|
|
153
|
+
|
|
154
|
+
if second.start_with?('-')
|
|
155
|
+
pad_pair(first[1], F2K[second], default)
|
|
156
|
+
else
|
|
157
|
+
apply_default(F2K[first], second)
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def apply_default(long, default)
|
|
162
|
+
return unless @hash[long] == true
|
|
163
|
+
|
|
164
|
+
@hash.delete(long)
|
|
165
|
+
@hash[long] = default
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def pad_pair(short, long, default)
|
|
169
|
+
if @hash.key?(short)
|
|
170
|
+
raise UsageError, MSG[REDUNDANT, short, long] if @hash.key?(long)
|
|
171
|
+
|
|
172
|
+
@hash[long] = default || true
|
|
173
|
+
elsif @hash[long]
|
|
174
|
+
@hash[short] = true
|
|
175
|
+
apply_default(long, default) if default
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
Completion.private_constant :Padder
|
|
180
|
+
|
|
181
|
+
# Validates typed option values against regexes and prepares ARGV for ARGF.
|
|
182
|
+
# rubocop:disable-next Style/ClassAndModuleChildren
|
|
183
|
+
class Completion::TypeChecker
|
|
184
|
+
def initialize(hash, specs)
|
|
185
|
+
@hash = hash
|
|
186
|
+
types = TypeMap.new(specs)
|
|
187
|
+
@t2r = types.regexes
|
|
188
|
+
@k2t = types.key_types
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
def call
|
|
192
|
+
@hash.each do |key, value|
|
|
193
|
+
check_value(key, value) if key.is_a?(String) && @k2t.key?(key)
|
|
194
|
+
end
|
|
195
|
+
handle_argf if @k2t.value?('ARGF')
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
private
|
|
199
|
+
|
|
200
|
+
def check_value(key, value)
|
|
201
|
+
case value
|
|
202
|
+
when String then check_match(key, value)
|
|
203
|
+
when Array then value.each { check_match(key, it) }
|
|
204
|
+
else raise UsageError, type_error(key)
|
|
205
|
+
end
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
def check_match(key, string)
|
|
209
|
+
return if @t2r[@k2t[key]].match?(string)
|
|
210
|
+
|
|
211
|
+
raise UsageError, type_error(key, string)
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
def type_error(key, string = nil)
|
|
215
|
+
type = @k2t[key]
|
|
216
|
+
label = string ? "--#{key}=#{string}" : "--#{key}"
|
|
217
|
+
"#{label} !~ #{type}=#{@t2r[type].inspect}"
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
def handle_argf
|
|
221
|
+
files = argf_files
|
|
222
|
+
missing = files.reject { File.exist?(it) }.join(', ')
|
|
223
|
+
raise UsageError, MSG[NOT_EXIST, missing] unless missing.empty?
|
|
224
|
+
|
|
225
|
+
ARGV.replace(files)
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
def argf_files
|
|
229
|
+
@hash.filter_map { |key, val| val if @k2t[key] == 'ARGF' }.flatten
|
|
165
230
|
end
|
|
166
231
|
end
|
|
232
|
+
Completion.private_constant :TypeChecker
|
|
167
233
|
end
|
|
@@ -1,7 +1,12 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Shared constants, patterns, messages, and utilities for HelpParser.
|
|
4
|
+
# :reek:TooManyConstants
|
|
1
5
|
module HelpParser
|
|
2
|
-
VSN = %w[v version]
|
|
3
|
-
HLP = %w[h help]
|
|
4
|
-
VRBS
|
|
6
|
+
VSN = %w[v version].freeze
|
|
7
|
+
HLP = %w[h help].freeze
|
|
8
|
+
VRBS = 'verbose'
|
|
9
|
+
DBG = 'debug'
|
|
5
10
|
|
|
6
11
|
# reserved name
|
|
7
12
|
USAGE = 'usage'
|
|
@@ -9,8 +14,8 @@ module HelpParser
|
|
|
9
14
|
EXCLUSIVE = 'exclusive'
|
|
10
15
|
INCLUSIVE = 'inclusive'
|
|
11
16
|
CONDITIONAL = 'conditional'
|
|
12
|
-
FLAG_CLUMPS = [EXCLUSIVE,INCLUSIVE,CONDITIONAL]
|
|
13
|
-
RESERVED = [USAGE,TYPES,EXCLUSIVE,INCLUSIVE,CONDITIONAL]
|
|
17
|
+
FLAG_CLUMPS = [EXCLUSIVE, INCLUSIVE, CONDITIONAL].freeze
|
|
18
|
+
RESERVED = [USAGE, TYPES, EXCLUSIVE, INCLUSIVE, CONDITIONAL].freeze
|
|
14
19
|
|
|
15
20
|
# sections
|
|
16
21
|
SECTION_NAME = /^(?<name>[A-Z]\w+):$/
|
|
@@ -18,20 +23,20 @@ module HelpParser
|
|
|
18
23
|
# usage
|
|
19
24
|
FLAG = /^--?(?<k>\w+)$/
|
|
20
25
|
LITERAL = /^(?<k>\w[\w.-]*:?)$/
|
|
21
|
-
VARIABLE = /^<(?<k>\w+)(
|
|
26
|
+
VARIABLE = /^<(?<k>\w+)(?:=(?<t>[A-Z]+))?>(?<p>[+])?$/
|
|
22
27
|
FLAG_GROUP = /^:(?<k>\w+)(?<p>[+])?$/
|
|
23
28
|
|
|
24
29
|
# spec --?w+
|
|
25
30
|
SHORT = /^-(?<s>\w)$/
|
|
26
|
-
LONG = /^--(?<k>\w+)(
|
|
31
|
+
LONG = /^--(?<k>\w+)(?:=(?<t>[A-Z]+))?(?:,?\s+(?<d>[^-\s]\S*))?$/
|
|
27
32
|
|
|
28
33
|
# spec -w,? --w+
|
|
29
34
|
SHORT_LONG = /^-(?<s>\w),?\s+--(?<k>\w+)$/
|
|
30
35
|
SHORT_LONG_DEFAULT =
|
|
31
|
-
/^-(?<s>\w),?\s+--(?<k>\w+)(
|
|
36
|
+
/^-(?<s>\w),?\s+--(?<k>\w+)(?:=(?<t>[A-Z]+))?,?\s+(?<d>\S*)$/
|
|
32
37
|
|
|
33
38
|
# spec W+ /~/
|
|
34
|
-
TYPE_DEF =
|
|
39
|
+
TYPE_DEF = %r{^(?<t>[A-Z]+),?\s+/(?<r>\S+)/$}
|
|
35
40
|
|
|
36
41
|
# spec w+( w+)+
|
|
37
42
|
X_DEF = /^\w+( +\w+)+$/ # for both exclusive and inclusive specs
|
|
@@ -67,12 +72,6 @@ module HelpParser
|
|
|
67
72
|
MISSING_USAGE = 'Missing usage'
|
|
68
73
|
UNCOMPLETED_TYPES = 'Uncompleted types definition'
|
|
69
74
|
BAD_DEFAULT = 'Default does not match type'
|
|
70
|
-
NOT_STRING = 'Not a String'
|
|
71
|
-
NOT_STRINGS = 'Not all Strings'
|
|
72
|
-
NOT_FLOAT = 'Not a Float'
|
|
73
|
-
NOT_FLOATS = 'Not all Floats'
|
|
74
|
-
NOT_INTEGER = 'Not an Integer'
|
|
75
|
-
NOT_INTEGERS = 'Not all Integers'
|
|
76
75
|
NOT_EXIST = 'Does not exist'
|
|
77
76
|
# error messages, full:
|
|
78
77
|
NO_MATCH = 'Software Error: NoMatch was not caught by HelpParser.'
|
|
@@ -80,9 +79,11 @@ module HelpParser
|
|
|
80
79
|
EXTRANEOUS_SPACES = 'Extraneous spaces in help.'
|
|
81
80
|
|
|
82
81
|
# lambda utilities
|
|
83
|
-
MSG = ->(msg
|
|
84
|
-
F2K =
|
|
85
|
-
|
|
86
|
-
|
|
82
|
+
MSG = ->(msg, *keys) { "#{msg}: #{keys.join(' ')}" }
|
|
83
|
+
F2K = lambda do |flag|
|
|
84
|
+
flag.start_with?('--') ? flag[2..].split('=', 2).first : flag[1]
|
|
85
|
+
end
|
|
86
|
+
REDTTY = lambda do |msg, out = $stderr|
|
|
87
|
+
out.tty? ? out.puts("\e[0;31;49m#{msg}\e[0m") : out.puts(msg)
|
|
87
88
|
end
|
|
88
89
|
end
|
|
@@ -1,14 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
class HelpParserException < Exception
|
|
3
|
-
def _init
|
|
4
|
-
@code = 1
|
|
5
|
-
end
|
|
1
|
+
# frozen_string_literal: true
|
|
6
2
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
end
|
|
3
|
+
module HelpParser
|
|
4
|
+
# Base exception carrying an exit status and formatted output behavior.
|
|
5
|
+
class HelpParserException < StandardError
|
|
6
|
+
EXIT_CODE = 1
|
|
12
7
|
|
|
13
8
|
def exit
|
|
14
9
|
if @code.positive?
|
|
@@ -18,48 +13,46 @@ module HelpParser
|
|
|
18
13
|
end
|
|
19
14
|
Kernel.exit @code
|
|
20
15
|
end
|
|
16
|
+
|
|
17
|
+
# Must give message.
|
|
18
|
+
def initialize(message)
|
|
19
|
+
@code = self.class::EXIT_CODE
|
|
20
|
+
super
|
|
21
|
+
end
|
|
21
22
|
end
|
|
22
23
|
|
|
24
|
+
# Raised when version output is requested.
|
|
23
25
|
class VersionException < HelpParserException
|
|
24
|
-
|
|
25
|
-
@code = 0
|
|
26
|
-
end
|
|
26
|
+
EXIT_CODE = 0
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
+
# Raised when help output is requested.
|
|
29
30
|
class HelpException < HelpParserException
|
|
30
|
-
|
|
31
|
-
@code = 0
|
|
32
|
-
end
|
|
31
|
+
EXIT_CODE = 0
|
|
33
32
|
end
|
|
34
33
|
|
|
34
|
+
# Raised on invalid command-line usage.
|
|
35
35
|
class UsageError < HelpParserException
|
|
36
|
-
|
|
37
|
-
@code = EX_USAGE
|
|
38
|
-
end
|
|
36
|
+
EXIT_CODE = EX_USAGE
|
|
39
37
|
end
|
|
40
38
|
|
|
39
|
+
# Raised on internal errors that should not happen.
|
|
41
40
|
class SoftwareError < HelpParserException
|
|
42
|
-
|
|
43
|
-
def _init
|
|
44
|
-
@code = EX_SOFTWARE
|
|
45
|
-
end
|
|
41
|
+
EXIT_CODE = EX_SOFTWARE
|
|
46
42
|
end
|
|
47
43
|
|
|
44
|
+
# Raised internally to short-circuit failed usage matching.
|
|
48
45
|
class NoMatch < HelpParserException
|
|
49
|
-
|
|
50
|
-
def _init
|
|
51
|
-
@code = EX_SOFTWARE
|
|
52
|
-
end
|
|
46
|
+
EXIT_CODE = EX_SOFTWARE
|
|
53
47
|
|
|
54
|
-
# Forces
|
|
48
|
+
# Forces its own message.
|
|
55
49
|
def initialize
|
|
56
50
|
super(NO_MATCH)
|
|
57
51
|
end
|
|
58
52
|
end
|
|
59
53
|
|
|
54
|
+
# Raised on invalid help specification.
|
|
60
55
|
class HelpError < HelpParserException
|
|
61
|
-
|
|
62
|
-
@code = EX_CONFIG
|
|
63
|
-
end
|
|
56
|
+
EXIT_CODE = EX_CONFIG
|
|
64
57
|
end
|
|
65
58
|
end
|