【发布时间】:2019-01-30 21:42:53
【问题描述】:
如果存在命令行参数,我想执行 Ruby 代码:
ruby sanity_checks.rb --trx-request -e staging_psp -g all
Ruby 代码:
def execute
command_options = CommandOptionsProcessor.parse_command_options
request_type = command_options[:env]
tested_env = command_options[:trx-request] // check here if flag is present
tested_gateways = gateway_names(env: tested_env, gateway_list: command_options[:gateways])
error_logs = []
if(request_type.nil?)
tested_gateways.each do |gateway|
........
end
end
raise error_logs.join("\n") if error_logs.any?
end
如何获取参数--trx-request 并检查它是否存在?
编辑:
命令解析器:
class CommandOptionsProcessor
def self.parse_command_options
command_options = {}
opt_parser = OptionParser.new do |opt|
opt.banner = 'Usage: ruby sanity_checks.rb [OPTIONS]'
opt.separator "Options:"
opt.on('-e TEST_ENV', '--env TEST_ENV','Set tested environment (underscored).') do |setting|
command_options[:env] = setting
end
opt.on('-g X,Y,Z', '--gateways X,Y,Z', Array, 'Set tested gateways (comma separated, no spaces).') do |setting|
command_options[:gateways] = setting
end
end
opt_parser.parse!(ARGV)
command_options
end
end
你能建议吗?
【问题讨论】:
-
command_options[:trx_request](注意下划线)应该可以工作。 -
什么是
CommandOptionsProcessor? -
@PeterPenzov 请发布您的
OptionParser(CommandOptionsProcessor) 没有完整的上下文很难提供帮助 -
@engineersmnky 帖子已更新。
标签: ruby