Class: ArgsParser::Parser

### Home Classes Methods

### In Files

- lib/args_parser/parser.rb
- lib/args_parser/styles/default.rb
- lib/args_parser/styles/equal.rb

### Parent

Object

### Methods

- ::new
- #[]
- #[]=
- #args
- #first
- #has_option?
- #has_param?
- #help
- #inspect
- #parse
- #parse_style_default
- #parse_style_equal

### Class/Module Index

Quicksearch

- ArgsParser
- ArgsParser::Config
- ArgsParser::Filter
- ArgsParser::Parser
- ArgsParser::ValidationError
- ArgsParser::Validator

No matching classes.

# ArgsParser::Parser

### Attributes

aliases[R]
argv[R]
params[R]

### Public Class Methods

new(config, &block) click to toggle source

    # File lib/args_parser/parser.rb, line 11
    def initialize(config, &block)
      raise ArgumentError, 'initialize block was not given' unless block_given?

      @config = config
      @argv = []
      @params = Hash.new{|h,k|
        h[k] = {
          :default => nil,
          :description => nil,
          :value => nil,
          :alias => nil,
          :index => -1
        }
      }
      @aliases = {}
      @filter = Filter.new
      @validator = Validator.new
      @ons = []

      filter do |v|
        (v.kind_of? String and v =~ /^-?\d+$/) ? v.to_i : v
      end
      filter do |v|
        (v.kind_of? String and v =~ /^-?\d+\.\d+$/) ? v.to_f : v
      end
      on_filter_error do |err, name, value|
        raise err
      end
      on_validate_error do |err, name, value|
        STDERR.puts "Error: #{err.message} (--#{name} #{value})"
        exit 1
      end

      instance_eval &block
    end

### Public Instance Methods

[](key) click to toggle source

    # File lib/args_parser/parser.rb, line 125
    def [](key)
      params[key.to_sym][:value] || default(key)
    end

[]=(key, value) click to toggle source

    # File lib/args_parser/parser.rb, line 129
    def []=(key, value)
      params[key.to_sym][:value] = value
    end

args() click to toggle source

    # File lib/args_parser/parser.rb, line 98
    def args
      params.keys
    end

first() click to toggle source

    # File lib/args_parser/parser.rb, line 7
    def first
      argv.first
    end

has_option?(*opts) click to toggle source

    # File lib/args_parser/parser.rb, line 133
    def has_option?(*opts)
      !opts.flatten.find{|i| self[i] != true}
    end

has_param?(*params) click to toggle source

    # File lib/args_parser/parser.rb, line 137
    def has_param?(*params)
      !params.flatten.find{|i| self[i] == nil or self[i] == true }
    end

help() click to toggle source

    # File lib/args_parser/parser.rb, line 149
    def help
      params_ = Array.new
      params.each do |k,v|
        v[:name] = k
        params_.push v
      end
      params_ = params_.reject{|i| i[:index] < 0 }.sort{|a,b| a[:index] <=> b[:index] }

      len = params_.map{|i|
        (i[:alias] ?
         " -#{i[:name]} (-#{i[:alias]})" :
         " -#{i[:name]}").size
      }.max

      "options:\n" + params_.map{|i|
        line = (i[:alias] ?
                " -#{i[:name]} (-#{i[:alias]})" :
                " -#{i[:name]}").ljust(len+2)
        line += i[:description].to_s
        line += " : default - #{default i[:name]}" if i[:default]
        line
      }.join("\n")
    end

inspect() click to toggle source

    # File lib/args_parser/parser.rb, line 141
    def inspect
      h = Hash.new
      params.each do |k,v|
        h[k] = v[:value]
      end
      h.inspect
    end

parse(argv) click to toggle source

    # File lib/args_parser/parser.rb, line 102
    def parse(argv)
      send "parse_style_#{@config[:style]}", argv
      params.each do |name, param|
        next if [nil, true].include? param[:value]
        begin
          param[:value] = @filter.filter name, param[:value]
        rescue => e
          on_filter_error e, name, param[:value]
        end
        begin
          msg = @validator.validate name, param[:value]
        rescue => e
          on_validate_error e, name, param[:value]
        end
        if msg
          on_validate_error ValidationError.new(msg), name, param[:value]
        end
      end
      params.keys.each do |name|
        on name if has_option? name or has_param? name
      end
    end

parse_style_default(argv) click to toggle source

    # File lib/args_parser/styles/default.rb, line 5
    def parse_style_default(argv)
      k = nil
      is_key = /^-+([^-\s]+)$/
      argv.each_with_index do |arg, index|
        unless k
          if arg =~ is_key
            k = arg.scan(is_key)[0][0].strip.to_sym
            k = aliases[k]  if aliases[k]
          else
            @argv.push arg
          end
        else
          if arg =~ is_key
            params[k][:value] = true
            k = arg.scan(is_key)[0][0].strip.to_sym
            k = aliases[k]  if aliases[k]
          else
            params[k][:value] = arg
            k = nil
          end
        end
      end
      if k
        params[k][:value] = true
      end
    end

parse_style_equal(argv) click to toggle source

    # File lib/args_parser/styles/equal.rb, line 5
    def parse_style_equal(argv)
      on_validate_error do |err, name, value|
        STDERR.puts "Error: #{err.message} (--#{name}=#{value})"
        exit 1
      end

      is_option = /^-+([^-=\s]+)$/
      is_param = /^-+([^-=\s]+)=(.+)$/
      argv.each_with_index do |arg, i|
        if arg =~ is_option
          k,v = arg.scan(is_option)[0][0], true
        elsif arg =~ is_param
          k,v = arg.scan(is_param)[0]
        else
          @argv.push arg
        end
        if k and v
          k = k.strip.to_sym
          k = aliases[k] if aliases[k]
          params[k][:value] = v
        end
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.