Ruby 从Thor类外部访问命令行参数
对Ruby和OO来说是相当陌生的。研究教科书和谷歌在Thor上找到的所有文章 我让Thor负责捕获多个命令行参数和选项。不过,我希望在Cli类之外完成其余的编程,并且在从Cli类之外访问命令行参数时遇到了问题 问题: 问题1。CliRuby 从Thor类外部访问命令行参数,ruby,thor,Ruby,Thor,对Ruby和OO来说是相当陌生的。研究教科书和谷歌在Thor上找到的所有文章 我让Thor负责捕获多个命令行参数和选项。不过,我希望在Cli类之外完成其余的编程,并且在从Cli类之外访问命令行参数时遇到了问题 问题: 问题1。Cli
#!/usr/bin/env ruby
require 'thor'
class Cli < Thor
attr_reader :a, :b
method_option :add, :type => :string, :desc => 'add servers'
method_option :prod, :type => :string, :desc => 'production stack'
desc "tier <stack folder name> <app | web>", "creates an app or web server tier for the stack"
def tier(a,b)
@a = a
@b = b
puts a
puts b
end
end
Cli.start
arguments = Cli.new
puts "the first argument is #{arguments.a}"
--
放置参数.tier.a
引发了以下错误:
./create.rb:11:in `tier': wrong number of arguments (0 for 2) (ArgumentError)
from ./create.rb:23:in `<main>'
输出:
$ ./create_wo_thor.rb
a
实例化
Cli
类没有多大意义;雷神不是这样设计的
您有几个选项可以从类外部访问内部数据。如果您只想访问几个变量,那么将它们存储为类变量,并通过getter(如果需要,还可以通过setter)使它们可用,这样可以:
require 'thor'
class Cli < Thor
method_option :add, :type => :string, :desc => 'add servers'
method_option :prod, :type => :string, :desc => 'production stack'
desc "tier <stack folder name> <app | web>", "creates an app or web server tier for the stack"
def tier(a,b)
@@a = a
@@b = b
puts a
puts b
end
def self.get_a
@@a
end
def self.get_b
@@b
end
end
Cli.start
puts "the first argument is #{Cli.get_a}"
什么是最好的取决于你打算如何使用它。如果您只想原始访问命令行参数,
ARGV
是一种方法。如果你想在Thor为你做了一些处理之后访问某些片段,前两个中的一个可能会更有帮助。实例化你的Cli
类没有多大意义;雷神不是这样设计的
您有几个选项可以从类外部访问内部数据。如果您只想访问几个变量,那么将它们存储为类变量,并通过getter(如果需要,还可以通过setter)使它们可用,这样可以:
require 'thor'
class Cli < Thor
method_option :add, :type => :string, :desc => 'add servers'
method_option :prod, :type => :string, :desc => 'production stack'
desc "tier <stack folder name> <app | web>", "creates an app or web server tier for the stack"
def tier(a,b)
@@a = a
@@b = b
puts a
puts b
end
def self.get_a
@@a
end
def self.get_b
@@b
end
end
Cli.start
puts "the first argument is #{Cli.get_a}"
什么是最好的取决于你打算如何使用它。如果您只想原始访问命令行参数,
ARGV
是一种方法。如果您想在Thor为您完成一些处理后访问某些部分,前两个选项中的一个可能更有用。以下是我的代码,其中包括从Cli#!/usr/bin/env ruby
require 'thor'
$args = {}
class Cli < Thor
attr_reader :a, :b
method_option :add, :type => :string, :desc => 'add servers'
method_option :prod, :type => :string, :desc => 'production stack'
desc "tier <stack folder name> <app | web>", "creates an app or web server tier for the stack"
def tier(a,b)
# store a and b in a global hash
$args[:a] = a
$args[:b] = b
# store a and b in class variables
@@a = a
@@b = b
end
# getter methods, for access of the class variables from outside the class
def self.get_a
@@a
end
def self.get_b
@@b
end
end
Cli.start
# three ways now to access the command line arguments from outside the Cli < Thor class
puts "the first argument, from $args[:a], is #{$args[:a]}"
puts "the second argument, from Cli.get_b, is #{Cli.get_b}"
puts "the first argument, from ARGV[1], is #{ARGV[1]}"
下面是我的代码,其中包括从Cli
#!/usr/bin/env ruby
require 'thor'
$args = {}
class Cli < Thor
attr_reader :a, :b
method_option :add, :type => :string, :desc => 'add servers'
method_option :prod, :type => :string, :desc => 'production stack'
desc "tier <stack folder name> <app | web>", "creates an app or web server tier for the stack"
def tier(a,b)
# store a and b in a global hash
$args[:a] = a
$args[:b] = b
# store a and b in class variables
@@a = a
@@b = b
end
# getter methods, for access of the class variables from outside the class
def self.get_a
@@a
end
def self.get_b
@@b
end
end
Cli.start
# three ways now to access the command line arguments from outside the Cli < Thor class
puts "the first argument, from $args[:a], is #{$args[:a]}"
puts "the second argument, from Cli.get_b, is #{Cli.get_b}"
puts "the first argument, from ARGV[1], is #{ARGV[1]}"
关于ARGV仍然可用的观点很好!有趣且好的是,ARGV忽略了命令行上的方法名。换句话说,对于/thor tier a b,ARGV[1]是“a”而不是“tier”。想知道Thor是否处理ARGV[]的赋值。它实际上在那里,零索引。这是纯Ruby(Thor在这里查找参数)。哦,对了,我在想bash,命令上的每个空格分隔的字符串都存储在$n中,从0开始,但在Ruby中,args存储在ARGV[]中,从0开始,文件名存储在$0中。ARGV仍然可用,这一点很好!有趣且好的是,ARGV忽略了命令行上的方法名。换句话说,对于/thor tier a b,ARGV[1]是“a”而不是“tier”。想知道Thor是否处理ARGV[]的赋值。它实际上在那里,零索引。这是纯Ruby(Thor在其中查找参数)。哦,对了,我在想bash,命令上的每个空格分隔字符串都存储在$n中,从0开始,但在Ruby中,args存储在ARGV[]中,从0开始,文件名存储在$0中。
require 'thor'
$args = {}
class Cli < Thor
method_option :add, :type => :string, :desc => 'add servers'
method_option :prod, :type => :string, :desc => 'production stack'
desc "tier <stack folder name> <app | web>", "creates an app or web server tier for the stack"
def tier(a,b)
$args[:a] = a
$args[:b] = b
puts a
puts b
end
end
Cli.start
puts "the first argument is #{$args[:a]}"
require 'thor'
class Cli < Thor
method_option :add, :type => :string, :desc => 'add servers'
method_option :prod, :type => :string, :desc => 'production stack'
desc "tier <stack folder name> <app | web>", "creates an app or web server tier for the stack"
def tier(a,b)
puts a
puts b
end
end
Cli.start
puts "The first argugment is #{ARGV[1]}"
#!/usr/bin/env ruby
require 'thor'
$args = {}
class Cli < Thor
attr_reader :a, :b
method_option :add, :type => :string, :desc => 'add servers'
method_option :prod, :type => :string, :desc => 'production stack'
desc "tier <stack folder name> <app | web>", "creates an app or web server tier for the stack"
def tier(a,b)
# store a and b in a global hash
$args[:a] = a
$args[:b] = b
# store a and b in class variables
@@a = a
@@b = b
end
# getter methods, for access of the class variables from outside the class
def self.get_a
@@a
end
def self.get_b
@@b
end
end
Cli.start
# three ways now to access the command line arguments from outside the Cli < Thor class
puts "the first argument, from $args[:a], is #{$args[:a]}"
puts "the second argument, from Cli.get_b, is #{Cli.get_b}"
puts "the first argument, from ARGV[1], is #{ARGV[1]}"
$ ./create.rb tier a b
the first argument, from $args[:a], is a
the second argument, from Cli.get_b, is b
the first argument, from ARGV[1], is a