Class: Rex::Parser::Arguments
- Inherits:
-
Object
- Object
- Rex::Parser::Arguments
- Defined in:
- lib/rex/parser/arguments.rb
Overview
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.
Instance Attribute Summary collapse
-
#fmt ⇒ Object
:nodoc:.
-
#longest ⇒ Object
:nodoc:.
Class Method Summary collapse
-
.from_s(str) ⇒ Object
Takes a string and converts it into an array of arguments.
Instance Method Summary collapse
- #arg_required?(opt) ⇒ Boolean
- #include?(search) ⇒ Boolean
-
#initialize(fmt) ⇒ Arguments
constructor
Initializes the format list with an array of formats like:.
-
#parse(args, &_block) ⇒ Object
Parses the supplied arguments into a set of options.
-
#usage ⇒ Object
Returns usage information for this parsing context.
Constructor Details
#initialize(fmt) ⇒ Arguments
Initializes the format list with an array of formats like:
Arguments.new(
'-b' => [ false, "some text" ]
)
22 23 24 25 |
# File 'lib/rex/parser/arguments.rb', line 22 def initialize(fmt) self.fmt = fmt self.longest = fmt.keys.max_by(&:length) end |
Instance Attribute Details
#fmt ⇒ Object
:nodoc:
91 92 93 |
# File 'lib/rex/parser/arguments.rb', line 91 def fmt @fmt end |
#longest ⇒ Object
:nodoc:
92 93 94 |
# File 'lib/rex/parser/arguments.rb', line 92 def longest @longest end |
Class Method Details
.from_s(str) ⇒ Object
Takes a string and converts it into an array of arguments.
30 31 32 |
# File 'lib/rex/parser/arguments.rb', line 30 def self.from_s(str) Shellwords.shellwords(str) end |
Instance Method Details
#arg_required?(opt) ⇒ Boolean
87 88 89 |
# File 'lib/rex/parser/arguments.rb', line 87 def arg_required?(opt) fmt[opt][0] if fmt[opt] end |
#include?(search) ⇒ Boolean
83 84 85 |
# File 'lib/rex/parser/arguments.rb', line 83 def include?(search) fmt.include?(search) end |
#parse(args, &_block) ⇒ Object
Parses the supplied arguments into a set of options.
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 |
# File 'lib/rex/parser/arguments.rb', line 37 def parse(args, &_block) skip_next = false args.each_with_index do |arg, idx| if skip_next skip_next = false next end if arg.length > 1 && arg[0] == '-' && arg[1] != '-' arg.split('').each do |flag| fmt.each_pair do |fmtspec, val| next if fmtspec != "-#{flag}" param = nil if val[0] param = args[idx + 1] skip_next = true end yield fmtspec, idx, param end end else yield nil, idx, arg end end end |
#usage ⇒ Object
Returns usage information for this parsing context.
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/rex/parser/arguments.rb', line 70 def usage txt = ["\nOPTIONS:\n"] fmt.sort.each do |entry| fmtspec, val = entry opt = val[0] ? " <opt> " : " " txt << " #{fmtspec.ljust(longest.length)}#{opt}#{val[1]}" end txt << "" txt.join("\n") end |