无法获取ruby optparse以输出选项

无法获取ruby optparse以输出选项,ruby,command-line,optparse,Ruby,Command Line,Optparse,我正在尝试学习如何使用OptPass接受命令行选项,但是我很难让它发挥作用,因为它显示在类文档和我可以在网上找到的任何示例中。特别是当我通过-h选项时,什么都不会出现。我可以输出ARGV及其显示,它接收到-h,但它不会显示opts.banner和或任何选项。我错过了什么 class TestThing def self.parse(args) options = {} options[:time] = 0 options[:operatio

我正在尝试学习如何使用OptPass接受命令行选项,但是我很难让它发挥作用,因为它显示在类文档和我可以在网上找到的任何示例中。特别是当我通过-h选项时,什么都不会出现。我可以输出ARGV及其显示,它接收到-h,但它不会显示opts.banner和或任何选项。我错过了什么

class TestThing

def self.parse(args)
    options = {}
        options[:time]        = 0
        options[:operation]   = :add
        options[:input_file]  = ARGV[-2]
        options[:output_file] = ARGV[-1]
            optparse = OptionParser.new do |opts|
                opts.banner = "Usage:[OPTIONS] input_file output_file"

                opts.separator = ""
                opts.separator = "Specific Options:"


                opts.on('-o', '--operation [OPERATION]', "Add or Subtract time, use 'add' or 'sub'") do |operation|
                    optopns[:operation] = operation.to_sym
                end

                opts.on('-t', '--time [TIME]', "Time to be shifted, in milliseconds") do |time|
                    options[:time] = time
                end

                opts.on_tail("-h", "--help", "Display help screen") do
                    puts opts
                    exit
                end

                opt_parser.parse!(args)
                options
            end
end
end

您需要保留
OptionParser.new的结果,然后调用
parse在其上:

op = OptionParser.new do
  # what you have now
end

op.parse!
请注意,您需要在给
new
的块之外执行此操作,如下所示:

class TestThing

def self.parse(args)
    options = {}
        options[:time]        = 0
        options[:operation]   = :add
        options[:input_file]  = ARGV[-2]
        options[:output_file] = ARGV[-1]
            optparse = OptionParser.new do |opts|
                opts.banner = "Usage:[OPTIONS] input_file output_file"
                # all the rest of your app
            end
            optparse.parse!(args)
end
end
(我留下了缩进,以便更清楚地表达我的意思,但另一方面,如果缩进一致,您会发现代码更容易使用)


另外,您不需要添加
-h
-help
-
选项解析器
自动为您提供这些选项,并完全按照您实现它们的方式执行。

我已经在第8行和底部的第5行中找到了它们,我没有正确地实现这个吗?你需要在你给new的模块之外调用它-更新我的答案以显示我的意思(希望如此)