class Rex::Parser::Arguments

This class parses arguments in a getopt style format, kind of. Unfortunately, the default ruby getopt implementation will only work on ARGV, so we can't use it.

Constants

HasArgument

Specifies that an option is expected to have an argument

Public Class Methods

from_s(str) click to toggle source

Takes a string and converts it into an array of arguments.

# File lib/rex/parser/arguments.rb, line 39
def self.from_s(str)
  Shellwords.shellwords(str)
end
new(fmt) click to toggle source

Initializes the format list with an array of formats like:

Arguments.new(

'-b' => [ false, "some text" ]

)

# File lib/rex/parser/arguments.rb, line 27
def initialize(fmt)
  self.fmt = fmt
  # I think reduce is a better name for this method, but it doesn't exist
  # before 1.8.7, so use the stupid inject instead.
  self.longest = fmt.keys.inject(0) { |max, str|
    max = ((max > str.length) ? max : str.length)
  }
end

Public Instance Methods

arg_required?(opt) click to toggle source
# File lib/rex/parser/arguments.rb, line 97
def arg_required?(opt)
  fmt[opt][0] if fmt[opt]
end
include?(search) click to toggle source
# File lib/rex/parser/arguments.rb, line 93
def include?(search)
  return fmt.include?(search)
end
parse(args) { |fmtspec, idx, param| ... } click to toggle source

Parses the supplied arguments into a set of options.

# File lib/rex/parser/arguments.rb, line 46
def parse(args, &block)
  skip_next = false

  args.each_with_index { |arg, idx|
    if (skip_next == true)
      skip_next = false
      next
    end

    if (arg.match(/^-/))
      cfs = arg[0..2]

      fmt.each_pair { |fmtspec, val|
        next if (fmtspec != cfs)

        param = nil

        if (val[0])
          param = args[idx+1]
          skip_next = true
        end

        yield fmtspec, idx, param
      }
    else
      yield nil, idx, arg
    end
  }
end
usage() click to toggle source

Returns usage information for this parsing context.

# File lib/rex/parser/arguments.rb, line 79
def usage
  txt = "\nOPTIONS:\n\n"

  fmt.sort.each { |entry|
    fmtspec, val = entry

    txt << "    #{fmtspec.ljust(longest)}" + ((val[0] == true) ? " <opt>  " : "        ")
    txt << val[1] + "\n"
  }

  txt << "\n"

  return txt
end