2005-07-13 22:57:29 +00:00
|
|
|
require 'shellwords'
|
|
|
|
|
2005-07-10 07:15:20 +00:00
|
|
|
module Rex
|
|
|
|
module Parser
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class Arguments
|
|
|
|
|
|
|
|
#
|
|
|
|
# Specifies that an option is expected to have an argument
|
|
|
|
#
|
|
|
|
HasArgument = (1 << 0)
|
|
|
|
|
|
|
|
#
|
|
|
|
# Initializes the format list with an array of formats like:
|
|
|
|
#
|
|
|
|
# Arguments.new(
|
|
|
|
# '-b' => [ false, "some text" ]
|
|
|
|
# )
|
|
|
|
#
|
|
|
|
def initialize(fmt)
|
|
|
|
self.fmt = fmt
|
2011-04-16 15:57:52 +00:00
|
|
|
# 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)
|
|
|
|
}
|
2005-07-10 07:15:20 +00:00
|
|
|
end
|
|
|
|
|
2005-07-10 07:27:50 +00:00
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Takes a string and converts it into an array of arguments.
|
2005-07-10 07:27:50 +00:00
|
|
|
#
|
|
|
|
def self.from_s(str)
|
2005-07-13 22:57:29 +00:00
|
|
|
Shellwords.shellwords(str)
|
2005-07-10 07:27:50 +00:00
|
|
|
end
|
|
|
|
|
2005-07-10 07:15:20 +00:00
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Parses the supplied arguments into a set of options.
|
2005-07-10 07:15:20 +00:00
|
|
|
#
|
|
|
|
def parse(args, &block)
|
2005-07-18 14:39:00 +00:00
|
|
|
skip_next = false
|
|
|
|
|
2005-07-10 07:15:20 +00:00
|
|
|
args.each_with_index { |arg, idx|
|
2005-07-18 14:39:00 +00:00
|
|
|
if (skip_next == true)
|
|
|
|
skip_next = false
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2005-07-10 07:15:20 +00:00
|
|
|
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]
|
2005-07-18 23:32:34 +00:00
|
|
|
skip_next = true
|
2005-07-10 07:15:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
yield fmtspec, idx, param
|
|
|
|
}
|
|
|
|
else
|
|
|
|
yield nil, idx, arg
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Returns usage information for this parsing context.
|
2005-07-10 07:15:20 +00:00
|
|
|
#
|
|
|
|
def usage
|
|
|
|
txt = "\nOPTIONS:\n\n"
|
|
|
|
|
2005-07-11 05:25:50 +00:00
|
|
|
fmt.sort.each { |entry|
|
|
|
|
fmtspec, val = entry
|
|
|
|
|
2011-04-16 15:57:52 +00:00
|
|
|
txt << " #{fmtspec.ljust(longest)}" + ((val[0] == true) ? " <opt> " : " ")
|
2009-03-08 07:55:47 +00:00
|
|
|
txt << val[1] + "\n"
|
2005-07-10 07:15:20 +00:00
|
|
|
}
|
|
|
|
|
2009-03-08 07:55:47 +00:00
|
|
|
txt << "\n"
|
2005-07-10 07:15:20 +00:00
|
|
|
|
|
|
|
return txt
|
|
|
|
end
|
2009-02-17 04:53:06 +00:00
|
|
|
def include?(search)
|
|
|
|
return fmt.include?(search)
|
|
|
|
end
|
2005-07-10 07:15:20 +00:00
|
|
|
|
2011-04-17 00:35:32 +00:00
|
|
|
def arg_required?(opt)
|
|
|
|
fmt[opt][0] if fmt[opt]
|
|
|
|
end
|
|
|
|
|
2011-04-16 15:57:52 +00:00
|
|
|
attr_accessor :fmt # :nodoc:
|
|
|
|
attr_accessor :longest # :nodoc:
|
2005-07-10 07:15:20 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2009-02-17 04:53:06 +00:00
|
|
|
end
|