美文网首页
[翻译]使用ruby的commander构建CLI-Builid

[翻译]使用ruby的commander构建CLI-Builid

作者: yohunl | 来源:发表于2019-07-25 15:18 被阅读0次

原文在cli-using-ruby-commander

这是一篇使用ruby和commander来构建一个命令行交互app(command line interface,CLI).

Gemfile

创建一个工程目录,创建一个Gemfile.

mkdir ruby-cli
cd ruby-cli
touch Gemfile

这个Gemfile只包含一个gem,即commander:

备注:commander的github已经从作者写文章时候的https://github.com/tj/commander 迁移到了 https://github.com/commander-rb/commander

source "http://rubygems.org"
gem "commander"

运行bundle install.如果你电脑上没有bundler,首先你需要用gem install bundler来安装它.

➜  bundle install
Fetching gem metadata from http://rubygems.org/...
Resolving dependencies...
Using bundler 2.0.1
Using highline 2.0.2
Using commander 4.4.7
Bundle complete! 1 Gemfile dependency, 3 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

翻译补充: gem可以安装一个包的很多个版本,那么对于多个版本所产生的问题,怎么解决呢?
Bundler https://bundler.io 就是为了解决这个问题的, 它提供了两个命令
bundle install :根据Gemfile中申明的gem全部安装,并将版本号等写入Gemfile.lock文件,保证不同的机器执行的时候,获取到的都是相同的依赖包
bundle exec:替我们解决了多版本环境不隔离的问题。当你执行 bundle exec的时候,Bundler 会把 LOAD_PATH中不相干的那些 gem 的路径全都去掉,然后读取 Gemfile.lock 中的 gem 版本(如果没有 Gemfile.lock 会决议版本后创建一个 Gemfile.lock),保证 $LOAD_PATH中只存在 Gemfile.lock 中已经固定版本的 gem 的路径

代码

下面是我们创建的的文件名为rbuy-cli.rb的示例代码:

#!/usr/bin/env ruby

require 'rubygems'
require "bundler/setup"
require 'commander/import'

program :name, "Ruby CLI"
program :version, '1.0.0'
program :description, 'A simple cli that does nothing'

command :extract do |c|
        c.syntax = 'ruby-cli extract foo -bar=123'
        c.description = 'Extract foo and bar'
        c.option '--bar STRING', String, 'Option bar with a string'
        c.action do |args, options|
                options.default :bar => 'BAR'
                puts 'Extracting..'
                puts "Args: #{args}"
                puts "Option Bar: #{options.bar}"
        end
end

我们现在可以运行ruby ruby-cli.rb extract.当然了,由于这个文件头部添加了使用ruby来解释的声明,还可以简单地使用./ruby-cli.rb来运行.(当然,首先还是得使用chmod a+x ruby-cli.rb来让这个文件具有可执行权限,否则是不可以直接调用的).

译者注

当你运行上面的命令的时候,你可能会遇到和我一样的问题

You must use Bundler 2 or greater with this lockfile.

当你检查

➜  bundle -v
Bundler version 2.0.1

明明bundle已经是2.x的版本了,怎么还提示这个错误呢?
我们可以通过 gem list来查看本地的bundle
看到

➜  gem list

*** LOCAL GEMS ***

activesupport (4.2.11.1)
addressable (2.6.0)
atomos (0.1.3)
aws-sdk (1.67.0)
aws-sdk-v1 (1.67.0)
babosa (1.0.2)
bigdecimal (default: 1.4.1)
bundler (2.0.1, default: 1.17.3)
.......

从上面我们可以看到,对于我们这个版本的ruby(我的是2.6.0),它默认装载的是bundle1.17.3.
解决的办法很简单,就是在
ruby-cli.rb文件的
require "bundler/setup"的上面添加require 'bundler'.

继续

从上面的示例,可以看到,通过commander,我们可以很容易的描述我们的CLI.运行

./ruby-cli.rb --help

,将会展示一个非常友好的关于所有支持的命令的帮助信息.在我们上面的rb文件中,只定义了一个名字为extract的命令.

在我们的extract命令中,我们打印了所有的参数和选项.

这里,只要一个选项 --bar,它带有一个默认的字符串"BAR".

Ruby Gem

直到现在为止,我们都是用 ruby ./ruby-cli.rb ... 来运行的.

其实我们可以将其制作成一个gem包,然后直接像其它命令那样,运行 ruby-cli ...

为了实现制作一个gem包,
我们创建一个bin目录,将脚本移动到bin目录下,并且去掉.rb后缀.

mkdir bin
mv ruby-cli.rb bin/ruby-cli

移动后,我们的目录应该是这样

.
├── Gemfile
├── Gemfile.lock
└── bin
    └── ruby-cli

现在,我们创建一个文件 ruby-cli.gemspec
添加内容

Gem::Specification.new do |s|
  s.name = 'ruby-cli'
  s.version = '1.0.0'
  s.date = '2015-12-25'
  s.summary = "Our simple CLI"
  s.description = "Our very very simple CLI"
  s.authors = [ "Luke Skywalker" ]
  s.email = 'lukeskywalker@gmail.com'
  s.executables << 'ruby-cli'

  s.add_dependency 'commander'
end

现在,编译这个gem:

gem build ruby-cli.gemset

没有错误的话,会在目录下生成文件 ruby-cli-1.0.0.gem

现在,我们已经得到了一个gem的安装文件

通过如下命令来安装它:

gem install ruby-cli-1.0.0.gem

现在,你可以直接运行这个命令了

➜  ruby-cli gem install ruby-cli-1.0.0.gem
Successfully installed ruby-cli-1.0.0
Parsing documentation for ruby-cli-1.0.0
Installing ri documentation for ruby-cli-1.0.0
Done installing documentation for ruby-cli after 0 seconds
1 gem installed
➜  ruby-cli ruby-cli extract foo -bar=1213
Extracting..
Args: ["foo"]
Option Bar: ar=1213

PS:这是我写的第一个Ruby教程.对于使用Ruby,我内心已经挣扎过很多次了,因为像Python和Go这样的语言已经很棒了,并且,我也确信,有一天可以使用Apple的Swift语言来编写脚本.
我不得不使用Ruby的最主要原因是,有很多现有的工具都是用Ruby构建的,例如,我最爱的诸多工具中的Jekyll和Fastlane.

译者添加

我们可以卸载它,卸载的时候,是不带版本号的!!

➜  ruby-cli gem uninstall ruby-cli
Remove executables:
    ruby-cli

in addition to the gem? [Yn]  Y
Removing ruby-cli
Successfully uninstalled ruby-cli-1.0.0

相关文章

网友评论

      本文标题:[翻译]使用ruby的commander构建CLI-Builid

      本文链接:https://www.haomeiwen.com/subject/hquhrctx.html