Skip to content

Commit 686f30e

Browse files
committed
Land rapid7#8117, p{grep,kill} for Meterpreter <3
2 parents 5a1c7ca + dd6e759 commit 686f30e

File tree

2 files changed

+239
-155
lines changed

2 files changed

+239
-155
lines changed

lib/rex/parser/arguments.rb

Lines changed: 72 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,94 @@
11
# -*- coding: binary -*-
2+
# frozen_string_literal: true
23
require 'shellwords'
34

45
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
2026

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
3633

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
4339

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
4945

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]
5548

56-
if (arg.match(/^-/))
57-
cfs = arg[0..2]
49+
fmt.each_pair do |fmtspec, val|
50+
next if fmtspec != cfs
5851

59-
fmt.each_pair { |fmtspec, val|
60-
next if (fmtspec != cfs)
52+
param = nil
6153

62-
param = nil
54+
if val[0]
55+
param = args[idx + 1]
56+
skip_next = true
57+
end
6358

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
6763
end
68-
69-
yield fmtspec, idx, param
70-
}
71-
else
72-
yield nil, idx, arg
64+
end
7365
end
74-
}
75-
end
7666

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"]
8272

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
8578

86-
txt << " #{fmtspec.ljust(longest)}" + ((val[0] == true) ? " <opt> " : " ")
87-
txt << val[1] + "\n"
88-
}
79+
txt.join("\n")
80+
end
8981

90-
txt << "\n"
82+
def include?(search)
83+
fmt.include?(search)
84+
end
9185

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
9789

98-
def arg_required?(opt)
99-
fmt[opt][0] if fmt[opt]
90+
attr_accessor :fmt # :nodoc:
91+
attr_accessor :longest # :nodoc:
92+
end
10093
end
101-
102-
attr_accessor :fmt # :nodoc:
103-
attr_accessor :longest # :nodoc:
104-
105-
end
106-
107-
end
10894
end

0 commit comments

Comments
 (0)