Class: Clitopic::Command::Base

Inherits:
Object
  • Object
show all
Extended by:
Parser::OptParser
Defined in:
lib/clitopic/command/base.rb

Direct Known Subclasses

ClitoVersion, DefaultsFile, Help, Suggestions, Version

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Parser::OptParser

check_all_required, check_required, help, parse, parser, process_options

Class Attribute Details

.argumentsObject

Returns the value of attribute arguments.



12
13
14
# File 'lib/clitopic/command/base.rb', line 12

def arguments
  @arguments
end

Returns the value of attribute banner.



11
12
13
# File 'lib/clitopic/command/base.rb', line 11

def banner
  @banner
end

.descriptionObject

Returns the value of attribute description.



11
12
13
# File 'lib/clitopic/command/base.rb', line 11

def description
  @description
end

.hiddenObject

Returns the value of attribute hidden.



11
12
13
# File 'lib/clitopic/command/base.rb', line 11

def hidden
  @hidden
end

.inherited_options(*cmds) ⇒ Object

Returns the value of attribute inherited_options.



12
13
14
# File 'lib/clitopic/command/base.rb', line 12

def inherited_options
  @inherited_options
end

.nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/clitopic/command/base.rb', line 11

def name
  @name
end

.optionsObject

Returns the value of attribute options.



12
13
14
# File 'lib/clitopic/command/base.rb', line 12

def options
  @options
end

.short_descriptionObject

Returns the value of attribute short_description.



11
12
13
# File 'lib/clitopic/command/base.rb', line 11

def short_description
  @short_description
end

Class Method Details

.callObject



53
54
55
# File 'lib/clitopic/command/base.rb', line 53

def call()
  puts "call with #{options} #{arguments}"
end

.cmd_optionsObject



14
15
16
# File 'lib/clitopic/command/base.rb', line 14

def cmd_options
  @cmd_options ||= []
end

.fullnameObject



43
44
45
46
47
48
49
50
51
# File 'lib/clitopic/command/base.rb', line 43

def fullname
  if topic.nil?
    return name
  elsif name == 'index'
    "#{topic.name}"
  else
    "#{topic.name}:#{name}"
  end
end

.load_defaults(file = nil) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/clitopic/command/base.rb', line 87

def load_defaults(file=nil)
  file ||= Clitopic::Helpers.find_default_file
  if file.nil?
    return
  end

  begin
    defaults = YAML.load_file(file)
  rescue
    puts "failed to load defaults file: #{file}"
    return
  end

  if self.topic.nil?
    if defaults.has_key?("commands")
      cmd_defaults = defaults["commands"][self.name]
    end
  else

    if defaults.has_key?("topics") && defaults["topics"].has_key?(self.topic.name)
      cmd_defaults = defaults['topics'][self.topic.name][self.name]
      topic_opts = defaults['topics'][self.topic.name]['topic_options']
      load_options(topic_opts)
      if !cmd_defaults.nil?
        load_options(cmd_defaults["options"])
        if cmd_defaults["args"] && !arguments
          @arguments += Array(cmd_defaults["args"])
        end
      end
      self.inherited_options.each do |cmd|
        if defaults["topics"].has_key?(self.topic.name)
          cmd_opts = defaults['topics'][self.topic.name][cmd.to_s]
          load_options(cmd_opts["options"]) if !cmd_opts.nil?
        end
      end
    end
  end
end

.load_options(opts) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/clitopic/command/base.rb', line 73

def load_options(opts)
  return if opts.nil?
  opts.each do |name, value|
    name = name.to_s.to_sym
    if !value.nil?
      if options[name].is_a?(Array)
        options[name] += value
      else
        options[name] = value
      end
    end
  end
end

.option(name, *args, &blk) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/clitopic/command/base.rb', line 35

def option(name, *args, &blk)
  opt = Clitopic::Utils.parse_option(name, *args, &blk)
  if !opt[:default].nil?
    options[name] = opt[:default]
  end
  cmd_options << opt
end

.register(opts = {}) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/clitopic/command/base.rb', line 140

def register(opts={})
  opts = {hidden: false}.merge(opts)
  if !opts.has_key?(:name)
    raise ArgumentError.new("missing Command name")
  end

  topic(opts[:topic])
  @description = opts[:description]
  @name = opts[:name]
  @hidden = opts[:hidden]
  @banner = opts[:banner]
  @short_description = opts[:short_description]

  if @topic.nil?
    Clitopic::Commands.global_commands[name] = self
  else
    topic.commands[name] = self
  end
end

.topic(arg = nil) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/clitopic/command/base.rb', line 126

def topic(arg=nil)
  if !arg.nil?
    if arg.is_a?(String)
      @topic ||= Topics[arg]
    elsif arg.is_a?(Class) && arg < Clitopic::Topic::Base
      @topic ||= arg.instance
    elsif arg.is_a?(Hash)
      @topic ||= Clitopic::Topic::Base.register(arg)
    end
  end
  return @topic
end

.topic_optionsObject



61
62
63
64
65
66
67
# File 'lib/clitopic/command/base.rb', line 61

def topic_options
  if !self.topic.nil?
    @topic_options ||= self.topic.class.options
  else
    @topic_options = {}
  end
end