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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f19d24ec56736d0d1bbcce1e0a847ac45e9d82defc89c7799c7b9c8c7c4ae078
4
- data.tar.gz: fe92cf2d740771329bee184dd205d59bc72c1265d79ac50af3a6c4d0fa735297
3
+ metadata.gz: 9403900fc387aec4da549c2df1cb349f03f5083616193c3cd47af69fbae890c5
4
+ data.tar.gz: 8b97b41c25813637166e344ac65df468a051a4e7eda8b78ce4d6ba64963989af
5
5
  SHA512:
6
- metadata.gz: f20dad3a1453e625236af9cb176328652352e9f2f35f00da02cc6ab1853671c82ae13d5ec8226247be116cbd82bdbb4aee24490200f04b2ddba79e380822f36b
7
- data.tar.gz: 0aebde782784afb644dd1047f32af8de07d07af4c53435ec47ed8c8f7e54a3997a6e6a08c372f23e1a38f37c10f6e0604c26afcb5f16b8d7677120891f10ed71
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,16 +1,17 @@
1
- # Help Parser VIII: Helpland
1
+ # Help Parser X: Judgment
2
2
 
3
- * [VERSION 8.2.230210](https://github.com/carlosjhr64/help_parser/releases)
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
 
7
7
  ## DESCRIPTION:
8
8
 
9
- Welcome to Help Parser!
10
- Do you have your help text?
11
- Let's parse!
9
+ Options parsing based on your help text.
12
10
 
13
11
  ## INSTALL:
12
+
13
+ * Required Ruby version: `>= 4.0`
14
+
14
15
  ```console
15
16
  $ gem install help_parser
16
17
  ```
@@ -18,94 +19,112 @@ $ gem install help_parser
18
19
  <!-- The following PREVIEW has been approved for ALL PROGRAMMERS by CarlosJHR64.
19
20
  For the README validator that checks against me lying....
20
21
  ```ruby
21
- unless File.basename($PROGRAM_NAME) == 'party'
22
+ unless File.basename($PROGRAM_NAME) == 'revelations'
22
23
  # For example's sake say
23
- $PROGRAM_NAME = 'party'
24
+ $PROGRAM_NAME = 'revelations'
24
25
  # and ARGV is
25
- ARGV.concat ["-\-age", "-\-date=2020-09-07", 'touch', 'that']
26
- # and proceed as if run as:
27
- # awesome -\-name=Doe -\-value a b c
26
+ ARGV.concat ['1,2,3','4,5,6']
27
+ # and proceed as if run as: `revelations 1,2,3 4,5,6`
28
28
  end
29
29
  ```
30
30
  The following gem has been rated
31
- | M | Mature |
32
- -->
31
+ | M | Mature | -->
33
32
  ```ruby
34
- require "help_parser"
33
+ #!/usr/bin/env ruby
34
+ require 'help_parser'
35
35
 
36
- HELP = <<-HELP
36
+ VERSION = '1.2.3'
37
+ OPTIONS = HelpParser[VERSION, <<-HELP]
37
38
  # <= Hash here, parser skips
38
- # HelpParser: Party command example #
39
+ # HelpParser command example #
40
+
41
+ One can write any notes on the help text as long as
42
+ it does not start with a space or a "Keyword:".
43
+
39
44
  Usage:
40
- party :options+ [<args>+]
41
- party [:alternate] <arg=FLOAT>
42
- party literal <arg1=WORD> <arg2=WORD>
45
+ revelations :options+ [<arg>]
46
+ revelations :alternate <args=FLOAT>+
47
+ revelations literal <arg1=WORD> <arg2=WORD>
48
+ revelations <numbers=CSVI>+
49
+
50
+ The ":keyword" refers to a flag in defined in the "Keyword:" section.
51
+ A "[...]" is an optional part of the usage.
52
+ A "+" means one or more of it is allowed.
53
+
43
54
  Options:
44
- -v --version \t Give version and quit
45
- -h --help \t Give help and quit
46
- -s --long \t Short long synonyms
47
- --touch that \t Defaulted
48
- --date=DATE \t Typed
49
- --age=INTEGER 80 \t Typed and Defaulted
50
- -a --all=YN y \t Short, long, typed, and defaulted
51
- --to_be
52
- --not_to_be
55
+ -v --version \t Give version and quit
56
+ -h --help \t Give help and quit
57
+ --verbose \t Set $VERBOSE true
58
+ --debug \t Set $DEBUG true
59
+
60
+ The above(version, help, verbose, debug) are built-in options.
61
+ The tab, "\t", splits the comment from the flags.
62
+ On its own a flag is set to true, else it's nil.
63
+ But you can also set a long flag value to a string as allowed by its type.
64
+
65
+ -a --all=YN y \t Short, long, typed, and defaulted
66
+
67
+ OPTIONS.all at first is nil. If set without a value, it's set to "y"
68
+ One can set it to to either "n" of "y" as allowed by YN(see below under Types:).
69
+
70
+ --stop=NUMBER \t Typed
71
+ --start=NUMBER 0 \t Typed and defaulted
72
+
53
73
  --rain
54
74
  --water
55
75
  --wet
76
+
77
+ --to_be
78
+ --not_to_be
79
+
56
80
  Exclusive:
57
- to_be not_to_be \t Tells parser these are mutually exclusive keys
81
+ to_be not_to_be \t Tells parser these are mutually exclusive keys
58
82
  Inclusive:
59
- date age \t Tells parser any of these must include all of these
83
+ start stop \t Tells parser any of these must include all of these
60
84
  Conditional:
61
- rain water wet \t Tells parser if first then all
85
+ rain water wet \t Tells parser if first then all
86
+ \t Note how one can continue the comment as needed
87
+
62
88
  Alternate:
63
- --invoke
64
- --wut
89
+ --sum
90
+ --multiply
91
+
65
92
  Types:
66
93
  WORD /^[A-Za-z]+$/
67
- DATE /^\\d\\d\\d\\d-\\d\\d-\\d\\d$/
68
- INTEGER /^\\d+$/
94
+ NUMBER /^\\d+$/
69
95
  FLOAT /^\\d+\\.\\d+$/
70
96
  YN /^[YNyn]$/
97
+ CSVI /^\\d+(,\\d+)*$/
71
98
  # <= Hash here, parser breaks out
72
- # Notes #
73
- I wouldn't touch that!
99
+ And now one can freely write whatever....
74
100
  HELP
75
101
 
76
- VERSION = "1.2.3"
77
-
78
- OPTIONS = HelpParser[VERSION, HELP] #~> HelpParser
79
-
80
- # Macros:
81
- HelpParser.strings?(:args) # for OPTIONS.args : Array(String) | Nil
82
- HelpParser.int?(:age) # for OPTIONS.age? : Integer | Nil
83
- HelpParser.float(:arg) # for options.arg : Float
84
- HelpParser.string(:arg1, :arg2, :arg3) # for OPTIONS.arg1, etc : String
85
- #=> [:arg1, :arg2, :arg3]
86
-
87
- ## If run as:
88
- ## party --age --date=2020-09-07 touch that
89
- OPTIONS.age? #=> 80
90
- OPTIONS.age?.class #=> Integer
91
- OPTIONS.args? #=> ["touch", "that"]
92
- OPTIONS.args?.class #=> Array
93
- OPTIONS.arg? and OPTIONS.arg #=> false
94
- OPTIONS.arg?.class #=> FalseClass
102
+ # Tell HelpParser how to remap the string values:
103
+ HelpParser.integer(:stop, :start) # HelpParser.map(:stop, :start, map: :to_i)
104
+ HelpParser.float(:args) # HelpParser.map(:args, map: :to_f)
105
+ # Also available: HelpParser.rational(*name) = HelpParser.map(*names, map: to_r)
106
+
107
+ # Tell HelpParser how to split the string values:
108
+ HelpParser.split(:numbers, sep: ',', map: :to_i)
109
+ # Also available: HelpParser.csv(*name) = HelpParser.split(*names)
110
+
111
+ # If one runs `revelations 1,2,3 4,5,6`, then:
112
+ OPTIONS.numbers #=> [[1, 2, 3], [4, 5, 6]]
113
+ # And everything else is unset:
114
+ OPTIONS.stop #=> nil
95
115
  ```
96
116
  ## Features
97
117
 
98
- * `ARGV` setup for `ARGF` when one of the "Types:" given is "ARGF"
99
- * `$DEBUG=true` on --debug
100
- * `$VERBOSE=true` on --verbose
118
+ * Allows `OPTIONS[key]`, `OPTIONS[key]=value`, and `OPTIONS.to_h`
101
119
  * -h and --help simultaneously will check help string for errors
120
+ * `ARGV` setup for `ARGF` when one of the "Types:" given is "ARGF"
102
121
  * `HelpParser::REDTTY[msg]` will red color output `msg` to `$stderr`.
103
122
 
104
123
  ## LICENSE:
105
124
 
106
125
  (The MIT License)
107
126
 
108
- Copyright (c) 2023 CarlosJHR64
127
+ Copyright (c) 2026 CarlosJHR64
109
128
 
110
129
  Permission is hereby granted, free of charge, to any person obtaining
111
130
  a copy of this software and associated documentation files (the
@@ -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 []=(k,v)
4
- raise HelpError, MSG[DUP_KEY,k] if key?(k)
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 []=(k,v)
11
- raise UsageError, MSG[DUP_KEY,k] if key?(k)
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,@specs = hash,specs
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
- usage or diagnose if @specs.key?(USAGE)
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
- # Prepare ARGV for ARGF.
15
- def handle_argf(k2t)
16
- files = @hash.select{|k,_|k2t[k]=='ARGF'}.map{|_,v|v}.flatten
17
- e = files.reject{File.exist?_1}.join(', ')
18
- raise UsageError, MSG[NOT_EXIST, e] unless e.empty?
19
- ARGV.replace files
20
- end
21
-
22
- # Which usage does the user's command options match?
23
- def usage
24
- @specs[USAGE].each do |cmd|
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
- dict = {}
40
- @specs.each do |section,list|
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 types
56
- t2r,k2t = HelpParser.t2r(@specs),HelpParser.k2t(@specs)
57
- @hash.each do |key,value|
58
- next unless key.is_a?(String) && (type=k2t[key])
59
- regex = t2r[type]
60
- case value
61
- when String
62
- unless value=~regex
63
- raise UsageError, "--#{key}=#{value} !~ #{type}=#{regex.inspect}"
64
- end
65
- when Array
66
- value.each do |string|
67
- unless string=~regex
68
- raise UsageError,
69
- "--#{key}=#{string} !~ #{type}=#{regex.inspect}"
70
- end
71
- end
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
- raise UsageError, "--#{key} !~ #{type}=#{regex.inspect}"
77
+ match_token(token)
78
+ @index += 1
74
79
  end
75
80
  end
76
- k2t
77
- end
78
-
79
- def pad
80
- # Synonyms and defaults:
81
- @specs.each do |section,options|
82
- next if RESERVED.any?{section==_1}
83
- options.each do |words|
84
- next unless words.size>1
85
- first,second,default = words[0],words[1],words[2]
86
- if first[0]=='-'
87
- if second[0]=='-'
88
- i = second.index('=') || 0
89
- short,long = first[1],second[2..(i-1)]
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 matches(cmd, i = 0)
119
- keys = @hash.keys
120
- cmd.each do |token|
121
- if token.is_a?(Array)
122
- begin
123
- i = matches(token, i)
124
- rescue NoMatch
125
- # OK, NEVERMIND!
126
- end
127
- next
128
- elsif (m=FLAG_GROUP.match token)
129
- group,plus = m[:k],m[:p]
130
- key = keys[i]
131
- raise NoMatch unless key.is_a? String
132
- list = @specs[group].flatten.select{|f|f[0]=='-'}.map{|f| F2K[f]}
133
- raise NoMatch unless list.include?(key)
134
- unless plus.nil?
135
- loop do
136
- key = keys[i+1]
137
- break unless key.is_a?(String) && list.include?(key)
138
- i+=1
139
- end
140
- end
141
- elsif (m=VARIABLE.match(token))
142
- key = keys[i]
143
- raise NoMatch unless key.is_a?(Integer)
144
- variable,plus = m[:k],m[:p]
145
- if plus.nil?
146
- @cache[variable] = @hash[key]
147
- else
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
- i
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