如何在Ruby中使用命令行选项?
如何在脚本中使用命令选项 我有一个连接到设备、运行命令和打印输出的脚本。我想为主机使用一个文件,为用户和密码信息使用一个文件。当我尝试运行脚本时,会出现以下错误:如何在Ruby中使用命令行选项?,ruby,Ruby,如何在脚本中使用命令选项 我有一个连接到设备、运行命令和打印输出的脚本。我想为主机使用一个文件,为用户和密码信息使用一个文件。当我尝试运行脚本时,会出现以下错误: ruby-threat\u-detection.rb--host\u-file=hosts--config\u-file=config threat_detection.rb:61:in '<main>': needless argument: --host_file=hosts (OptionParser::Needle
ruby-threat\u-detection.rb--host\u-file=hosts--config\u-file=config
threat_detection.rb:61:in '<main>': needless argument: --host_file=hosts (OptionParser::NeedlessArgument)
threat_detection.rb:61:in '<main>': invalid option: --host_file-hosts (OptionParser::InvalidOption)
ruby-threat\u-detection.rb--host\u file-hosts--config\u file-config
threat_detection.rb:61:in '<main>': needless argument: --host_file=hosts (OptionParser::NeedlessArgument)
threat_detection.rb:61:in '<main>': invalid option: --host_file-hosts (OptionParser::InvalidOption)
如何使这些选项被读取?这是我的猜测(基于python的经验)。我现在正在寻找它来打印这些行:
if :config_file == true
File.open(:config_file, r) do |params|
puts params
end
end
if :host_file == true
File.open(:host_file, r) do |host|
put host
end
end
要获取参数,需要使用以下格式
"--switch=MANDATORY" or "--switch MANDATORY" # mandatory argument
"--switch[=OPTIONAL]" # optional argument
"--switch" # no argument
您当前使用的是第三种格式,它被解释为不带参数。你应该使用第一个或第二个
同样值得注意的是,当一个标志打开时,您可能希望进行赋值而不是比较
你想要这个
options[:host_file] = host_file
不是这个
options[:host_file] == host_file
要获取参数,需要使用以下格式
"--switch=MANDATORY" or "--switch MANDATORY" # mandatory argument
"--switch[=OPTIONAL]" # optional argument
"--switch" # no argument
您当前使用的是第三种格式,它被解释为不带参数。你应该使用第一个或第二个
同样值得注意的是,当一个标志打开时,您可能希望进行赋值而不是比较
你想要这个
options[:host_file] = host_file
不是这个
options[:host_file] == host_file
要获取参数,需要使用以下格式
"--switch=MANDATORY" or "--switch MANDATORY" # mandatory argument
"--switch[=OPTIONAL]" # optional argument
"--switch" # no argument
您当前使用的是第三种格式,它被解释为不带参数。你应该使用第一个或第二个
同样值得注意的是,当一个标志打开时,您可能希望进行赋值而不是比较
你想要这个
options[:host_file] = host_file
不是这个
options[:host_file] == host_file
要获取参数,需要使用以下格式
"--switch=MANDATORY" or "--switch MANDATORY" # mandatory argument
"--switch[=OPTIONAL]" # optional argument
"--switch" # no argument
您当前使用的是第三种格式,它被解释为不带参数。你应该使用第一个或第二个
同样值得注意的是,当一个标志打开时,您可能希望进行赋值而不是比较
你想要这个
options[:host_file] = host_file
不是这个
options[:host_file] == host_file