HBASE-24772 Use GetoptLong or OptionParser in hbase-shell (#2918)

Signed-off-by: Viraj Jasani <vjasani@apache.org>
Signed-off-by: stack <stack@apache.org>
This commit is contained in:
Laxman Goswami 2021-02-09 10:44:23 +05:30 committed by GitHub
parent ae063f953e
commit f09e420e5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 32 additions and 27 deletions

View File

@ -40,6 +40,7 @@ include Java
# Some goodies for hirb. Should these be left up to the user's discretion? # Some goodies for hirb. Should these be left up to the user's discretion?
require 'irb/completion' require 'irb/completion'
require 'pathname' require 'pathname'
require 'getoptlong'
# Add the directory names in hbase.jruby.sources commandline option # Add the directory names in hbase.jruby.sources commandline option
# to the ruby load path so I can load up my HBase ruby modules # to the ruby load path so I can load up my HBase ruby modules
@ -50,11 +51,6 @@ unless sources.nil?
$LOAD_PATH.unshift Pathname.new(sources) $LOAD_PATH.unshift Pathname.new(sources)
end end
#
# FIXME: Switch args processing to getopt
#
# See if there are args for this shell. If any, read and then strip from ARGV
# so they don't go through to irb. Output shell 'usage' if user types '--help'
cmdline_help = <<HERE # HERE document output as shell usage cmdline_help = <<HERE # HERE document output as shell usage
Usage: shell [OPTIONS] [SCRIPTFILE [ARGUMENTS]] Usage: shell [OPTIONS] [SCRIPTFILE [ARGUMENTS]]
@ -82,6 +78,14 @@ def add_to_configuration(c, arg)
c c
end 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 ]
)
found = [] found = []
script2run = nil script2run = nil
log_level = org.apache.log4j.Level::ERROR log_level = org.apache.log4j.Level::ERROR
@ -90,32 +94,33 @@ interactive = true
top_level_definitions = false top_level_definitions = false
_configuration = nil _configuration = nil
D_ARG = '-D'.freeze D_ARG = '-D'.freeze
while (arg = ARGV.shift)
if arg == '-h' || arg == '--help' opts.each do |opt, arg|
case opt || arg
when '--help' || '-h'
puts cmdline_help puts cmdline_help
exit when D_ARG
elsif arg == D_ARG
argValue = ARGV.shift || (raise "#{D_ARG} takes a 'key=value' parameter") argValue = ARGV.shift || (raise "#{D_ARG} takes a 'key=value' parameter")
_configuration = add_to_configuration(_configuration, argValue) _configuration = add_to_configuration(_configuration, argValue)
found.push(arg) found.push(arg)
found.push(argValue) found.push(argValue)
elsif arg.start_with? D_ARG when arg.start_with?(D_ARG)
_configuration = add_to_configuration(_configuration, arg[2..-1]) _configuration = add_to_configuration(_configuration, arg[2..-1])
found.push(arg) found.push(arg)
elsif arg == '-d' || arg == '--debug' when '--debug'|| '-d'
log_level = org.apache.log4j.Level::DEBUG log_level = org.apache.log4j.Level::DEBUG
$fullBackTrace = true $fullBackTrace = true
@shell_debug = true @shell_debug = true
found.push(arg) found.push(arg)
puts 'Setting DEBUG log level...' puts 'Setting DEBUG log level...'
elsif arg == '-n' || arg == '--noninteractive' when '--noninteractive'|| '-n'
interactive = false interactive = false
found.push(arg) found.push(arg)
elsif arg == '-r' || arg == '--return-values' when '--return-values' || 'r'
warn '[INFO] the -r | --return-values option is ignored. we always behave '\ warn '[INFO] the -r | --return-values option is ignored. we always behave '\
'as though it was given.' 'as though it was given.'
found.push(arg) found.push(arg)
elsif arg == '--top-level-defs' when '--top-level-defs'
top_level_definitions = true top_level_definitions = true
else else
# Presume it a script. Save it off for running later below # Presume it a script. Save it off for running later below
@ -123,10 +128,10 @@ while (arg = ARGV.shift)
script2run = arg script2run = arg
found.push(arg) found.push(arg)
# Presume that any other args are meant for the script. # Presume that any other args are meant for the script.
break
end end
end end
# Delete all processed args # Delete all processed args
found.each { |arg| ARGV.delete(arg) } found.each { |arg| ARGV.delete(arg) }
# Make sure debug flag gets back to IRB # Make sure debug flag gets back to IRB