Sha256: a6e3d39d19e346448762b45c6442a48aab1a58e34eb1af6421ce937e746cf906

Contents?: true

Size: 1.36 KB

Versions: 5

Compression:

Stored size: 1.36 KB

Contents

# -*- coding: utf-8 -*-
#
# Copyright 2013 whiteleaf. All rights reserved.
#

require "optparse"

module Command
  class CommandBase
    def initialize(postfix = "")
      @opt = OptionParser.new(nil, 20)
      @opt.banner = ("<bold><green>" +
                     TermColor.escape("Usage: narou #{self.class.to_s.scan(/::(.+)$/)[0][0].downcase} #{postfix}") +
                     "</green></bold>").termcolor
      @options = {}
    end

    def execute(argv)
      @opt.parse!(argv)
    rescue OptionParser::InvalidOption => e
      error "不正なオプションです(#{e})"
      exit 1
    rescue OptionParser::MissingArgument => e
      error "オプションの引数が不正です(#{e})"
      exit 1
    end

    def load_local_settings
      command_prefix = self.class.to_s.scan(/[^:]+$/)[0].downcase
      local_settings = LocalSetting.get["local_setting"]
      local_settings.each do |name, value|
        if name =~ /^#{command_prefix}\.(.+)$/
          @options[$1] = value
        end
      end
    end

    #
    # 普通にコマンドを実行するけど、exit(2) を補足してexitstatus を返す
    # 正常終了なら0
    #
    def self.execute!(argv)
      self.new.execute(argv)
    rescue SystemExit => e
      e.status
    else
      0
    end

    def oneline_help(msg)
      ""
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
narou-1.5.11 lib/commandbase.rb
narou-1.5.10.1 lib/commandbase.rb
narou-1.5.10 lib/commandbase.rb
narou-1.5.9 lib/commandbase.rb
narou-1.5.8 lib/commandbase.rb