Class: Lich::Common::ArgParser
- Inherits:
-
Object
- Object
- Lich::Common::ArgParser
- Defined in:
- documented/common/arg_parser.rb
Overview
ArgParser provides argument parsing for lich scripts.
Matches script arguments against definition patterns and returns an OpenStruct of matched values, or displays help and exits if no match is found.
Instance Method Summary collapse
-
#display_args(data) ⇒ void
Displays the argument definitions and their descriptions.
-
#parse_args(data, flex_args = false) ⇒ OpenStruct?
Parses the provided arguments against defined patterns.
Instance Method Details
#display_args(data) ⇒ void
This method returns an undefined value.
Displays the argument definitions and their descriptions.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'documented/common/arg_parser.rb', line 65 def display_args(data) return if Script.current.name == "bootstrap" data.each do |def_set| def_set .select { |x| x[:name].to_s == "script_summary" } .each { |x| respond " SCRIPT SUMMARY: #{x[:description]} " } respond '' respond " SCRIPT CALL FORMAT AND ARG DESCRIPTIONS (arguments in brackets are optional):" respond " ;#{Script.current.name} " + def_set.map { |x| format_item(x) unless x[:name].to_s == "script_summary" }.join(' ') def_set .reject { |x| x[:name].to_s == "script_summary" } .each { |x| respond " #{(x[:display] || x[:name]).ljust(12)} #{x[:description]} #{x[:options] ? '[' + x[:options].join(', ') + ']' : ''}" } end # Display help output for settings used in the script. Relies on base-help.yaml. if respond_to?(:get_data, true) yaml_data = get_data('help').to_h yaml_settings = yaml_data.select { |_field, info| info.is_a?(Hash) && info["referenced_by"]&.include?(Script.current.name) } unless yaml_settings.empty? respond '' respond " YAML SETTINGS USED:" yaml_settings.each do |field, info| setting_line = " #{field}: #{info["description"]} #{info.dig("specific_descriptions", Script.current.name)}" setting_line += " [Ex: #{info["example"]}]" unless info["example"].to_s.empty? respond setting_line end respond "" end end end |
#parse_args(data, flex_args = false) ⇒ OpenStruct?
Parses the provided arguments against defined patterns.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'documented/common/arg_parser.rb', line 39 def parse_args(data, flex_args = false) raw_args = variable.first baselist = variable.drop(1).dup || Array.new unless baselist.size == 1 && baselist.grep(/^help$|^\?$|^h$/).any? result = data.map { |definition| check_match(definition, baselist.dup, flex_args) }.compact return result.first if result.length == 1 if result.empty? echo "***INVALID ARGUMENTS DON'T MATCH ANY PATTERN***" respond "Provided Arguments: '#{raw_args}'" elsif result.length > 1 echo '***INVALID ARGUMENTS MATCH MULTIPLE PATTERNS***' respond "Provided Arguments: '#{raw_args}'" end end display_args(data) exit end |