Ruby OptionParser用于解析来自文件而不是命令行的参数
我正在使用Ruby执行一个接受命令行参数的代码。 现在我试着用同一个程序使用不同的选项,所以我把选项放在一个文件中,我希望程序读取每一行,解释选项,并相应地执行程序 但我得到了这个错误。“C:/Ruby193/lib/ruby/1.9.1/optpasse.rb:1348:inRuby OptionParser用于解析来自文件而不是命令行的参数,ruby,arguments,optparse,optionparser,Ruby,Arguments,Optparse,Optionparser,我正在使用Ruby执行一个接受命令行参数的代码。 现在我试着用同一个程序使用不同的选项,所以我把选项放在一个文件中,我希望程序读取每一行,解释选项,并相应地执行程序 但我得到了这个错误。“C:/Ruby193/lib/ruby/1.9.1/optpasse.rb:1348:in块中按顺序解析:未定义的方法--C execue--query unix--Servername abc123:”字符串(NoMethodError) 我理解它读取文件并将行视为字符串。但是想知道是否有办法克服这个移位错误
块中按顺序解析:未定义的方法--C execue--query unix--Servername abc123:”字符串(NoMethodError)
我理解它读取文件并将行视为字符串。但是想知道是否有办法克服这个移位错误,并将该行视为是在命令提示符下输入的。或者任何更好的解决方案
这是我的密码
require 'optparse'
require 'micro-optparse'
# --command execue --query unix command --Servername abc123
f =File.open("list_of_commands.txt", "r")
f.each_line { |line|
line= line.chomp
#line = "--c execue --query unix --Servername abc123"
#line = eval("\"#{line}\"")
puts line
options = {}
OptionParser.new do |opts|
opts.on("-c", "--command result,execue,chart,scpfile", String, "Single command to execute ") do |c|
options[:comd] = c
end
opts.on("-q", "--query remote command, unix command", String, "performs the command on local or remote machine") do |q|
options[:query] = q
end
opts.on("-s", "--servername CHSXEDWDC002 ", String, "server name to execute the command") do |v|
options[:hname] = v
end
opts.on_tail('-h', '--help', 'Show this message') do
puts opts
exit
end
end.parse!(line)
p options
}
该文件的内容如下所示
--c execue--查询unix--服务器名abc123
我也尝试使用micro OptPass,但遇到了同样的错误。有解决办法吗
更新:
根据“@mu太短”的建议,我尝试了以下选项。
结束,解析!(“#{Shellwords.shellsplit(行)}”)和/或
结束,解析!(Shellwords.shellsplit(行))。
但没有一个成功
我还尝试使用“line=line.split(“\t”)”将行拆分为数组,然后
结束,解析!(行)。作为
--行政长官
--查询unix
--服务器名abc123
但现在我得到的错误是block in:invalid option--c execute
更新:#2
查看错误,问题在于参数错误(-c)。但要感谢用户“@mu太短”建议使用数组
更新:3
传递数组只对短形式的参数(如-c)有效,但当提供长形式时,传递数组失败,参数erorr无效
我没有看到太多关于optparse的文档。我甚至尝试了micro parse,但它需要默认值,这对我来说不是一个选项:(该方法需要数组作为其参数,而不是字符串。您可能希望使用而不是(或类似的手动方法)将您的行
转换为数组,以防您需要处理引用之类的问题。类似于:
OptionParser.new do |opts|
#...
end.parse!(Shellwords.shellsplit(line))
该方法需要一个数组作为其参数,而不是字符串。您可能需要使用而不是(或类似的手动方法)将行
转换为数组,以防您需要处理引用之类的问题。类似如下:
OptionParser.new do |opts|
#...
end.parse!(Shellwords.shellsplit(line))
虽然您可以将命令行参数放入文件、标志和全部,但有更好的方法来记住配置设置
使用文件而不是存储标志。YAML是一种很好的数据格式,可以很容易地转换为Ruby哈希和对象。“”是一个非常有用的页面,用于学习Ruby格式的细节。有许多其他语言的YAML解析器,可以很容易地共享设置,随着系统的发展,这些设置可能会很有用
通过一些创造性的代码,您可以使用YAML作为基本设置,并让CLI标志覆盖存储的设置
如果您不熟悉YAML,可以很容易地使用以下内容开始使用该文件:
require 'yaml'
data = {
'command' => %w[result execute chart scpfile],
'query' => ['remote command', 'unix command'],
'servername' => 'CHSXEDWHDC002',
}
puts data.to_yaml
哪些产出:
---
command:
- result
- execute
- chart
- scpfile
query:
- remote command
- unix command
servername: CHSXEDWHDC002
将该输出重定向到以.yaml
结尾的文件,您就可以开始了
要将其读回脚本,请使用:
require 'yaml'
data = YAML.load_file('path/to/data.yaml')
快速往返测试显示:
require 'yaml'
data = {
'command' => %w[result execute chart scpfile],
'query' => ['remote command', 'unix command'],
'servername' => 'CHSXEDWHDC002',
}
YAML.load(data.to_yaml)
这看起来像:
{"command"=>["result", "execute", "chart", "scpfile"],
"query"=>["remote command", "unix command"],
"servername"=>"CHSXEDWHDC002"}
如果要将默认值存储在YAML文件中,并使用命令行标志覆盖它们,请从文件中读取数据,然后将生成的对象用作OptionParse的基础:
require 'optparse'
require 'yaml'
# Note, YAML can deal with symbols as keys, but other languages might not like them.
options = {
:comd => %w[result execute chart scpfile],
:query => ['remote command', 'unix command'],
:hname => 'CHSXEDWHDC002',
}
# we'll overwrite the options variable to pretend we loaded it from a file.
options = YAML.load(options.to_yaml)
OptionParser.new do |opts|
opts.on("-c", "--Command result,execue,chart,scpfile", String, "Single command to execute ") do |c|
options[:comd] = c
end
opts.on("-q", "--query remote command, unix command", String, "performs the command on local or remote machine") do |q|
options[:query] = q
end
opts.on("-s", "--Servername CHSXEDWHDC002 ", String, "server name to execute the command") do |v|
options[:hname] = v
end
opts.on_tail('-h', '--help', 'Show this message') do
puts opts
exit
end
end.parse!
这没有经过测试,但我们在工作中一直在做类似的事情,所以请将其保存到文件中,然后用棍子戳一戳,看看您会想到什么。虽然您可以将命令行参数放入文件、标志等,但有更好的方法来记住配置设置
使用文件而不是存储标志。YAML是一种很好的数据格式,可以很容易地转换为Ruby哈希和对象。“”是一个非常有用的页面,用于学习Ruby格式的细节。有许多其他语言的YAML解析器,可以很容易地共享设置,随着系统的发展,这些设置可能会很有用
通过一些创造性的代码,您可以使用YAML作为基本设置,并让CLI标志覆盖存储的设置
如果您不熟悉YAML,可以很容易地使用以下内容开始使用该文件:
require 'yaml'
data = {
'command' => %w[result execute chart scpfile],
'query' => ['remote command', 'unix command'],
'servername' => 'CHSXEDWHDC002',
}
puts data.to_yaml
哪些产出:
---
command:
- result
- execute
- chart
- scpfile
query:
- remote command
- unix command
servername: CHSXEDWHDC002
将该输出重定向到以.yaml
结尾的文件,您就可以开始了
要将其读回脚本,请使用:
require 'yaml'
data = YAML.load_file('path/to/data.yaml')
快速往返测试显示:
require 'yaml'
data = {
'command' => %w[result execute chart scpfile],
'query' => ['remote command', 'unix command'],
'servername' => 'CHSXEDWHDC002',
}
YAML.load(data.to_yaml)
这看起来像:
{"command"=>["result", "execute", "chart", "scpfile"],
"query"=>["remote command", "unix command"],
"servername"=>"CHSXEDWHDC002"}
如果要将默认值存储在YAML文件中,并使用命令行标志覆盖它们,请从文件中读取数据,然后将生成的对象用作OptionParse的基础:
require 'optparse'
require 'yaml'
# Note, YAML can deal with symbols as keys, but other languages might not like them.
options = {
:comd => %w[result execute chart scpfile],
:query => ['remote command', 'unix command'],
:hname => 'CHSXEDWHDC002',
}
# we'll overwrite the options variable to pretend we loaded it from a file.
options = YAML.load(options.to_yaml)
OptionParser.new do |opts|
opts.on("-c", "--Command result,execue,chart,scpfile", String, "Single command to execute ") do |c|
options[:comd] = c
end
opts.on("-q", "--query remote command, unix command", String, "performs the command on local or remote machine") do |q|
options[:query] = q
end
opts.on("-s", "--Servername CHSXEDWHDC002 ", String, "server name to execute the command") do |v|
options[:hname] = v
end
opts.on_tail('-h', '--help', 'Show this message') do
puts opts
exit
end
end.parse!
这没有经过测试,但我们在工作中一直在做类似的事情,所以请将其保存到一个文件中,然后用一根棍子戳一戳,看看你能想到什么。您好,谢谢您的快速回复。我尝试了这些选项。end.parse!(“{Shellwords.shellsplit(line)}”)和end.parse!(Shellwords.shellsplit(line))。但它们都不起作用。我还尝试使用“line=line.split(“\t”)”将行拆分为数组,然后使用end.parse!(line)但是我在
中没有得到块:无效选项--c执行你的选项。在
参数上找不到合适的--c
选项。为什么你可能指的是--Command
?问题还没有完全解决,通过传递数组,我可以使用参数的缩写形式,但是当传递长表单时,我得到的错误为“Invalid option:--command execute(OptionParser::invalidooption)。是的,我正在使用该命令,而不是Command@user2300908:使用-c
或--command
均可正常工作,micro OptPass是否会干扰正常的OptPass行为?它仅适用于简短的示例“-c execue\t-q unix组件和