help_parser 8.2.230210 → 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.
@@ -1,85 +1,216 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Options parsing based on your help text.
1
4
  module HelpParser
2
- module Validate
3
- def self.balanced_brackets(chars)
4
- count = 0
5
- chars.each do |c|
6
- c=='[' && (count+=1) or c==']' && (count-=1)
7
- break if count.negative?
8
- end
9
- raise HelpError, MSG[UNBALANCED,chars.join] unless count.zero?
10
- end
5
+ # Validates help text syntax, usage specs, flag clumps, and types.
6
+ class Validate < ParseHelp
7
+ BRACKET_DELTA = { '[' => 1, ']' => -1 }.freeze
8
+ OPTION_SPECS = [SHORT, LONG, SHORT_LONG, SHORT_LONG_DEFAULT].freeze
9
+ USAGE_TOKENS = [LITERAL, VARIABLE, FLAG_GROUP].freeze
10
+
11
+ def initialize(help)
12
+ super
13
+ UsageCheck.new(specs).call
14
+ TypeCheck.new(specs).call if specs.key?(TYPES)
15
+ end
16
+
17
+ private
18
+
19
+ def check_empty_spec(comment)
20
+ raise HelpError, EXTRANEOUS_SPACES if comment.to_s.empty?
21
+ end
22
+
23
+ # :reek:FeatureEnvy
24
+ def check_balanced_brackets(spec)
25
+ balanced = spec.each_char.reduce(0) do |depth, char|
26
+ next -1 if depth.negative?
27
+
28
+ depth + BRACKET_DELTA.fetch(char, 0)
29
+ end
30
+ raise HelpError, MSG[UNBALANCED, spec] unless balanced.zero?
31
+ end
11
32
 
12
- def self.usage_tokens(tokens)
13
- words = []
14
- tokens.flatten.each do |token|
15
- raise HelpError, MSG[UNRECOGNIZED_TOKEN,token] unless
16
- [FLAG,LITERAL,VARIABLE,FLAG_GROUP]
17
- .detect{(_=token.match _1) && words.push(_[:k])}
33
+ def add_spec(spec)
34
+ validate_spec(spec)
35
+ super
36
+ end
37
+
38
+ def validate_spec(spec)
39
+ case section
40
+ when USAGE then check_balanced_brackets(spec)
41
+ when TYPES then check_pattern(TYPE_DEF, UNRECOGNIZED_TYPE, spec)
42
+ when *FLAG_CLUMPS then check_pattern(X_DEF, UNRECOGNIZED_X, spec)
43
+ else check_option_spec(spec)
44
+ end
18
45
  end
19
- words.each_with_index do |word,i|
20
- raise HelpError, MSG[DUP_WORD,word] unless i==words.rindex(word)
46
+
47
+ def check_pattern(pattern, message, spec)
48
+ raise HelpError, MSG[message, spec] unless pattern.match?(spec)
49
+ end
50
+
51
+ def check_option_spec(spec)
52
+ return if OPTION_SPECS.any? { it.match?(spec) }
53
+
54
+ raise HelpError, MSG[UNRECOGNIZED_OPTION, spec]
21
55
  end
22
56
  end
23
57
 
