Class: S7::S7Cli::Command

Inherits:
Object
  • Object
show all
Includes:
GetText
Defined in:
lib/s7/s7cli/command.rb

Overview

コマンドを表現する。

Constant Summary collapse

@@command_classes =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GetText

#N_, #_, bindtextdomain, included

Constructor Details

#initialize(world) ⇒ Command

Returns a new instance of Command.



54
55
56
57
58
# File 'lib/s7/s7cli/command.rb', line 54

def initialize(world)
  @world = world
  @config = {}
  @options = []
end

Instance Attribute Details

#configObject (readonly)

設定。



15
16
17
# File 'lib/s7/s7cli/command.rb', line 15

def config
  @config
end

#optionsObject (readonly)

オプションの配列。



12
13
14
# File 'lib/s7/s7cli/command.rb', line 12

def options
  @options
end

#worldObject (readonly)

World オブジェクト。



18
19
20
# File 'lib/s7/s7cli/command.rb', line 18

def world
  @world
end

Class Method Details

.create_instance(name, world) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/s7/s7cli/command.rb', line 31

def create_instance(name, world)
  klass = @@command_classes[name]
  if klass
    return klass.new(world)
  else
    return nil
  end
end

.split_name_and_argv(s) ⇒ Object

s で指定した文字列をコマンド名とオプションに分割する。



24
25
26
27
28
29
# File 'lib/s7/s7cli/command.rb', line 24

def split_name_and_argv(s)
  s = s.strip
  name = s.slice!(/\A\w+/)
  argv = s.split
  return name, argv
end

Instance Method Details

#aliasesObject

別名の配列を取得する。



66
67
68
# File 'lib/s7/s7cli/command.rb', line 66

def aliases
  return self.class.const_get("ALIASES")
end

#descriptionObject

概要を取得する。



71
72
73
# File 'lib/s7/s7cli/command.rb', line 71

def description
  return self.class.const_get("DESCRIPTION")
end

#helpObject

使用方法を示す文字列を返す。



99
100
101
# File 'lib/s7/s7cli/command.rb', line 99

def help
  puts(option_parser)
end

#nameObject

コマンド名を取得する。



61
62
63
# File 'lib/s7/s7cli/command.rb', line 61

def name
  return self.class.const_get("NAME")
end

#option_parserObject

コマンドラインオプションを解析するための OptionParser オブジェ クトを取得する。



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/s7/s7cli/command.rb', line 82

def option_parser
  return OptionParser.new { |opts|
    opts.banner = [
                   "#{name} (#{aliases.join(",")}): #{description}",
                   usage,
                  ].join("\n")
    if options && options.length > 0
      opts.separator("")
      opts.separator("Valid options:")
      for option in options
        option.define(opts, config)
      end
    end
  }
end

#usageObject

使用例を取得する。



76
77
78
# File 'lib/s7/s7cli/command.rb', line 76

def usage
  return self.class.const_get("USAGE")
end