help_parser 8.0.210917 → 8.1.221206
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -3
- data/lib/help_parser/completion.rb +13 -6
- data/lib/help_parser/constants.rb +4 -4
- data/lib/help_parser/k2t2r.rb +6 -2
- data/lib/help_parser/macros.rb +8 -26
- data/lib/help_parser/options.rb +3 -4
- data/lib/help_parser/parseh.rb +3 -3
- data/lib/help_parser/parseu.rb +4 -0
- data/lib/help_parser/validate.rb +7 -10
- data/lib/help_parser.rb +1 -1
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 157ae793a16a5b92ca1633306080465fcd2e9671aaed67dc01fb47cb318f0824
|
4
|
+
data.tar.gz: cc72ed9f03c94ef2f5455897807a49ba69e79786ceae5daa3d158d3265e9a931
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aea35622e25f8fd0cf84cc3115ce15b524440e698f516ebbd89188fa56d30c098dcc8711d61cee4b07aa7b77f935091222c2b0ff71465480d0e9cf76530810f5
|
7
|
+
data.tar.gz: 88a2613d9487be0b11e0da90bef805abc5fc1a7ca0012a97b32d5aa62540f382cc91e38b6714e4279bdba319475b323126625b4cf092e037742f30ba1c4ee22a
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Help Parser VIII: Helpland
|
2
2
|
|
3
|
-
* [VERSION 8.
|
3
|
+
* [VERSION 8.1.221206](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
|
|
@@ -102,13 +102,13 @@ OPTIONS.arg?.class #=> FalseClass
|
|
102
102
|
* `$DEBUG=true` on --debug
|
103
103
|
* `$VERBOSE=true` on --verbose
|
104
104
|
* -h and --help simultaneously will check help string for errors
|
105
|
-
* `HelpParser::REDTTY[msg]` will red color output `msg` to
|
105
|
+
* `HelpParser::REDTTY[msg]` will red color output `msg` to `$stderr`.
|
106
106
|
|
107
107
|
## LICENSE:
|
108
108
|
|
109
109
|
(The MIT License)
|
110
110
|
|
111
|
-
Copyright (c)
|
111
|
+
Copyright (c) 2022 CarlosJHR64
|
112
112
|
|
113
113
|
Permission is hereby granted, free of charge, to any person obtaining
|
114
114
|
a copy of this software and associated documentation files (the
|
@@ -28,9 +28,14 @@ module HelpParser
|
|
28
28
|
# Diagnose user's usage.
|
29
29
|
def diagnose
|
30
30
|
dict = {}
|
31
|
-
@specs.each do |
|
32
|
-
next if RESERVED.include?
|
33
|
-
|
31
|
+
@specs.each do |section,list|
|
32
|
+
next if RESERVED.include? section
|
33
|
+
list.flatten.select{_1[0]=='-'}.each do |key_type|
|
34
|
+
key_type.scan(/\w+/) do |key|
|
35
|
+
dict[key]=true
|
36
|
+
break
|
37
|
+
end
|
38
|
+
end
|
34
39
|
end
|
35
40
|
typos = @hash.keys.select{|k|k.is_a? String and not dict[k]}
|
36
41
|
raise UsageError, MSG[UNRECOGNIZED, typos] unless typos.empty?
|
@@ -66,7 +71,7 @@ module HelpParser
|
|
66
71
|
def pad
|
67
72
|
# Synonyms and defaults:
|
68
73
|
@specs.each do |section,options|
|
69
|
-
next if section==
|
74
|
+
next if RESERVED.any?{section==_1}
|
70
75
|
options.each do |words|
|
71
76
|
next unless words.size>1
|
72
77
|
first,second,default = words[0],words[1],words[2]
|
@@ -95,6 +100,8 @@ module HelpParser
|
|
95
100
|
@hash[long] = default
|
96
101
|
end
|
97
102
|
end
|
103
|
+
else
|
104
|
+
raise SoftwareError, MSG[UNEXPECTED, words]
|
98
105
|
end
|
99
106
|
end
|
100
107
|
end
|
@@ -113,13 +120,13 @@ module HelpParser
|
|
113
120
|
elsif m=FLAG_GROUP.match(token)
|
114
121
|
group,plus = m[:k],m[:p]
|
115
122
|
key = keys[i]
|
116
|
-
raise NoMatch
|
123
|
+
raise NoMatch unless key.is_a? String
|
117
124
|
list = @specs[group].flatten.select{|f|f[0]=='-'}.map{|f| F2K[f]}
|
118
125
|
raise NoMatch unless list.include?(key)
|
119
126
|
unless plus.nil?
|
120
127
|
loop do
|
121
128
|
key = keys[i+1]
|
122
|
-
break
|
129
|
+
break unless key.is_a?(String) and list.include?(key)
|
123
130
|
i+=1
|
124
131
|
end
|
125
132
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module HelpParser
|
2
|
-
|
3
|
-
|
2
|
+
VSN = ['v','version']
|
3
|
+
HLP = ['h','help']
|
4
4
|
VRBS,DBG = 'verbose','debug'
|
5
5
|
|
6
6
|
# reserved name
|
@@ -13,7 +13,7 @@ module HelpParser
|
|
13
13
|
RESERVED = [USAGE,TYPES,EXCLUSIVE,INCLUSIVE,CONDITIONAL]
|
14
14
|
|
15
15
|
# sections
|
16
|
-
SECTION_NAME = /^[A-Z]\w
|
16
|
+
SECTION_NAME = /^(?<name>[A-Z]\w+):$/
|
17
17
|
|
18
18
|
# usage
|
19
19
|
FLAG = /^[-][-]?(?<k>\w+)$/
|
@@ -28,7 +28,7 @@ module HelpParser
|
|
28
28
|
# spec -w,? --w+
|
29
29
|
SHORT_LONG = /^[-](?<s>\w),?\s+[-][-](?<k>\w+)$/
|
30
30
|
SHORT_LONG_DEFAULT =
|
31
|
-
/^[-](?<s>\w),?\s+[-][-](?<k>\w+)(=(?<t>[A-Z]+))?,?\s+(?<d
|
31
|
+
/^[-](?<s>\w),?\s+[-][-](?<k>\w+)(=(?<t>[A-Z]+))?,?\s+(?<d>\S*)$/
|
32
32
|
|
33
33
|
# spec W+ /~/
|
34
34
|
TYPE_DEF = /^(?<t>[A-Z]+),?\s+\/(?<r>\S+)\/$/
|
data/lib/help_parser/k2t2r.rb
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
module HelpParser
|
2
|
+
# k2t is an acronym for the "key to type" mapping
|
2
3
|
def self.k2t(specs)
|
3
4
|
k2t = NoDupHash.new
|
4
|
-
|
5
|
-
|
5
|
+
# If specs section is not a RESERVED section, it's an options list.
|
6
|
+
tokens = specs.select{|k,v| k==USAGE or not RESERVED.include?(k)}
|
7
|
+
# Tokens associating a key to a type.
|
8
|
+
.values.flatten.select{|v|v.include?('=')}
|
6
9
|
tokens.each do |token|
|
7
10
|
if match = VARIABLE.match(token) || LONG.match(token)
|
8
11
|
name, type = match[:k], match[:t]
|
@@ -19,6 +22,7 @@ module HelpParser
|
|
19
22
|
return k2t
|
20
23
|
end
|
21
24
|
|
25
|
+
# t2r is an acronym for "type to regexp"
|
22
26
|
def self.t2r(specs)
|
23
27
|
if types = specs[TYPES]
|
24
28
|
t2r = NoDupHash.new
|
data/lib/help_parser/macros.rb
CHANGED
@@ -66,9 +66,7 @@ module HelpParser
|
|
66
66
|
code = <<-CODE
|
67
67
|
class Options
|
68
68
|
def #{name}
|
69
|
-
|
70
|
-
raise if f.nil?
|
71
|
-
f.to_f
|
69
|
+
@hash['#{name}']&.to_f or raise
|
72
70
|
rescue
|
73
71
|
raise UsageError, MSG[NOT_FLOAT,'#{name}']
|
74
72
|
end
|
@@ -83,9 +81,7 @@ module HelpParser
|
|
83
81
|
code = <<-CODE
|
84
82
|
class Options
|
85
83
|
def #{name}?
|
86
|
-
|
87
|
-
f = f.to_f if f
|
88
|
-
return f
|
84
|
+
@hash['#{name}']&.to_f
|
89
85
|
rescue
|
90
86
|
raise UsageError, MSG[NOT_FLOAT,'#{name}']
|
91
87
|
end
|
@@ -100,9 +96,7 @@ module HelpParser
|
|
100
96
|
code = <<-CODE
|
101
97
|
class Options
|
102
98
|
def #{name}
|
103
|
-
|
104
|
-
raise unless f.is_a?(Array)
|
105
|
-
f.map{_1.to_f}
|
99
|
+
@hash['#{name}'].map{_1.to_f}
|
106
100
|
rescue
|
107
101
|
raise UsageError, MSG[#{NOT_FLOATS},'#{name}']
|
108
102
|
end
|
@@ -117,10 +111,7 @@ module HelpParser
|
|
117
111
|
code = <<-CODE
|
118
112
|
class Options
|
119
113
|
def #{name}?
|
120
|
-
|
121
|
-
return nil unless f
|
122
|
-
raise unless f.is_a?(Array)
|
123
|
-
f.map{_1.to_f}
|
114
|
+
@hash['#{name}']&.map{_1.to_f}
|
124
115
|
rescue
|
125
116
|
raise UsageError, MSG[NOT_FLOATS,'#{name}']
|
126
117
|
end
|
@@ -135,9 +126,7 @@ module HelpParser
|
|
135
126
|
code = <<-CODE
|
136
127
|
class Options
|
137
128
|
def #{name}
|
138
|
-
|
139
|
-
raise if f.nil?
|
140
|
-
f.to_i
|
129
|
+
@hash['#{name}']&.to_i or raise
|
141
130
|
rescue
|
142
131
|
raise UsageError, MSG[NOT_INTEGER,'#{name}']
|
143
132
|
end
|
@@ -152,9 +141,7 @@ module HelpParser
|
|
152
141
|
code = <<-CODE
|
153
142
|
class Options
|
154
143
|
def #{name}?
|
155
|
-
|
156
|
-
f = f.to_i if f
|
157
|
-
return f
|
144
|
+
@hash['#{name}']&.to_i
|
158
145
|
rescue
|
159
146
|
raise UsageError, MSG[NOT_INTEGER,'#{name}']
|
160
147
|
end
|
@@ -169,9 +156,7 @@ module HelpParser
|
|
169
156
|
code = <<-CODE
|
170
157
|
class Options
|
171
158
|
def #{name}
|
172
|
-
|
173
|
-
raise unless f.is_a?(Array)
|
174
|
-
f.map{_1.to_i}
|
159
|
+
@hash['#{name}'].map{_1.to_i}
|
175
160
|
rescue
|
176
161
|
raise UsageError, MSG[NOT_INTEGERS,'#{name}']
|
177
162
|
end
|
@@ -186,10 +171,7 @@ module HelpParser
|
|
186
171
|
code = <<-CODE
|
187
172
|
class Options
|
188
173
|
def #{name}?
|
189
|
-
|
190
|
-
return nil unless f
|
191
|
-
raise unless f.is_a?(Array)
|
192
|
-
f.map{_1.to_i}
|
174
|
+
@hash['#{name}']&.map{_1.to_i}
|
193
175
|
rescue
|
194
176
|
raise UsageError, MSG[NOT_INTEGERS,'#{name}']
|
195
177
|
end
|
data/lib/help_parser/options.rb
CHANGED
@@ -2,14 +2,13 @@ module HelpParser
|
|
2
2
|
class Options
|
3
3
|
def initialize(version, help, argv)
|
4
4
|
@hash = HelpParser.parsea(argv)
|
5
|
-
if version &&
|
5
|
+
if version && VSN.any?{@hash.has_key? _1}
|
6
6
|
# -v or --version
|
7
7
|
raise VersionException, version
|
8
8
|
end
|
9
9
|
if help
|
10
|
-
|
11
|
-
|
12
|
-
HelpParser.parseh(help, validate: true) if h.all?{@hash.key? _1}
|
10
|
+
if HLP.any?{@hash.key? _1}
|
11
|
+
HelpParser.parseh(help, validate: true) if HLP.all?{@hash.key? _1}
|
13
12
|
raise HelpException, help
|
14
13
|
end
|
15
14
|
specs = HelpParser.parseh(help)
|
data/lib/help_parser/parseh.rb
CHANGED
@@ -4,8 +4,8 @@ module HelpParser
|
|
4
4
|
help.each_line do |line|
|
5
5
|
line.chomp!
|
6
6
|
next if line==''
|
7
|
-
if line
|
8
|
-
name =
|
7
|
+
if md = SECTION_NAME.match(line)
|
8
|
+
name = md[:name].downcase
|
9
9
|
specs[name] = []
|
10
10
|
else
|
11
11
|
next if name==''
|
@@ -15,7 +15,7 @@ module HelpParser
|
|
15
15
|
raise HelpError, EXTRANEOUS_SPACES if validate and spec==''
|
16
16
|
case name
|
17
17
|
when USAGE
|
18
|
-
Validate.
|
18
|
+
Validate.balanced_brackets(spec.chars) if validate
|
19
19
|
tokens = HelpParser.parseu(spec.chars)
|
20
20
|
Validate.usage_tokens(tokens) if validate
|
21
21
|
specs[USAGE].push tokens
|
data/lib/help_parser/parseu.rb
CHANGED
data/lib/help_parser/validate.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module HelpParser
|
2
2
|
module Validate
|
3
|
-
def self.
|
3
|
+
def self.balanced_brackets(chars)
|
4
4
|
count = 0
|
5
5
|
chars.each do |c|
|
6
6
|
if c=='['
|
@@ -16,12 +16,9 @@ module Validate
|
|
16
16
|
def self.usage_tokens(tokens)
|
17
17
|
words = []
|
18
18
|
tokens.flatten.each do |token|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
token.match(FLAG_GROUP)
|
23
|
-
raise HelpError, MSG[UNRECOGNIZED_TOKEN,token] unless match
|
24
|
-
words.push match[:k] # key
|
19
|
+
raise HelpError, MSG[UNRECOGNIZED_TOKEN,token] unless
|
20
|
+
[FLAG,LITERAL,VARIABLE,FLAG_GROUP]
|
21
|
+
.detect{_=token.match(_1) and words.push(_[:k])}
|
25
22
|
end
|
26
23
|
words.each_with_index do |word,i|
|
27
24
|
raise HelpError, MSG[DUP_WORD,word] unless i==words.rindex(word)
|
@@ -29,8 +26,8 @@ module Validate
|
|
29
26
|
end
|
30
27
|
|
31
28
|
def self.usage_specs(specs)
|
32
|
-
|
33
|
-
|
29
|
+
flags = specs.select{|a,b| not RESERVED.include? a}.values.flatten
|
30
|
+
.select{|f|f[0]=='-'}.map{|f| F2K[f]}
|
34
31
|
FLAG_CLUMPS.each do |k|
|
35
32
|
if a=specs[k]
|
36
33
|
seen = {}
|
@@ -74,7 +71,7 @@ module Validate
|
|
74
71
|
raise HelpError, MSG[UNCOMPLETED_TYPES,c.join(',')]
|
75
72
|
end
|
76
73
|
specs.each do |section,tokens|
|
77
|
-
next if
|
74
|
+
next if RESERVED.include? section
|
78
75
|
tokens.each do |words|
|
79
76
|
next if words.size<2
|
80
77
|
default = words[-1]
|
data/lib/help_parser.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: help_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 8.
|
4
|
+
version: 8.1.221206
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- CarlosJHR64
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-12-06 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: "Welcome to Help Parser! \nDo you have your help text? \nLet's parse!\n"
|
14
14
|
email: carlosjhr64@gmail.com
|
@@ -34,7 +34,7 @@ homepage: https://github.com/carlosjhr64/help_parser
|
|
34
34
|
licenses:
|
35
35
|
- MIT
|
36
36
|
metadata: {}
|
37
|
-
post_install_message:
|
37
|
+
post_install_message:
|
38
38
|
rdoc_options: []
|
39
39
|
require_paths:
|
40
40
|
- lib
|
@@ -49,9 +49,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
49
49
|
- !ruby/object:Gem::Version
|
50
50
|
version: '0'
|
51
51
|
requirements:
|
52
|
-
- 'ruby: ruby 3.
|
53
|
-
rubygems_version: 3.
|
54
|
-
signing_key:
|
52
|
+
- 'ruby: ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [aarch64-linux]'
|
53
|
+
rubygems_version: 3.3.7
|
54
|
+
signing_key:
|
55
55
|
specification_version: 4
|
56
56
|
summary: Welcome to Help Parser! Do you have your help text? Let's parse!
|
57
57
|
test_files: []
|