24
- def self.usage_specs(specs)
25
- flags = specs.except(*RESERVED).values.flatten
26
- .select{_1[0]=='-'}.map{F2K[_1]}
27
- FLAG_CLUMPS.each do |k|
28
- next unless (a=specs[k])
58
+ # Validates flag clumps, flag uniqueness, usage references, and sections.
59
+ # rubocop:disable-next Style/ClassAndModuleChildren
60
+ class Validate::UsageCheck
61
+ USAGE_TOKENS = Validate::USAGE_TOKENS
62
+
63
+ def self.record_clump(group, seen)
64
+ key = group.sort.join(' ').to_sym
65
+ if seen[key] || group.length != group.uniq.length
66
+ raise HelpError, MSG[DUP_X, key]
67
+ end
68
+
69
+ seen[key] = true
70
+ end
71
+
72
+ def initialize(specs)
73
+ @specs = specs
74
+ @flags = defined_flags
75
+ @groups = []
76
+ @vars = {}
77
+ end
78
+
79
+ def self.check_unique(items, message)
80
+ items.each_with_index do |item, index|
81
+ raise HelpError, MSG[message, item] unless index == items.rindex(item)
82
+ end
83
+ end
84
+
85
+ def call
86
+ validate_usage_lines
87
+ validate_clumps
88
+ self.class.check_unique(@flags, DUP_FLAG)
89
+ validate_usage_references
90
+ validate_sections
91
+ end
92
+
93
+ private
94
+
95
+ def defined_flags
96
+ @specs.except(*RESERVED).values.flatten
97
+ .select { it.start_with?('-') }.map { F2K[it] }
98
+ end
99
+
100
+ def validate_clump_entry(group, seen)
101
+ self.class.record_clump(group, seen)
102
+ group.each { check_defined_flag(it) }
103
+ end
104
+
105
+ def validate_clumps
106
+ FLAG_CLUMPS.each { validate_clump(it) }
107
+ end
108
+
109
+ def check_defined_flag(flag)
110
+ raise HelpError, MSG[UNSEEN_FLAG, flag] unless @flags.include?(flag)
111
+ end
112
+
113
+ def validate_usage_lines
114
+ @specs[USAGE]&.each do |tokens|
115
+ words = tokens.flatten.map { match_usage_token(it) }
116
+ self.class.check_unique(words, DUP_WORD)
117
+ end
118
+ end
119
+
120
+ def validate_clump(name)
121
+ return unless (entries = @specs[name])
122
+
29
123
  seen = {}
30
- a.each do |xs|
31
- k = xs.sort.join(' ').to_sym
32
- if seen[k] || xs.length!=xs.uniq.length
33
- raise HelpError, MSG[DUP_X,k]
34
- end
35
- seen[k] = true
36
- xs.each do |x|
37
- raise HelpError, MSG[UNSEEN_FLAG, x] unless flags.include?(x)
38
- end
124
+ entries.each { validate_clump_entry(it, seen) }
125
+ end
126
+
127
+ def validate_usage_references
128
+ @specs[USAGE]&.flatten&.each { record_usage_token(it) }
129
+ end
130
+
131
+ def validate_sections
132
+ has_usage = @specs.key?(USAGE)
133
+ @specs.each do |key, tokens|
134
+ raise HelpError, MSG[MISSING_CASES, key] if tokens.empty?
135
+ next if !has_usage || RESERVED.include?(key)
136
+ raise HelpError, MSG[MISSING_USAGE, key] unless @groups.include?(key)
39
137
  end
40
138
  end
41
- flags.each_with_index do |flag,i|
42
- raise HelpError, MSG[DUP_FLAG,flag] unless i==flags.rindex(flag)
139
+
140
+ def match_usage_token(token)
141
+ USAGE_TOKENS.each do |pattern|
142
+ match = pattern.match(token)
143
+ return match[:k] if match
144
+ end
145
+ raise HelpError, MSG[UNRECOGNIZED_TOKEN, token]
43
146
  end
44
- group = []
45
- specs_usage = specs[USAGE]
46
- specs_usage&.flatten&.each do |token|
47
- if (match=token.match FLAG_GROUP)
48
- key = match[:k]
49
- raise HelpError, MSG[UNDEFINED_SECTION,key] unless specs[key]
50
- group.push(key)
147
+
148
+ def record_usage_token(token)
149
+ if (match = VARIABLE.match(token))
150
+ record_variable(*match.values_at(:k, :p))
151
+ elsif (match = FLAG_GROUP.match(token))
152
+ record_group(match[:k])
51
153
  end
52
154
  end
53
- specs.each do |key,tokens|
54
- raise HelpError, MSG[MISSING_CASES,key] if tokens.empty?
55
- next if specs_usage.nil? || RESERVED.include?(key)
56
- raise HelpError, MSG[MISSING_USAGE,key] unless group.include?(key)
155
+
156
+ def record_group(key)
157
+ raise HelpError, MSG[UNDEFINED_SECTION, key] unless @specs[key]
158
+
159
+ @groups.push(key)
160
+ end
161
+
162
+ def record_variable(key, plus)
163
+ single = !plus
164
+ if @vars.key?(key)
165
+ unless @vars[key] == single
166
+ raise HelpError, MSG[INCONSISTENT, "<#{key}>", "<#{key}>+"]
167
+ end
168
+ else
169
+ @vars[key] = single
170
+ end
57
171
  end
58
172
  end
173
+ Validate.private_constant :UsageCheck
59
174
 
