class Confgit::CLI

Public Class Methods

define_options(command, *banner, &block) click to toggle source

オプション解析を定義する

# File lib/confgit/cli.rb, line 78
def self.define_options(command, *banner, &block)
        define_method "options_#{command}" do |argv|
                options = {}

                OptionParser.new { |opts|
                        begin
                                opts.banner = banner(opts, command, *banner)
                                instance_exec(opts, argv, options, &block)
                        rescue => e
                                abort e.to_s
                        end
                }

                options
        end
end
run(argv = ARGV, options = {}) click to toggle source
# File lib/confgit/cli.rb, line 11
def self.run(argv = ARGV, options = {})
        CLI.new.run(argv, options)
end

Public Instance Methods

action(command, argv, options = {}) click to toggle source

アクションの実行

# File lib/confgit/cli.rb, line 60
def action(command, argv, options = {})
        command = command.gsub(/-/, '_')

        # オプション解析
        options_method = "options_#{command}"
        options.merge!(send(options_method, argv)) if respond_to?(options_method)

        confgit = Repo.new
        confgit.send("confgit_#{command}", options, *argv)
end
banner(opts, method, *args) click to toggle source

サブコマンド・オプションのバナー作成

i18n_init() click to toggle source

I18n を初期化する

# File lib/confgit/cli.rb, line 44
def i18n_init
        I18n.load_path = Dir[File.expand_path('../locales/*.yml', __FILE__)]
        I18n.backend.load_translations
        I18n.enforce_available_locales = false

        locale = ENV['LANG'][0, 2].to_sym if ENV['LANG']
        I18n.locale = locale if I18n.available_locales.include?(locale)
end
run(argv = ARGV, options = {}) click to toggle source
# File lib/confgit/cli.rb, line 15
def run(argv = ARGV, options = {})
        i18n_init

        trap ('SIGINT') { abort '' }

        # コマンド引数の解析
        command = nil

        OptionParser.new { |opts|
                begin
                        opts.version = LONG_VERSION || VERSION
                        opts.banner = "Usage: #{opts.program_name} <command> [<args>]"

                        opts.on('-h', '--help', t(:help))   { abort opts.help }
                        opts.separator ''
                        opts.separator t(:commands)

                        opts.order!(argv)
                        command = argv.shift
                        abort opts.help unless command
                rescue => e
                        abort e.to_s
                end
        }

        action(command, argv, options)
end
t(code, options = {}) click to toggle source

I18n で翻訳する

# File lib/confgit/cli.rb, line 54
def t(code, options = {})
        options[:scope] ||= [:usage]
        I18n.t(code, options)
end