|
1 | 1 | # -*- coding: binary -*-
|
| 2 | +# frozen_string_literal: true |
2 | 3 | require 'shellwords'
|
3 | 4 |
|
4 | 5 | module Rex
|
5 |
| -module Parser |
6 |
| - |
7 |
| -### |
8 |
| -# |
9 |
| -# This class parses arguments in a getopt style format, kind of. |
10 |
| -# Unfortunately, the default ruby getopt implementation will only |
11 |
| -# work on ARGV, so we can't use it. |
12 |
| -# |
13 |
| -### |
14 |
| -class Arguments |
15 |
| - |
16 |
| - # |
17 |
| - # Specifies that an option is expected to have an argument |
18 |
| - # |
19 |
| - HasArgument = (1 << 0) |
| 6 | + module Parser |
| 7 | + ### |
| 8 | + # |
| 9 | + # This class parses arguments in a getopt style format, kind of. |
| 10 | + # Unfortunately, the default ruby getopt implementation will only |
| 11 | + # work on ARGV, so we can't use it. |
| 12 | + # |
| 13 | + ### |
| 14 | + class Arguments |
| 15 | + # |
| 16 | + # Initializes the format list with an array of formats like: |
| 17 | + # |
| 18 | + # Arguments.new( |
| 19 | + # '-b' => [ false, "some text" ] |
| 20 | + # ) |
| 21 | + # |
| 22 | + def initialize(fmt) |
| 23 | + self.fmt = fmt |
| 24 | + self.longest = fmt.keys.max_by(&:length) |
| 25 | + end |
20 | 26 |
|
21 |
| - # |
22 |
| - # Initializes the format list with an array of formats like: |
23 |
| - # |
24 |
| - # Arguments.new( |
25 |
| - # '-b' => [ false, "some text" ] |
26 |
| - # ) |
27 |
| - # |
28 |
| - def initialize(fmt) |
29 |
| - self.fmt = fmt |
30 |
| - # I think reduce is a better name for this method, but it doesn't exist |
31 |
| - # before 1.8.7, so use the stupid inject instead. |
32 |
| - self.longest = fmt.keys.inject(0) { |max, str| |
33 |
| - max = ((max > str.length) ? max : str.length) |
34 |
| - } |
35 |
| - end |
| 27 | + # |
| 28 | + # Takes a string and converts it into an array of arguments. |
| 29 | + # |
| 30 | + def self.from_s(str) |
| 31 | + Shellwords.shellwords(str) |
| 32 | + end |
36 | 33 |
|
37 |
| - # |
38 |
| - # Takes a string and converts it into an array of arguments. |
39 |
| - # |
40 |
| - def self.from_s(str) |
41 |
| - Shellwords.shellwords(str) |
42 |
| - end |
| 34 | + # |
| 35 | + # Parses the supplied arguments into a set of options. |
| 36 | + # |
| 37 | + def parse(args, &_block) |
| 38 | + skip_next = false |
43 | 39 |
|
44 |
| - # |
45 |
| - # Parses the supplied arguments into a set of options. |
46 |
| - # |
47 |
| - def parse(args, &block) |
48 |
| - skip_next = false |
| 40 | + args.each_with_index do |arg, idx| |
| 41 | + if skip_next |
| 42 | + skip_next = false |
| 43 | + next |
| 44 | + end |
49 | 45 |
|
50 |
| - args.each_with_index { |arg, idx| |
51 |
| - if (skip_next == true) |
52 |
| - skip_next = false |
53 |
| - next |
54 |
| - end |
| 46 | + if arg[0] == '-' |
| 47 | + cfs = arg[0..2] |
55 | 48 |
|
56 |
| - if (arg.match(/^-/)) |
57 |
| - cfs = arg[0..2] |
| 49 | + fmt.each_pair do |fmtspec, val| |
| 50 | + next if fmtspec != cfs |
58 | 51 |
|
59 |
| - fmt.each_pair { |fmtspec, val| |
60 |
| - next if (fmtspec != cfs) |
| 52 | + param = nil |
61 | 53 |
|
62 |
| - param = nil |
| 54 | + if val[0] |
| 55 | + param = args[idx + 1] |
| 56 | + skip_next = true |
| 57 | + end |
63 | 58 |
|
64 |
| - if (val[0]) |
65 |
| - param = args[idx+1] |
66 |
| - skip_next = true |
| 59 | + yield fmtspec, idx, param |
| 60 | + end |
| 61 | + else |
| 62 | + yield nil, idx, arg |
67 | 63 | end
|
68 |
| - |
69 |
| - yield fmtspec, idx, param |
70 |
| - } |
71 |
| - else |
72 |
| - yield nil, idx, arg |
| 64 | + end |
73 | 65 | end
|
74 |
| - } |
75 |
| - end |
76 | 66 |
|
77 |
| - # |
78 |
| - # Returns usage information for this parsing context. |
79 |
| - # |
80 |
| - def usage |
81 |
| - txt = "\nOPTIONS:\n\n" |
| 67 | + # |
| 68 | + # Returns usage information for this parsing context. |
| 69 | + # |
| 70 | + def usage |
| 71 | + txt = ["\nOPTIONS:\n"] |
82 | 72 |
|
83 |
| - fmt.sort.each { |entry| |
84 |
| - fmtspec, val = entry |
| 73 | + fmt.sort.each do |entry| |
| 74 | + fmtspec, val = entry |
| 75 | + opt = val[0] ? " <opt> " : " " |
| 76 | + txt << " #{fmtspec.ljust(longest.length)}#{opt}#{val[1]}" |
| 77 | + end |
85 | 78 |
|
86 |
| - txt << " #{fmtspec.ljust(longest)}" + ((val[0] == true) ? " <opt> " : " ") |
87 |
| - txt << val[1] + "\n" |
88 |
| - } |
| 79 | + txt.join("\n") |
| 80 | + end |
89 | 81 |
|
90 |
| - txt << "\n" |
| 82 | + def include?(search) |
| 83 | + fmt.include?(search) |
| 84 | + end |
91 | 85 |
|
92 |
| - return txt |
93 |
| - end |
94 |
| - def include?(search) |
95 |
| - return fmt.include?(search) |
96 |
| - end |
| 86 | + def arg_required?(opt) |
| 87 | + fmt[opt][0] if fmt[opt] |
| 88 | + end |
97 | 89 |
|
98 |
| - def arg_required?(opt) |
99 |
| - fmt[opt][0] if fmt[opt] |
| 90 | + attr_accessor :fmt # :nodoc: |
| 91 | + attr_accessor :longest # :nodoc: |
| 92 | + end |
100 | 93 | end
|
101 |
| - |
102 |
| - attr_accessor :fmt # :nodoc: |
103 |
| - attr_accessor :longest # :nodoc: |
104 |
| - |
105 |
| -end |
106 |
| - |
107 |
| -end |
108 | 94 | end
|
0 commit comments