Sha256: 132c3e3aef690e9bbf852d0ae0bd5c852d1deb6cf112002bb94350f9011bbea2

Contents?: true

Size: 1.49 KB

Versions: 16

Compression:

Stored size: 1.49 KB

Contents

require 'shellwords'

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
	end

	#
	# Takes a string and converts it into an array of arguments.
	#
	def self.from_s(str)
		Shellwords.shellwords(str)
	end

	#
	# Parses the supplied arguments into a set of options.
	#
	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

	#
	# Returns usage information for this parsing context.
	#
	def usage
		txt = "\nOPTIONS:\n\n"

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

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

		txt << "\n"

		return txt
	end
	def include?(search)
		return fmt.include?(search)
	end

	attr_accessor :fmt # :nodoc:

end

end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
librex-0.0.28 lib/rex/parser/arguments.rb
librex-0.0.27 lib/rex/parser/arguments.rb
librex-0.0.26 lib/rex/parser/arguments.rb
librex-0.0.25 lib/rex/parser/arguments.rb
librex-0.0.23 lib/rex/parser/arguments.rb
librex-0.0.21 lib/rex/parser/arguments.rb
librex-0.0.19 lib/rex/parser/arguments.rb
librex-0.0.17 lib/rex/parser/arguments.rb
librex-0.0.13 lib/rex/parser/arguments.rb
librex-0.0.12 lib/rex/parser/arguments.rb
librex-0.0.7 lib/rex/parser/arguments.rb
librex-0.0.6 lib/rex/parser/arguments.rb
librex-0.0.5 lib/rex/parser/arguments.rb
librex-0.0.4 lib/rex/parser/arguments.rb
librex-0.0.3 lib/rex/parser/arguments.rb
librex-0.0.1 lib/rex/parser/arguments.rb