Skip to content

Commit

Permalink
HBASE-24772 Use GetoptLong or OptionParser in hbase-shell
Browse files Browse the repository at this point in the history
  • Loading branch information
Laxman Goswami committed Feb 1, 2021
1 parent a04ea7e commit 4e6dc47
Showing 1 changed file with 33 additions and 29 deletions.
62 changes: 33 additions & 29 deletions hbase-shell/src/main/ruby/jar-bootstrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
# Some goodies for hirb. Should these be left up to the user's discretion?
require 'irb/completion'
require 'pathname'
require 'getoptlong'

# Add the directory names in hbase.jruby.sources commandline option
# to the ruby load path so I can load up my HBase ruby modules
Expand All @@ -50,20 +51,22 @@
$LOAD_PATH.unshift Pathname.new(sources)
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'
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', GetoptLong::OPTIONAL_ARGUMENT ]
)

cmdline_help = <<HERE # HERE document output as shell usage
Usage: shell [OPTIONS] [SCRIPTFILE [ARGUMENTS]]
-d | --debug Set DEBUG log levels.
-h | --help This help.
-n | --noninteractive Do not run within an IRB session and exit with non-zero
status on first error.
--top-level-defs Compatibility flag to export HBase shell commands onto
Ruby's main object
Ruby's main object
-Dkey=value Pass hbase-*.xml Configuration overrides. For example, to
use an alternate zookeeper ensemble, pass:
-Dhbase.zookeeper.quorum=zookeeper.example.org
Expand All @@ -72,6 +75,7 @@
-Dhbase.ipc.client.connect.max.retries=3
HERE


# Takes configuration and an arg that is expected to be key=value format.
# If c is empty, creates one and returns it
def add_to_configuration(c, arg)
Expand All @@ -90,40 +94,40 @@ def add_to_configuration(c, arg)
top_level_definitions = false
_configuration = nil
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
exit
elsif arg == D_ARG
when '-Dkey=value'
argValue = ARGV.shift || (raise "#{D_ARG} takes a 'key=value' parameter")
_configuration = add_to_configuration(_configuration, argValue)
found.push(arg)
found.push(argValue)
elsif arg.start_with? D_ARG
when arg.start_with?(D_ARG)
_configuration = add_to_configuration(_configuration, arg[2..-1])
found.push(arg)
elsif arg == '-d' || arg == '--debug'
when '--debug'|| '-d'
log_level = org.apache.log4j.Level::DEBUG
$fullBackTrace = true
@shell_debug = true
found.push(arg)
puts 'Setting DEBUG log level...'
elsif arg == '-n' || arg == '--noninteractive'
interactive = false
found.push(arg)
elsif arg == '-r' || arg == '--return-values'
warn '[INFO] the -r | --return-values option is ignored. we always behave '\
'as though it was given.'
found.push(arg)
elsif arg == '--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.
break
when '--noninteractive'|| '-n'
interactive = false
found.push(arg)
when '--return-values' || 'r'
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.
end
end

Expand Down

0 comments on commit 4e6dc47

Please sign in to comment.