60
- def self.k2t2r(specs, k2t, t2r)
61
- a,b = k2t.values.uniq.sort,t2r.keys.sort
62
- unless a==b
63
- c = (a+b).uniq.reject{|x|a.include?(x) && b.include?(x)}
64
- raise HelpError, MSG[UNCOMPLETED_TYPES,c.join(',')]
65
- end
66
- specs.each do |section,tokens|
67
- next if RESERVED.include? section
68
- tokens.each do |words|
69
- next if words.size<2
70
- default = words[-1]
71
- next if default[0]=='-'
72
- long_type = words[-2]
73
- i = long_type.index('=')
74
- next if i.nil?
75
- long = long_type[2..(i-1)]
76
- type = long_type[(i+1)..]
77
- regex = t2r[type]
78
- unless regex=~default
79
- raise HelpError, MSG[BAD_DEFAULT,long,default,type,regex.inspect]
80
- end
175
+ # Validates type coverage and default values against type regexes.
176
+ # rubocop:disable-next Style/ClassAndModuleChildren
177
+ class Validate::TypeCheck
178
+ def initialize(specs)
179
+ @specs = specs
180
+ @types = TypeMap.new(specs)
181
+ end
182
+
183
+ def call
184
+ check_coverage
185
+ check_defaults
186
+ end
187
+
188
+ private
189
+
190
+ def check_coverage
191
+ used = @types.key_types.values.uniq.sort
192
+ defined = @types.regexes.keys.sort
193
+ return if used == defined
194
+
195
+ diff = (used | defined) - (used & defined)
196
+ raise HelpError, MSG[UNCOMPLETED_TYPES, diff.join(',')]
197
+ end
198
+
199
+ def check_defaults
200
+ @specs.except(*RESERVED).each_value do |tokens|
201
+ tokens.each { check_default(*it.last(2)) if it.size >= 2 }
81
202
  end
82
203
  end
204
+
205
+ def check_default(spec, default)
206
+ long, type = spec.split('=', 2)
207
+ return if default.start_with?('-') || !type
208
+
209
+ regex = @types.regexes[type]
210
+ return if regex.match?(default)
211
+
212
+ raise HelpError, MSG[BAD_DEFAULT, long[2..], default, type, regex.inspect]
213
+ end
83
214
  end
84
- end
215
+ Validate.private_constant :TypeCheck
85
216
  end
data/lib/help_parser.rb CHANGED
@@ -1,27 +1,28 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'help_parser/constants'
2
4
  require_relative 'help_parser/exceptions'
3
5
  require_relative 'help_parser/aliases'
4
- require_relative 'help_parser/parsea'
5
- require_relative 'help_parser/parseu'
6
- require_relative 'help_parser/parseh'
7
- require_relative 'help_parser/k2t2r'
6
+ require_relative 'help_parser/parse_argv'
7
+ require_relative 'help_parser/parse_help'
8
+ require_relative 'help_parser/type_map'
8
9
  require_relative 'help_parser/completion'
9
10
  require_relative 'help_parser/options'
10
11
  require_relative 'help_parser/macros'
11
12
 
13
+ # HelpParser namespace for library
12
14
  module HelpParser
13
- VERSION = '8.2.230210'
15
+ VERSION = '10.0.261206'
14
16
  autoload :Validate, 'help_parser/validate'
15
17
 
18
+ # :reek:UncommunicativeVariableName e is standard
16
19
  def self.[](
17
20
  version = nil,
18
21
  help = nil,
19
- argv = [File.basename($0)]+ARGV)
22
+ argv = [File.basename($PROGRAM_NAME)] + ARGV
23
+ )
20
24
  Options.new(version, help, argv)
21
25
  rescue HelpParserException => e
22
26
  e.exit
23
27
  end
24
28
  end
25
-
26
- # Requires:
27
- # `ruby`
metadata CHANGED
@@ -1,116 +1,58 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: help_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 8.2.230210
4
+ version: 10.0.261206
5
5
  platform: ruby
6
6
  authors:
