Thor - 方法中无法识别命令行选项

时间:2017-03-26 23:28:06

标签: ruby thor

我必须使用此命令来运行我的$ruby filename.rb NAME --from="People" --yell 程序:

require 'thor'

class CLI < Thor
  desc "hello NAME", "say hello to NAME"

  method_option :from, :required => true
  method_option :yell, :type => :boolean
  def self.hello(name)
    output = []
    output << "from: #{options[:from]}" if options[:from]
    output << "Hello #{name}"
    output = output.join("\n")
    puts options[:yell] ? output.upcase : output
  end
end

CLI.hello(ARGV)

我有这样的剧本:

c:\RubyWorkplace\Assignment1>ruby testing.rb Jay --from="Ray"
FROM: #<THOR::OPTION:0X000000031D7998>
HELLO ["JAY", "--FROM=RAY"]

c:\RubyWorkplace\Assignment1>ruby testing.rb Jay --from="Ray" --yell
FROM: #<THOR::OPTION:0X0000000321E528>
HELLO ["JAY", "--FROM=RAY", "--YELL"]

当我运行代码时,我得到以下输出:

:yell

看起来options总是有效,无论我是否指定,name都被hello方法中的import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.Select; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class webelements2 { public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.gecko.driver","C:\\Users\\rpremala003\\Downloads\\geckodriver-v0.14.0-win64\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.google.com/"); driver.manage().window().maximize(); WebDriverWait wait = new WebDriverWait(driver, 10); if (driver.findElements(By.xpath("//a[@title='No thanks']")).size() !=0) { wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//a[@title='No thanks']")))); driver.findElement(By.xpath("//a[@title='No thanks']")).click(); } driver.findElement(By.xpath("//div[@id='gb']//div[@id='gbwa']/div/a[@title='Google apps' and @role='button']")).click(); wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//a[@id='gb8']/span[text()='Maps']")))); driver.findElement(By.xpath("//a[@id='gb8']/span[text()='Maps']")).click(); } } 读取。

我从在线教程中发现并尝试了很多方法,但问题没有解决。请告诉我出了什么问题。谢谢!

1 个答案:

答案 0 :(得分:0)

问题是由我在脚本中调用CLI.hello ARGV引起的。当程序运行时,它将调用hello方法并将所有命令行输入识别为hello的参数,这是一个数组。

解决此问题的方法之一是通过删除hello,通过self方法调用脚本来公开start

require 'thor'

class CLI < Thor
  desc "hello NAME", "say hello to NAME"

  method_option :from, :required => true
  method_option :yell, :type => :boolean
  def hello(name)
    #do something
  end
end

CLI.start ARGV