From eab4d3083d903641a2ab65cf3c8f21c87449eb93 Mon Sep 17 00:00:00 2001 From: Wade Alcorn Date: Wed, 11 Jan 2012 20:53:51 +1000 Subject: [PATCH] Fix added for #78 Gracefully fail command line with unknown option https://github.com/beefproject/beef/issues/78 Now the framework suggests the user runs --help --- extensions/console/commandline.rb | 76 +++++++++++++++++-------------- 1 file changed, 41 insertions(+), 35 deletions(-) diff --git a/extensions/console/commandline.rb b/extensions/console/commandline.rb index 8932bdf32..701050d44 100644 --- a/extensions/console/commandline.rb +++ b/extensions/console/commandline.rb @@ -14,43 +14,49 @@ # limitations under the License. # module BeEF -module Extension -module Console - # - # This module parses the command line argument when running beef. - # - module CommandLine - - @options = Hash.new - @options[:verbose] = false - @options[:resetdb] = false - - @already_parsed = false - - # - # Parses the command line arguments of the console. - # It also populates the 'options' hash. - # - def self.parse - return @options if @already_parsed - - optparse = OptionParser.new do |opts| - opts.on('-x', '--reset', 'Reset the database') do - @options[:resetdb] = true - end - - opts.on('-v', '--verbose', 'Display debug information') do - @options[:verbose] = true + module Extension + module Console + # + # This module parses the command line argument when running beef. + # + module CommandLine + + @options = Hash.new + @options[:verbose] = false + @options[:resetdb] = false + + @already_parsed = false + + # + # Parses the command line arguments of the console. + # It also populates the 'options' hash. + # + def self.parse + return @options if @already_parsed + + begin + optparse = OptionParser.new do |opts| + opts.on('-x', '--reset', 'Reset the database') do + @options[:resetdb] = true + end + + opts.on('-v', '--verbose', 'Display debug information') do + @options[:verbose] = true + end + end + + + optparse.parse! + @already_parsed = true + @options + rescue OptionParser::InvalidOption => e + puts "Invalid command line option provided. Please run beef --help" + exit 1 + end end + end - - optparse.parse! - @already_parsed = true - @options + end - end - -end -end end \ No newline at end of file