我已经在Windows上安装了RubyInstaller,我正在运行IMAP Sync,但我需要使用它来同步数百个帐户。如果我可以通过命令行将这些变量传递给它,我可以更好地自动化整个过程。
# Source server connection info.
SOURCE_NAME = 'username@example.com'
SOURCE_HOST = 'mail.example.com'
SOURCE_PORT = 143
SOURCE_SSL = false
SOURCE_USER = 'username'
SOURCE_PASS = 'password'
# Destination server connection info.
DEST_NAME = 'username@gmail.com'
DEST_HOST = 'imap.gmail.com'
DEST_PORT = 993
DEST_SSL = true
DEST_USER = 'username@gmail.com'
DEST_PASS = 'password'
博士tl;
我知道这很旧,但这里没有提到getoptlong,它可能是目前解析命令行参数的最佳方法。
解析命令行参数
我强烈推荐getoptlong。它很容易使用,而且效果很好。
下面是一个从上面的链接中提取的例子
require 'getoptlong'
opts = GetoptLong.new(
[ '--help', '-h', GetoptLong::NO_ARGUMENT ],
[ '--repeat', '-n', GetoptLong::REQUIRED_ARGUMENT ],
[ '--name', GetoptLong::OPTIONAL_ARGUMENT ]
)
dir = nil
name = nil
repetitions = 1
opts.each do |opt, arg|
case opt
when '--help'
puts <<-EOF
hello [OPTION] ... DIR
-h, --help:
show help
--repeat x, -n x:
repeat x times
--name [name]:
greet user by name, if name not supplied default is John
DIR: The directory in which to issue the greeting.
EOF
when '--repeat'
repetitions = arg.to_i
when '--name'
if arg == ''
name = 'John'
else
name = arg
end
end
end
if ARGV.length != 1
puts "Missing dir argument (try --help)"
exit 0
end
dir = ARGV.shift
Dir.chdir(dir)
for i in (1..repetitions)
print "Hello"
if name
print ", #{name}"
end
puts
end
你可以这样叫它
ruby你好。Rb -n 6——name——/tmp
OP在做什么
在这种情况下,我认为最好的选择是使用这个答案中建议的YAML文件
除非是最琐碎的情况,否则在Ruby中使用命令行选项只有一种明智的方式。它被称为docopt,并在这里进行了记录。
它的神奇之处在于它的简单。你所要做的就是为你的命令指定“帮助”文本。你在那里写的东西会被独立的ruby库自动解析。
从例子中可以看出:
#!/usr/bin/env ruby
require 'docopt.rb'
doc = <<DOCOPT
Usage: #{__FILE__} --help
#{__FILE__} -v...
#{__FILE__} go [go]
#{__FILE__} (--path=<path>)...
#{__FILE__} <file> <file>
Try: #{__FILE__} -vvvvvvvvvv
#{__FILE__} go go
#{__FILE__} --path ./here --path ./there
#{__FILE__} this.txt that.txt
DOCOPT
begin
require "pp"
pp Docopt::docopt(doc)
rescue Docopt::Exit => e
puts e.message
end
输出:
$ ./counted_example.rb -h
Usage: ./counted_example.rb --help
./counted_example.rb -v...
./counted_example.rb go [go]
./counted_example.rb (--path=<path>)...
./counted_example.rb <file> <file>
Try: ./counted_example.rb -vvvvvvvvvv
./counted_example.rb go go
./counted_example.rb --path ./here --path ./there
./counted_example.rb this.txt that.txt
$ ./counted_example.rb something else
{"--help"=>false,
"-v"=>0,
"go"=>0,
"--path"=>[],
"<file>"=>["something", "else"]}
$ ./counted_example.rb -v
{"--help"=>false, "-v"=>1, "go"=>0, "--path"=>[], "<file>"=>[]}
$ ./counted_example.rb go go
{"--help"=>false, "-v"=>0, "go"=>2, "--path"=>[], "<file>"=>[]}
享受吧!
您应该尝试console_runner gem。这个宝石使您的纯Ruby代码可以从命令行执行。你所需要的就是在你的代码中添加YARD注释:
# @runnable This tool can talk to you. Run it when you are lonely.
# Written in Ruby.
class MyClass
def initialize
@hello_msg = 'Hello'
@bye_msg = 'Good Bye'
end
# @runnable Say 'Hello' to you.
# @param [String] name Your name
# @param [Hash] options options
# @option options [Boolean] :second_meet Have you met before?
# @option options [String] :prefix Your custom prefix
def say_hello(name, options = {})
second_meet = nil
second_meet = 'Nice to see you again!' if options['second_meet']
prefix = options['prefix']
message = @hello_msg + ', '
message += "#{prefix} " if prefix
message += "#{name}. "
message += second_meet if second_meet
puts message
end
end
然后从控制台运行它:
$ c_run /projects/example/my_class.rb say_hello -n John --second-meet --prefix Mr.
-> Hello, Mr. John. Nice to see you again!