Class: Tweetwine::CLI::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/tweetwine/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Command

Returns a new instance of Command.



191
192
193
194
# File 'lib/tweetwine/cli.rb', line 191

def initialize(args)
  parsing_succeeded = parse(args)
  self.class.abort_with_usage unless parsing_succeeded
end

Class Method Details

.abort_with_usageObject



185
186
187
188
# File 'lib/tweetwine/cli.rb', line 185

def abort_with_usage
  show_usage
  exit CommandLineError.status_code
end

.about(description = nil) ⇒ Object



153
154
155
156
# File 'lib/tweetwine/cli.rb', line 153

def about(description = nil)
  return @about unless description
  @about = description.chomp
end

.inherited(child) ⇒ Object



145
146
147
148
149
150
151
# File 'lib/tweetwine/cli.rb', line 145

def inherited(child)
  # Silence warnings about uninitialized variables if a child does not
  # set its about, name, or usage.
  child.instance_eval do
    @about, @name, @usage = nil
  end
end

.nameObject



163
164
165
# File 'lib/tweetwine/cli.rb', line 163

def name
  @name
end

.register(*names) ⇒ Object



158
159
160
161
# File 'lib/tweetwine/cli.rb', line 158

def register(*names)
  @name = names.first
  CLI.register_command(self, names)
end

.show_usage(about_cmd = self) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
# File 'lib/tweetwine/cli.rb', line 173

def show_usage(about_cmd = self)
  about = about_cmd.about
  name = about_cmd.name
  usage = about_cmd.usage
  result = <<-END
#{about}

Usage: #{CLI::EXEC_NAME} #{name} #{usage}
  END
  CLI.ui.info result.strip!
end

.usage(description = nil) ⇒ Object

Usage description for the command, use if overriding #parse.



168
169
170
171
# File 'lib/tweetwine/cli.rb', line 168

def usage(description = nil)
  return @usage unless description
  @usage = description
end

Instance Method Details

#parse(args) ⇒ Object

Default behavior, which succeeds always; override for real argument parsing if the command needs arguments.



198
199
200
# File 'lib/tweetwine/cli.rb', line 198

def parse(args)
  true
end