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
data/lib/help_parser/macros.rb
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Option value conversion macros for HelpParser::Options.
|
|
1
4
|
module HelpParser
|
|
5
|
+
def self.integer(*names) = map(*names, map: :to_i)
|
|
6
|
+
|
|
2
7
|
def self.map(*names, map:)
|
|
8
|
+
define_converter(names, &map)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def self.float(*names) = map(*names, map: :to_f)
|
|
12
|
+
def self.rational(*names) = map(*names, map: :to_r)
|
|
13
|
+
|
|
14
|
+
def self.define_converter(names, &convert)
|
|
3
15
|
names.each do |name|
|
|
4
|
-
Options.
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
v.map(&map) :
|
|
8
|
-
v.method(map).call)
|
|
9
|
-
end
|
|
16
|
+
Options.define_method(name) do
|
|
17
|
+
value = @hash[name.to_s]
|
|
18
|
+
value.is_a?(Array) ? value.map(&convert) : value && convert.call(value)
|
|
10
19
|
end
|
|
11
20
|
end
|
|
12
21
|
end
|
|
13
|
-
|
|
14
|
-
def self.float(*names) = HelpParser.map(*names, map: :to_f)
|
|
15
|
-
def self.rational(*names) = HelpParser.map(*names, map: :to_r)
|
|
22
|
+
private_class_method :define_converter
|
|
16
23
|
|
|
17
24
|
def self.split(*names, sep:, map:)
|
|
18
|
-
names.
|
|
19
|
-
Options.instance_eval do
|
|
20
|
-
define_method(name) do
|
|
21
|
-
v = @hash[name.to_s] and (v.is_a?(Array) ?
|
|
22
|
-
v.map{_1.split(sep).map(&map)} :
|
|
23
|
-
v.split(sep).map(&map))
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
end
|
|
25
|
+
define_converter(names) { it.split(sep).map(&map) }
|
|
27
26
|
end
|
|
28
|
-
|
|
27
|
+
|
|
28
|
+
def self.csv(*names) = split(*names, sep: ',', map: :strip)
|
|
29
29
|
end
|
data/lib/help_parser/options.rb
CHANGED
|
@@ -1,63 +1,93 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module HelpParser
|
|
4
|
+
# Parsed command-line options with hash-like and method-style access.
|
|
2
5
|
class Options
|
|
3
|
-
def
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
def [](key) = @hash[key]
|
|
7
|
+
|
|
8
|
+
def []=(key, value)
|
|
9
|
+
@hash[key] = value
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# :reek:BooleanParameter
|
|
13
|
+
def respond_to_missing?(name, include_all = false)
|
|
14
|
+
name.end_with?('!') ? super : true
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def method_missing(name, *args)
|
|
18
|
+
super unless respond_to_missing?(name)
|
|
19
|
+
if args.any? || block_given?
|
|
20
|
+
raise ArgumentError, 'expected neither args nor block'
|
|
8
21
|
end
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
end
|
|
37
|
-
end
|
|
22
|
+
|
|
23
|
+
key = name.to_s
|
|
24
|
+
key.end_with?('?') ? @hash.key?(key.chop) : @hash[key]
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def to_h = @hash
|
|
28
|
+
|
|
29
|
+
def initialize(version, help, argv)
|
|
30
|
+
@hash = ParseArgv.new(argv).to_h
|
|
31
|
+
check_version(version) if version
|
|
32
|
+
apply_help(String(help)) if help
|
|
33
|
+
$VERBOSE = true if @hash[VRBS] == true
|
|
34
|
+
$DEBUG = true if @hash[DBG] == true
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
def check_version(version)
|
|
40
|
+
return unless VSN.any? { @hash.key? it }
|
|
41
|
+
|
|
42
|
+
raise VersionException, String(version)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def check_help(help)
|
|
46
|
+
case HLP.count { @hash.key? it }
|
|
47
|
+
when 0 then return
|
|
48
|
+
when HLP.length then Validate.new(help)
|
|
38
49
|
end
|
|
39
|
-
|
|
40
|
-
|
|
50
|
+
|
|
51
|
+
raise HelpException, help
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def apply_help(help)
|
|
55
|
+
check_help(help)
|
|
56
|
+
specs = ParseHelp.new(help).specs
|
|
57
|
+
Completion.new(@hash, specs)
|
|
58
|
+
check_clumps(specs)
|
|
41
59
|
end
|
|
42
60
|
|
|
43
|
-
def
|
|
44
|
-
|
|
61
|
+
def check_exclusive(groups)
|
|
62
|
+
groups&.each do |keys|
|
|
63
|
+
next if keys.count { @hash.key? it } <= 1
|
|
64
|
+
|
|
65
|
+
raise UsageError, MSG[EXCLUSIVE_KEYS, *keys]
|
|
66
|
+
end
|
|
45
67
|
end
|
|
46
68
|
|
|
47
|
-
def
|
|
48
|
-
|
|
69
|
+
def check_clumps(specs)
|
|
70
|
+
check_exclusive(specs[EXCLUSIVE])
|
|
71
|
+
check_inclusive(specs[INCLUSIVE])
|
|
72
|
+
check_conditional(specs[CONDITIONAL])
|
|
49
73
|
end
|
|
50
74
|
|
|
51
|
-
def
|
|
52
|
-
|
|
75
|
+
def check_inclusive(groups)
|
|
76
|
+
groups&.each do |keys|
|
|
77
|
+
count = keys.count { @hash.key? it }
|
|
78
|
+
next if count.zero? || count == keys.length
|
|
79
|
+
|
|
80
|
+
raise UsageError, MSG[INCLUSIVE_KEYS, *keys]
|
|
81
|
+
end
|
|
53
82
|
end
|
|
54
83
|
|
|
55
|
-
def
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
84
|
+
def check_conditional(groups)
|
|
85
|
+
groups&.each do |keys|
|
|
86
|
+
next unless @hash.key?(keys.first)
|
|
87
|
+
next if keys.all? { @hash.key? it }
|
|
88
|
+
|
|
89
|
+
raise UsageError, MSG[CONDITIONAL_KEYS, *keys]
|
|
59
90
|
end
|
|
60
|
-
m[-1]=='?' ? @hash.key?(m[0..-2].to_s) : @hash[m.to_s]
|
|
61
91
|
end
|
|
62
92
|
end
|
|
63
93
|
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module HelpParser
|
|
4
|
+
# Parses command-line arguments into an ArgvHash.
|
|
5
|
+
class ParseArgv
|
|
6
|
+
STOP_FLAGS = %w[- --].freeze
|
|
7
|
+
|
|
8
|
+
def to_h = @hash.to_h
|
|
9
|
+
|
|
10
|
+
def initialize(argv)
|
|
11
|
+
@hash = ArgvHash.new
|
|
12
|
+
@index = 0
|
|
13
|
+
parse(argv)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
def parse(argv)
|
|
19
|
+
argv.each do |arg|
|
|
20
|
+
if arg.start_with?('-')
|
|
21
|
+
break if STOP_FLAGS.include?(arg)
|
|
22
|
+
|
|
23
|
+
parse_flag(arg[1..])
|
|
24
|
+
else
|
|
25
|
+
add_positional(arg)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def add_positional(arg)
|
|
31
|
+
@hash[@index] = arg
|
|
32
|
+
@index += 1
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def parse_flag(flag)
|
|
36
|
+
if flag.start_with?('-')
|
|
37
|
+
parse_long(flag[1..])
|
|
38
|
+
else
|
|
39
|
+
parse_short(flag)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def parse_short(chars)
|
|
44
|
+
chars.each_char { |char| @hash[char] = true }
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def parse_long(spec)
|
|
48
|
+
if spec.include?('=')
|
|
49
|
+
key, value = spec.split('=', 2)
|
|
50
|
+
@hash[key] = value
|
|
51
|
+
else
|
|
52
|
+
@hash[spec] = true
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module HelpParser
|
|
4
|
+
# Parses a help text string into a section-keyed NoDupHash of specs.
|
|
5
|
+
class ParseHelp
|
|
6
|
+
USAGE_SCAN = /\[|\]|[^ \[\]]+/
|
|
7
|
+
|
|
8
|
+
attr_reader :specs
|
|
9
|
+
|
|
10
|
+
def initialize(help)
|
|
11
|
+
@specs = NoDupHash.new
|
|
12
|
+
@section = ''
|
|
13
|
+
parse(help)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
attr_reader :section
|
|
19
|
+
|
|
20
|
+
def parse(help)
|
|
21
|
+
help.each_line do |line|
|
|
22
|
+
stripped = line.chomp
|
|
23
|
+
next if stripped.empty?
|
|
24
|
+
break if process_line(stripped) == :stop
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def process_line(line)
|
|
29
|
+
if (match = SECTION_NAME.match(line))
|
|
30
|
+
@section = match[:name].downcase
|
|
31
|
+
@specs[@section] = []
|
|
32
|
+
elsif !@section.empty?
|
|
33
|
+
return :stop if line.start_with?('#')
|
|
34
|
+
|
|
35
|
+
parse_indented(line) if line.start_with?(' ')
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def check_empty_spec(_comment) = nil
|
|
40
|
+
|
|
41
|
+
def parse_indented(line)
|
|
42
|
+
spec, comment = line.split("\t", 2).map(&:strip)
|
|
43
|
+
if spec.empty?
|
|
44
|
+
check_empty_spec(comment)
|
|
45
|
+
else
|
|
46
|
+
add_spec(spec)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def add_spec(spec)
|
|
51
|
+
entry =
|
|
52
|
+
@section == USAGE ? parse_usage(spec.scan(USAGE_SCAN)) : spec.split(CSV)
|
|
53
|
+
@specs[@section].push(entry)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def parse_usage(tokens)
|
|
57
|
+
result = []
|
|
58
|
+
while (token = tokens.shift)
|
|
59
|
+
result.push(case token
|
|
60
|
+
when ']' then return result
|
|
61
|
+
when '[' then parse_usage(tokens)
|
|
62
|
+
else token
|
|
63
|
+
end)
|
|
64
|
+
end
|
|
65
|
+
result
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module HelpParser
|
|
4
|
+
# Maps option/variable keys to type names and type names to compiled regexes.
|
|
5
|
+
class TypeMap
|
|
6
|
+
attr_reader :key_types, :regexes
|
|
7
|
+
|
|
8
|
+
def self.record_key_type(map, token)
|
|
9
|
+
match = VARIABLE.match(token) || LONG.match(token)
|
|
10
|
+
raise SoftwareError, MSG[UNEXPECTED, token] unless match
|
|
11
|
+
|
|
12
|
+
name, type = match.values_at(:k, :t)
|
|
13
|
+
return map[name] = type unless (existing = map[name])
|
|
14
|
+
|
|
15
|
+
raise HelpError, MSG[INCONSISTENT, name, type, existing] unless
|
|
16
|
+
type == existing
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def self.compile_regex(type, pattern)
|
|
20
|
+
Regexp.new(pattern[1..-2])
|
|
21
|
+
rescue StandardError
|
|
22
|
+
raise HelpError, MSG[BAD_REGEX, type, pattern]
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def initialize(specs)
|
|
26
|
+
@specs = specs
|
|
27
|
+
@regexes = build_regexes
|
|
28
|
+
@key_types = build_key_types
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def build_key_types
|
|
34
|
+
typed_tokens.each_with_object(NoDupHash.new) do |token, map|
|
|
35
|
+
self.class.record_key_type(map, token)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def build_regexes
|
|
40
|
+
types = @specs[TYPES]
|
|
41
|
+
return unless types
|
|
42
|
+
|
|
43
|
+
types.each_with_object(NoDupHash.new) do |(type, pattern), map|
|
|
44
|
+
map[type] = self.class.compile_regex(type, pattern)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def typed_tokens
|
|
49
|
+
@specs.select { |key, _| key == USAGE || !RESERVED.include?(key) }
|
|
50
|
+
.values.flatten.select { it.include?('=') }
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
data/lib/help_parser/validate.rb
CHANGED
|
@@ -1,95 +1,216 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Options parsing based on your help text.
|
|
1
4
|
module HelpParser
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
|
11
18
|
|
|
12
|
-
|
|
13
|
-
|
|
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])}
|
|
19
|
+
def check_empty_spec(comment)
|
|
20
|
+
raise HelpError, EXTRANEOUS_SPACES if comment.to_s.empty?
|
|
18
21
|
end
|
|
19
|
-
|
|
20
|
-
|
|
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
|
|
32
|
+
|
|
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
|
|
45
|
+
end
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
specs_usage&.flatten&.each do |token|
|
|
47
|
-
case token
|
|
48
|
-
when VARIABLE
|
|
49
|
-
key,bool = $~[:k],$~[:p].nil?
|
|
50
|
-
if var.key? key
|
|
51
|
-
unless var[key]==bool
|
|
52
|
-
raise HelpError, MSG[INCONSISTENT,"<#{key}>","<#{key}>+"]
|
|
53
|
-
end
|
|
54
|
-
else
|
|
55
|
-
var[key]=bool
|
|
56
|
-
end
|
|
57
|
-
when FLAG_GROUP
|
|
58
|
-
key = $~[:k]
|
|
59
|
-
raise HelpError, MSG[UNDEFINED_SECTION,key] unless specs[key]
|
|
60
|
-
group.push(key)
|
|
139
|
+
|
|
140
|
+
def match_usage_token(token)
|
|
141
|
+
USAGE_TOKENS.each do |pattern|
|
|
142
|
+
match = pattern.match(token)
|
|
143
|
+
return match[:k] if match
|
|
61
144
|
end
|
|
145
|
+
raise HelpError, MSG[UNRECOGNIZED_TOKEN, token]
|
|
62
146
|
end
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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])
|
|
153
|
+
end
|
|
67
154
|
end
|
|
68
|
-
end
|
|
69
155
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
next if default[0]=='-'
|
|
82
|
-
long_type = words[-2]
|
|
83
|
-
i = long_type.index('=')
|
|
84
|
-
next if i.nil?
|
|
85
|
-
long = long_type[2..(i-1)]
|
|
86
|
-
type = long_type[(i+1)..]
|
|
87
|
-
regex = t2r[type]
|
|
88
|
-
unless regex=~default
|
|
89
|
-
raise HelpError, MSG[BAD_DEFAULT,long,default,type,regex.inspect]
|
|
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}>+"]
|
|
90
167
|
end
|
|
168
|
+
else
|
|
169
|
+
@vars[key] = single
|
|
91
170
|
end
|
|
92
171
|
end
|
|
93
172
|
end
|
|
94
|
-
|
|
173
|
+
Validate.private_constant :UsageCheck
|
|
174
|
+
|
|
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 }
|
|
202
|
+
end
|
|
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
|
|
214
|
+
end
|
|
215
|
+
Validate.private_constant :TypeCheck
|
|
95
216
|
end
|