7
7
  - CarlosJHR64
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2023-02-10 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: colorize
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '0.8'
20
- - - ">="
21
- - !ruby/object:Gem::Version
22
- version: 0.8.1
23
- type: :development
24
- prerelease: false
25
- version_requirements: !ruby/object:Gem::Requirement
26
- requirements:
27
- - - "~>"
28
- - !ruby/object:Gem::Version
29
- version: '0.8'
30
- - - ">="
31
- - !ruby/object:Gem::Version
32
- version: 0.8.1
33
- - !ruby/object:Gem::Dependency
34
- name: parser
35
- requirement: !ruby/object:Gem::Requirement
36
- requirements:
37
- - - "~>"
38
- - !ruby/object:Gem::Version
39
- version: '3.2'
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- version: 3.2.0
43
- type: :development
44
- prerelease: false
45
- version_requirements: !ruby/object:Gem::Requirement
46
- requirements:
47
- - - "~>"
48
- - !ruby/object:Gem::Version
49
- version: '3.2'
50
- - - ">="
51
- - !ruby/object:Gem::Version
52
- version: 3.2.0
53
- - !ruby/object:Gem::Dependency
54
- name: test-unit
55
- requirement: !ruby/object:Gem::Requirement
56
- requirements:
57
- - - "~>"
58
- - !ruby/object:Gem::Version
59
- version: '3.5'
60
- - - ">="
61
- - !ruby/object:Gem::Version
62
- version: 3.5.7
63
- type: :development
64
- prerelease: false
65
- version_requirements: !ruby/object:Gem::Requirement
66
- requirements:
67
- - - "~>"
68
- - !ruby/object:Gem::Version
69
- version: '3.5'
70
- - - ">="
71
- - !ruby/object:Gem::Version
72
- version: 3.5.7
73
- description: "Welcome to Help Parser! \nDo you have your help text? \nLet's parse!\n"
10
+ date: 2026-12-06 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: 'Options parsing based on your help text.
13
+
14
+ '
74
15
  email: carlosjhr64@gmail.com
75
16
  executables: []
76
17
  extensions: []
77
18
  extra_rdoc_files: []
78
19
  files:
20
+ - ".version"
21
+ - CREDITS.md
79
22
  - README.md
80
23
  - lib/help_parser.rb
81
24
  - lib/help_parser/aliases.rb
82
25
  - lib/help_parser/completion.rb
83
26
  - lib/help_parser/constants.rb
84
27
  - lib/help_parser/exceptions.rb
85
- - lib/help_parser/k2t2r.rb
86
28
  - lib/help_parser/macros.rb
87
29
  - lib/help_parser/options.rb
88
- - lib/help_parser/parsea.rb
89
- - lib/help_parser/parseh.rb
90
- - lib/help_parser/parseu.rb
30
+ - lib/help_parser/parse_argv.rb
31
+ - lib/help_parser/parse_help.rb
32
+ - lib/help_parser/type_map.rb
91
33
  - lib/help_parser/validate.rb
92
34
  homepage: https://github.com/carlosjhr64/help_parser
93
35
  licenses:
94
36
  - MIT
95
37
  metadata: {}
96
- post_install_message:
97
38
  rdoc_options: []
98
39
  require_paths:
99
40
  - lib
100
41
  required_ruby_version: !ruby/object:Gem::Requirement
101
42
  requirements:
43
+ - - "~>"
44
+ - !ruby/object:Gem::Version
45
+ version: '4.0'
102
46
  - - ">="
103
47
  - !ruby/object:Gem::Version
104
- version: '0'
48
+ version: 4.0.0
105
49
  required_rubygems_version: !ruby/object:Gem::Requirement
106
50
  requirements:
107
51
  - - ">="
108
52
  - !ruby/object:Gem::Version
109
53
  version: '0'
110
- requirements:
111
- - 'git: 2.30'
112
- rubygems_version: 3.4.6
113
- signing_key:
54
+ requirements: []
55
+ rubygems_version: 4.0.20
114
56
  specification_version: 4
115
- summary: Welcome to Help Parser! Do you have your help text? Let's parse!
57
+ summary: Options parsing based on your help text.
116
58
  test_files: []
