Class: Inch::CLI::Command::Base Abstract

Inherits:
Object
  • Object
show all
Includes:
TraceHelper
Defined in:
lib/inch/cli/command/base.rb

Overview

This class is abstract.

Subclass and override #run to implement a new command

Abstract base class for CLI controller objects

Direct Known Subclasses

BaseList, Diff

Constant Summary collapse

EXIT_STATUS_SUCCESS =
0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TraceHelper

#ui

Constructor Details

#initialize(kwargs = {}) ⇒ Base

Returns a new instance of Base.



65
66
67
68
# File 'lib/inch/cli/command/base.rb', line 65

def initialize(kwargs = {})
  @ui = kwargs[:ui] if kwargs[:ui]
  initialize_cli_options
end

Instance Attribute Details

#codebaseCodebase::Proxy (readonly)

Returns:



43
44
45
# File 'lib/inch/cli/command/base.rb', line 43

def codebase
  @codebase
end

Class Method Details

.register_command_as(name, default = false) ⇒ void

This method returns an undefined value.

Registers the current Command in the CommandParser

Parameters:

  • name (Symbol)

    name of the Command



60
61
62
63
# File 'lib/inch/cli/command/base.rb', line 60

def self.register_command_as(name, default = false)
  CLI::CommandParser.default_command = name if default
  CLI::CommandParser.commands[name] = self
end

.run(*args) ⇒ Command::Base

Helper method to run an instance with the given args

Returns:

See Also:



49
50
51
52
53
54
# File 'lib/inch/cli/command/base.rb', line 49

def self.run(*args)
  kwargs = args.last.is_a?(Hash) ? args.pop : {}
  command = new(kwargs)
  command.run(*args)
  command
end

Instance Method Details

#descriptionString

Returns a description of the command

Returns:

  • (String)


73
74
75
# File 'lib/inch/cli/command/base.rb', line 73

def description
  ''
end

#exit_statusFixnum

Returns the exit status of the running command. This can be overridden to customize exit statusses.

Returns:

  • (Fixnum)

    zero (by default)



81
82
83
# File 'lib/inch/cli/command/base.rb', line 81

def exit_status
  EXIT_STATUS_SUCCESS
end

#nameString

Returns the name of the command by which it is referenced in the command list

Returns:

  • (String)


89
90
91
92
93
# File 'lib/inch/cli/command/base.rb', line 89

def name
  CommandParser.commands.each do |name, klass|
    return name if klass == self.class
  end
end

#run(*_args) ⇒ Object

This method is abstract.
Note:

Override with implementation

Runs the command with the given args

Parameters:

  • *_args (Array<String>)


100
101
102
# File 'lib/inch/cli/command/base.rb', line 100

def run(*_args)
  fail NotImplementedError
end

#usageString

Returns a description of the command’s usage pattern

Returns:

  • (String)


107
108
109
# File 'lib/inch/cli/command/base.rb', line 107

def usage
  "Usage: inch #{name} [options]"
end