Commander是Ruby命令行执行补全解决方案。
安装:
$ gem install commander代码示例:
require 'rubygems'require 'commander/import'# :name is optional, otherwise uses the basename of this executableprogram :name, 'Foo Bar'program :version, '1.0.0'program :description, 'Stupid command that prints foo or bar.'command :foo do |c| c.syntax = 'foobar foo' c.description = 'Displays foo' c.action do |args, options| say 'foo' endendcommand :bar do |c| c.syntax = 'foobar bar [options]' c.description = 'Display bar with optional prefix and suffix' c.option '--prefix STRING', String, 'Adds a prefix to bar' c.option '--suffix STRING', String, 'Adds a suffix to bar' c.action do |args, options| options.default :prefix => '(', :suffix => ')' say "#{options.prefix}bar#{options.suffix}" endend示例输出:
$ foobar bar# => (bar)$ foobar bar --suffix '}' --prefix '{'# => {bar}
评论