From 65c5ea63e8962c755e9c387f711970f83b8c3b24 Mon Sep 17 00:00:00 2001 From: Sean Busbey Date: Wed, 15 Dec 2021 14:35:01 -0600 Subject: [PATCH] HBASE-26543 correct parsing of shell args with GetoptLong (#4000) Signed-off-by: Mike Drob (cherry picked from commit dda337fd9ec8db0a35cdbe0d0627185ec886ffd1) --- hbase-shell/src/main/ruby/jar-bootstrap.rb | 73 +++++++++++----------- 1 file changed, 35 insertions(+), 38 deletions(-) diff --git a/hbase-shell/src/main/ruby/jar-bootstrap.rb b/hbase-shell/src/main/ruby/jar-bootstrap.rb index 03dba743b3c..47f2de487ff 100644 --- a/hbase-shell/src/main/ruby/jar-bootstrap.rb +++ b/hbase-shell/src/main/ruby/jar-bootstrap.rb @@ -78,62 +78,59 @@ def add_to_configuration(c, arg) c end -opts = GetoptLong.new( - [ '--help', '-h', GetoptLong::NO_ARGUMENT ], - [ '--debug', '-d', GetoptLong::OPTIONAL_ARGUMENT ], - [ '--noninteractive', '-n', GetoptLong::OPTIONAL_ARGUMENT ], - [ '--top-level-defs', GetoptLong::OPTIONAL_ARGUMENT ], - [ '--Dkey=value', '-D', GetoptLong::NO_ARGUMENT ] -) +conf_from_cli = nil + +# strip out any config definitions that won't work with GetoptLong +D_ARG = '-D'.freeze +ARGV.delete_if do |arg| + if arg.start_with?(D_ARG) && arg.include?('=') + conf_from_cli = add_to_configuration(conf_from_cli, arg[2..-1]) + true + else + false + end +end + +opts = GetoptLong.new( + ['--help', '-h', GetoptLong::NO_ARGUMENT], + ['--debug', '-d', GetoptLong::NO_ARGUMENT], + ['--noninteractive', '-n', GetoptLong::NO_ARGUMENT], + ['--top-level-defs', GetoptLong::NO_ARGUMENT], + ['-D', GetoptLong::REQUIRED_ARGUMENT], + ['--return-values', '-r', GetoptLong::NO_ARGUMENT] +) +opts.ordering = GetoptLong::REQUIRE_ORDER -found = [] script2run = nil log_level = org.apache.log4j.Level::ERROR @shell_debug = false interactive = true top_level_definitions = false -_configuration = nil -D_ARG = '-D'.freeze opts.each do |opt, arg| - case opt || arg - when '--help' || '-h' + case opt + when '--help' puts cmdline_help + exit when D_ARG - argValue = ARGV.shift || (raise "#{D_ARG} takes a 'key=value' parameter") - _configuration = add_to_configuration(_configuration, argValue) - found.push(arg) - found.push(argValue) - when arg.start_with?(D_ARG) - _configuration = add_to_configuration(_configuration, arg[2..-1]) - found.push(arg) - when '--debug'|| '-d' + conf_from_cli = add_to_configuration(conf_from_cli, arg) + when '--debug' log_level = org.apache.log4j.Level::DEBUG $fullBackTrace = true @shell_debug = true - found.push(arg) puts 'Setting DEBUG log level...' - when '--noninteractive'|| '-n' - interactive = false - found.push(arg) - when '--return-values' || 'r' - warn '[INFO] the -r | --return-values option is ignored. we always behave '\ + when '--noninteractive' + interactive = false + when '--return-values' + warn '[INFO] the -r | --return-values option is ignored. we always behave '\ 'as though it was given.' - found.push(arg) - when '--top-level-defs' - top_level_definitions = true - else - # Presume it a script. Save it off for running later below - # after we've set up some environment. - script2run = arg - found.push(arg) - # Presume that any other args are meant for the script. + when '--top-level-defs' + top_level_definitions = true end end +script2run = ARGV.shift unless ARGV.empty? -# Delete all processed args -found.each { |arg| ARGV.delete(arg) } # Make sure debug flag gets back to IRB ARGV.unshift('-d') if @shell_debug @@ -151,7 +148,7 @@ require 'hbase_shell' require 'shell/formatter' # Setup the HBase module. Create a configuration. -@hbase = _configuration.nil? ? Hbase::Hbase.new : Hbase::Hbase.new(_configuration) +@hbase = conf_from_cli.nil? ? Hbase::Hbase.new : Hbase::Hbase.new(conf_from_cli) # Setup console @shell = Shell::Shell.new(@hbase, interactive)