Ruby Option Parser. Is there more concision possible for this code? -
i've written working script has several command line options. want make few of these options required options. research got me following code. you'll see @ end of script i'm checking separately options have been supplied.
in python, argparse module has argument "required=true" automatically exit program if given argument supplied. couldn't find in ruby module. there different, more advanced argument parsing module? there more concise way write code?
options = {} optparse = optionparser.new |opts| opts.banner = "\nusage: how use code" opts.on('--debug', "change log level debug. default info") |l| options[:debug] = l end options[:logfile] = "stage_refresh.out" opts.on('-l', '--logfile logfile', "logfile log results. default stage_refresh.out") |l| options[:source] = l end options[:source] = "localhost" opts.on('-s', '--source source_host', "source host dump. default localhost") |s| options[:source] = s end opts.on('-t', '--target target', "target host refresh. must specify.") |t| options[:target] = t end end optparse.parse! unless options[:source_pass] puts "must have --source_pass password" end unless options[:target] puts "must have --target target_server" end if options[:debug] logger.level = logger::debug else logger.level = logger::info end puts options[:source_pass] puts options[:target] .................
the trollop gem might of interest you. has long/short options, default values, required flags, etc. use of not unlike have right now.
Comments
Post a Comment