如何在Ruby中使用命令行选项?

时间:2023-02-02 19:14:50

How do I use command options in scripts?

如何在脚本中使用命令选项?

I have a script that connects to a device, runs commands and prints the output. I would like to use a file for the hosts, and one for the user and passwd info. When I try to run the script I get these errors:

我有一个连接到设备的脚本,运行命令并打印输出。我想为主机使用一个文件,为用户和passwd信息使用一个文件。当我尝试运行脚本时,我收到以下错误:

  1. ruby threat_detection.rb --host_file=hosts --config_file=config

    ruby threat_detection.rb --host_file = hosts --config_file = config

    threat_detection.rb:61:in '<main>': needless argument: --host_file=hosts (OptionParser::NeedlessArgument)
    
  2. ruby threat_detection.rb '--host_file=hosts' '--config_file=config'

    ruby threat_detection.rb' - host_file = hosts'' - config_file = config'

    threat_detection.rb:61:in '<main>': needless argument: --host_file=hosts (OptionParser::NeedlessArgument)
    
  3. ruby threat_detection.rb --host_file-hosts --config_file-config

    ruby threat_detection.rb --host_file-hosts --config_file-config

    threat_detection.rb:61:in '<main>': invalid option: --host_file-hosts (OptionParser::InvalidOption)
    

This is the code:

这是代码:

options ={}

opt_parser = OptionParser.new do |opt|
  opt.banner = 'Usage: opt_parser COMMAND [OPTIONS]'
  opt.on('--host_file', 'I need hosts, put them here') do |host_file|
    options[:host_file] == host_file
  end
  opt.on('--config_file', 'I need config info, put it here') do |config_file|
    options[:config_file] == config_file
  end
  opt.on('-h', '--help', 'What your looking at') do |help|
    options[:help] == help
    puts opt
  end
end

opt_parser.parse!

How do I make these options get read? This is my guess (based off python experience). I'm just looking for it to print the lines right now:

如何让这些选项被阅读?这是我的猜测(基于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

1 个解决方案

#1


3  

For taking arguments you need to use the following formats

要获取参数,您需要使用以下格式

"--switch=MANDATORY" or "--switch MANDATORY" # mandatory argument
"--switch[=OPTIONAL]"                        # optional argument
"--switch"                                   # no argument

You are currently using the third format, which is being interpreted as taking no argument. You should use the first or second.

您当前正在使用第三种格式,该格式被解释为不带参数。你应该使用第一个或第二个。

Also worth noting you probably want to be doing an assignment instead of a comparison when a flag is on

另外值得注意的是,当标志打开时,您可能希望进行分配而不是比较

You want this

你要这个

options[:host_file] = host_file

not this

不是这个

options[:host_file] == host_file

#1


3  

For taking arguments you need to use the following formats

要获取参数,您需要使用以下格式

"--switch=MANDATORY" or "--switch MANDATORY" # mandatory argument
"--switch[=OPTIONAL]"                        # optional argument
"--switch"                                   # no argument

You are currently using the third format, which is being interpreted as taking no argument. You should use the first or second.

您当前正在使用第三种格式,该格式被解释为不带参数。你应该使用第一个或第二个。

Also worth noting you probably want to be doing an assignment instead of a comparison when a flag is on

另外值得注意的是,当标志打开时,您可能希望进行分配而不是比较

You want this

你要这个

options[:host_file] = host_file

not this

不是这个

options[:host_file] == host_file