Class: Argser::Parser
- Inherits:
-
Object
- Object
- Argser::Parser
- Defined in:
- lib/argser/parser.rb
Defined Under Namespace
Classes: ParserError
Constant Summary collapse
- TYPES =
Available types (:integer, :float, :boolean, :alpanumeric, :letters, :words, :range, :interval and :string)
{ integer: [/^\s*\d+\s*$/, lambda {|value, match, default| value.to_i || 0}], float: [/^\s*\d+\.\d+\s*$/, lambda {|value, match, default| value.to_f || 0.0}], boolean: [/^\s*$/, lambda {|value, match, default| !default || false}], letters: [/^\s*[A-Za-z]+\s*$/, lambda {|value, match, default| (!default) ? value.strip : (value || '').strip}], alphanumeric: [/^\s*\w+\s*$/, lambda {|value, match, default| (!default) ? value.strip : (value || '').strip}], words: [/^\s*[A-Za-z]+(?:\s+[A-Za-z]+)*\s*$/, lambda {|value, match, default| (!default) ? value.strip : (value || '').strip}], range: [/^\s*(?:(\d+)\.\.(\d+)|(\d*)\.\.|\.\.(\d+))\s*$/, lambda {|value, match, default| (!default) ? [(match[0] || match[3]).to_i, (match[2] || match[4]).to_i] : value || [0,0]}], interval: [/^\s*(?:(\d+(?:\.\d+)?)\.\.(\d+(?:\.\d+)?)|(\d*(?:\.\d+)?)\.\.|\.\.(\d+(?:\.\d+)?))\s*$/, lambda {|value, match, default| (!default) ? [(match[0] || match[3]).to_f, (match[2] || match[4]).to_f] : value || [0.0,0.0]}], string: [/^.*$/, lambda {|value, match, default| value || ''}] }
Instance Method Summary collapse
-
#dump ⇒ Object
Return both parameters and remaining values in a hash.
-
#help ⇒ Object
Display the help and exit.
-
#initialize(config) ⇒ Parser
constructor
Initialize the parser.
-
#param(param) ⇒ Object
Return the value of a parameter.
-
#param?(param) ⇒ Boolean
Check if a parameter was given.
-
#parse(args) ⇒ Object
Parse an array with arguments and associate each element to one of the parameters or to the remaining array.
-
#remaining ⇒ Object
Return the remaining values.
-
#remaining? ⇒ Boolean
Returns the number of remaining strings that were found.
-
#show_error(error) ⇒ Object
Print the error and how to find more information.
Constructor Details
#initialize(config) ⇒ Parser
Initialize the parser
24 25 26 27 28 29 |
# File 'lib/argser/parser.rb', line 24 def initialize(config) @tokens, @options, @params, @remaining = {}, {}, {}, {} (config[:options] || {}) init_params(config[:params] || {}) init_remaining(config[:remaining] || {}) end |
Instance Method Details
#dump ⇒ Object
Return both parameters and remaining values in a hash
101 102 103 104 105 106 107 |
# File 'lib/argser/parser.rb', line 101 def dump values = {} @params.each do |token, param| values[token] = param[:value] end {params: values, remaining: @remaining[:value]} end |
#help ⇒ Object
Display the help and exit
110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/argser/parser.rb', line 110 def help puts "#{@options[:description]}\n\n" unless @options[:description].empty? puts "#{@options[:usage]}\n\n" unless @options[:usage].empty? @params.each do |token, param| token = "--#{token.to_s}" shortcut = (param[:shortcut].is_a? Symbol) ? "-#{param[:shortcut]}" : '' help = param[:help] puts " #{shortcut.ljust(5, ' ')}#{token.ljust(20, ' ')}#{help}" end puts "\n #{''.ljust(25, ' ')}#{@remaining[:help]}" unless @remaining[:help].empty? puts "\n#{@options[:details]}" unless @options[:details].empty? exit end |
#param(param) ⇒ Object
Return the value of a parameter
86 87 88 |
# File 'lib/argser/parser.rb', line 86 def param(param) (@tokens.has_key? param) ? @params[@tokens[param]][:value] : '' end |
#param?(param) ⇒ Boolean
Check if a parameter was given
81 82 83 |
# File 'lib/argser/parser.rb', line 81 def param?(param) (@tokens.has_key? param) ? !!@params[@tokens[param]][:given] : false end |
#parse(args) ⇒ Object
Parse an array with arguments and associate each element to one of the parameters or to the remaining array
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/argser/parser.rb', line 32 def parse(args) begin #Identify each element in the array of arguments last_param = nil args.each do |arg| if (arg =~ /^--?\w+$/) arg = arg.downcase raise_error(@options[:unknown], arg) unless @tokens.has_key? arg param = @params[@tokens[arg]] param[:given] = arg if (@params[@tokens[arg]][:type] != :boolean) last_param = param end #If parameter :help was given and the parser is supposed to automatically display the help, do so help if (param? :help) and (@options[:help]) else if (last_param) last_param[:value] = arg last_param = nil else @remaining[:value] = [] if @remaining[:given] == 0 @remaining[:given] += 1 @remaining[:value].push arg end end end #Iterate over all parameters and process their values @params.each do |token, param| raise_error(param[:errors][:required], "--#{param[:token].to_s}") unless param?(token) or !param[:required] validate_param(param) sanitize(param) end #Process the value of the remaining array raise_error(@remaining[:errors][:required]) unless (@remaining[:minimum] <= remaining?) and (remaining? <= @remaining[:maximum]) validate_remaining() sanitize(@remaining) #Invoke all callbacks invoke_callbacks rescue ParserError => error show_error error. end end |
#remaining ⇒ Object
Return the remaining values
96 97 98 |
# File 'lib/argser/parser.rb', line 96 def remaining() @remaining[:value] end |
#remaining? ⇒ Boolean
Returns the number of remaining strings that were found
91 92 93 |
# File 'lib/argser/parser.rb', line 91 def remaining?() @remaining[:given] end |
#show_error(error) ⇒ Object
Print the error and how to find more information
125 126 127 128 129 |
# File 'lib/argser/parser.rb', line 125 def show_error(error) puts error puts @options[:info] unless @options[:info].empty? exit end |