@@ -1,41 +0,0 @@
1
- module HelpParser
2
- # k2t is an acronym for the "key to type" mapping
3
- def self.k2t(specs)
4
- k2t = NoDupHash.new
5
- # If specs section is not a RESERVED section, it's an options list.
6
- tokens = specs.select{|k,_| k==USAGE or !RESERVED.include?(k)}
7
- # Tokens associating a key to a type.
8
- .values.flatten.select{|v|v.include?('=')}
9
- tokens.each do |token|
10
- if (match = VARIABLE.match(token) || LONG.match(token))
11
- name, type = match[:k], match[:t]
12
- if (_=k2t[name])
13
- raise HelpError, MSG[INCONSISTENT,name,type,_] unless type==_
14
- else
15
- k2t[name] = type
16
- end
17
- else
18
- # Expected these to be caught earlier...
19
- raise SoftwareError, MSG[UNEXPECTED,token]
20
- end
21
- end
22
- k2t
23
- end
24
-
25
- # t2r is an acronym for "type to regexp"
26
- def self.t2r(specs)
27
- if (types=specs[TYPES])
28
- t2r = NoDupHash.new
29
- types.each do |pair|
30
- type, pattern = *pair
31
- begin
32
- t2r[type] = Regexp.new(pattern[1..-2])
33
- rescue
34
- raise HelpError, MSG[BAD_REGEX,type,pattern]
35
- end
36
- end
37
- return t2r
38
- end
39
- nil
40
- end
41
- end
@@ -1,29 +0,0 @@
1
- module HelpParser
2
- def self.parsea(argv)
3
- hsh = ArgvHash.new
4
- n = 0
5
- argv.each do |a|
6
- if a[0]=='-'
7
- break if a.size==1 # '-' quits argv processing
8
- if a[1]=='-'
9
- break if a.size==2 # '--' also quits argv processing
10
- s = a[2..]
11
- if s.include?('=')
12
- k,v = s.split('=',2)
13
- hsh[k] = v
14
- else
15
- hsh[s] = true
16
- end
17
- else
18
- a.chars[1..].each do |c|
19
- hsh[c] = true
20
- end
21
- end
22
- else
23
- hsh[n] = a
24
- n += 1
25
- end
26
- end
27
- hsh
28
- end
29
- end
@@ -1,50 +0,0 @@
1
- module HelpParser
2
- def self.parseh(help, validate: false)
3
- specs,name = NoDupHash.new,''
4
- help.each_line do |line|
5
- line.chomp!
6
- next if line==''
7
- if (md=SECTION_NAME.match(line))
8
- name = md[:name].downcase
9
- specs[name] = []
10
- else
11
- next if name==''
12
- break if line[0]=='#'
13
- next unless line[0]==' '
14
- spec = (index=line.rindex("\t"))? line[0,index].strip : line.strip
15
- raise HelpError, EXTRANEOUS_SPACES if validate && spec.empty?
16
- case name
17
- when USAGE
18
- Validate.balanced_brackets(spec.chars) if validate
19
- tokens = HelpParser.parseu(spec.chars)
20
- Validate.usage_tokens(tokens) if validate
21
- specs[USAGE].push tokens
22
- when TYPES
23
- if validate && !TYPE_DEF.match?(spec)
24
- raise HelpError, MSG[UNRECOGNIZED_TYPE,spec]
25
- end
26
- specs[TYPES].push spec.split(CSV)
27
- when *FLAG_CLUMPS # EXCLUSIVE,INCLUSIVE,CONDITIONAL,...
28
- if validate && !X_DEF.match?(spec)
29
- raise HelpError, MSG[UNRECOGNIZED_X,spec]
30
- end
31
- specs[name].push spec.split(CSV)
32
- else
33
- if validate &&
34
- [SHORT, LONG, SHORT_LONG, SHORT_LONG_DEFAULT].none?{_1=~spec}
35
- raise HelpError, MSG[UNRECOGNIZED_OPTION,spec]
36
- end
37
- specs[name].push spec.split(CSV)
38
- end
39
- end
40
- end
41
- if validate
42
- Validate.usage_specs(specs)
43
- if (t2r=HelpParser.t2r(specs))
44
- k2t = HelpParser.k2t(specs)
45
- Validate.k2t2r(specs, k2t, t2r)
46
- end
47
- end
48
- specs
49
- end
50
- end
@@ -1,24 +0,0 @@
1
- module HelpParser
2
- # Chars := String.split(/\t/,2).first.strip.chars
3
- # Token := String=~/^[^ \[\]]$/
4
- # Note that emergent Token is String=~/^[^\s\[\]]$/
5
- # Tokens := Array(Token|Tokens)
6
- def self.parseu(chars)
7
- tokens,token = [],''
8
- while (c=chars.shift)
9
- case c
10
- when ' ','[',']'
11
- unless token==''
12
- tokens.push(token)
13
- token = ''
14
- end
15
- tokens.push HelpParser.parseu(chars) if c=='['
16
- return tokens if c==']'
17
- else
18
- token += c
19
- end
20
- end
21
- tokens.push(token) unless token==''
22
- tokens
23
- end
24
- end