add yard doc and allow for -A and -B at same time

unstable
kernelsmith 2013-01-15 16:22:04 -06:00
parent 9ad726167e
commit c60556389f
1 changed files with 23 additions and 18 deletions

View File

@ -70,10 +70,10 @@ class Core
"-i" => [ false, "Ignore case." ], "-i" => [ false, "Ignore case." ],
"-m" => [ true, "Stop after arg matches." ], "-m" => [ true, "Stop after arg matches." ],
"-v" => [ false, "Invert match." ], "-v" => [ false, "Invert match." ],
"-A" => [ true, "Show arg lines after a match." ], "-A" => [ true, "Show arg lines of output After a match." ],
"-B" => [ true, "Show arg lines before a match." ], "-B" => [ true, "Show arg lines of output Before a match." ],
"-s" => [ true, "Skip arg lines before attempting match." ], "-s" => [ true, "Skip arg lines of output before attempting match."],
"-k" => [ true, "Keep (include) arg lines at start of file." ], "-k" => [ true, "Keep (include) arg lines at start of output." ],
"-c" => [ false, "Only print a count of matching lines." ]) "-c" => [ false, "Only print a count of matching lines." ])
@@search_opts = Rex::Parser::Arguments.new( @@search_opts = Rex::Parser::Arguments.new(
@ -2372,12 +2372,12 @@ class Core
args.shift(s) args.shift(s)
when "-A" when "-A"
# also return arg lines after a match # also return arg lines after a match
output_mods[:also] = val.to_i output_mods[:after] = val.to_i
# delete opt and val from args list # delete opt and val from args list
args.shift(2) args.shift(2)
when "-B" when "-B"
# also return arg lines before a match # also return arg lines before a match
output_mods[:also] = (val.to_i * -1) output_mods[:before] = (val.to_i * -1)
# delete opt and val from args list # delete opt and val from args list
args.shift(2) args.shift(2)
when "-v" when "-v"
@ -2449,7 +2449,8 @@ class Core
break if match_mods[:max] and count >= match_mods[:max] break if match_mods[:max] and count >= match_mods[:max]
if eval statement if eval statement
count += 1 count += 1
our_lines += get_grep_lines(all_lines,line_num,output_mods[:also]) # we might get a -A/after and a -B/before at the same time
our_lines += retrieve_grep_lines(all_lines,line_num,output_mods[:before], output_mods[:after])
end end
end end
@ -2930,18 +2931,22 @@ protected
) )
end end
# #
# Returns array of matched line at +line_num+ plus any after/before lines requested as # Returns an array of lines at the provided line number plus any after/before lines requested from
# integer +also+ from the lines specified as +all_lines+. +also+ is positive for "after" # all_lines by supplying the 'after' and/or 'before' parameters which are always positive
# and negative for "before" lines
# #
def get_grep_lines(all_lines,line_num, also=nil) # @param all_lines [Array<String>] An array of all lines being considered for matching
also = also.to_i # @param line_num [Integer] The line number in all_lines which has satisifed the match
return [all_lines[line_num]] unless also or also == 0 # @param after [Integer] The number of lines after the match line to include (should always be positive)
if also < 0 # @param before [Integer] The number of lines before the match line to include (should always be positive)
return all_lines.slice(line_num + also, also.abs + 1) # @return [Array<String>] Array of lines including the line at line_num and any before and after
else
return all_lines.slice(line_num, also + 1) def retrieve_grep_lines(all_lines,line_num, before = nil, after = nil)
end after = after.to_i.abs
before = before.to_i.abs
start = line_num - before
start = 0 if start < 0
finish = line_num + before
return all_lines.slice(start..finish)
end end
end end