Class: Ergane::Command

Inherits:
Object show all
Extended by:
DSL::CommandDSL
Includes:
Ergane::Concerns::Inheritance, Ergane::Concerns::OptionHandling
Defined in:
lib/ergane/command.rb

Direct Known Subclasses

Tool

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL::CommandDSL

aliases, argument, command, description, flag, option

Methods included from Ergane::Concerns::OptionHandling

included

Methods included from Ergane::Concerns::Inheritance

included

Constructor Details

#initialize(argv = []) ⇒ Command

Returns a new instance of Command.



68
69
70
71
# File 'lib/ergane/command.rb', line 68

def initialize(argv = [])
  @options = self.class.build_default_options
  @argv = parse_options(argv.dup)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



62
63
64
# File 'lib/ergane/command.rb', line 62

def options
  @options
end

Class Method Details

.command_nameObject



24
25
26
# File 'lib/ergane/command.rb', line 24

def command_name
  @command_name || derive_command_name
end

.command_name=(name) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ergane/command.rb', line 12

def command_name=(name)
  @command_name = name&.to_sym
  return unless @command_name

  parent = superclass
  if parent.respond_to?(:tool) && parent.abstract_class?
    parent.inherited_command_name_set(self)
  else
    register_subcommand(self)
  end
end

.inherited(subclass) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/ergane/command.rb', line 36

def inherited(subclass)
  super
  subclass.instance_variable_set(:@option_definitions, option_definitions.dup)
  subclass.instance_variable_set(:@argument_definitions, argument_definitions.dup)
  subclass.instance_variable_set(:@subcommands, {})
  register_subcommand(subclass)
end

.subcommandsObject



32
33
34
# File 'lib/ergane/command.rb', line 32

def subcommands
  @subcommands ||= {}
end

.termsObject



28
29
30
# File 'lib/ergane/command.rb', line 28

def terms
  [command_name, *aliases].compact.uniq
end

Instance Method Details

#abbreviate_path(path) ⇒ Object



64
65
66
# File 'lib/ergane/command.rb', line 64

def abbreviate_path(path)
  Ergane.paths.abbreviate(path)
end

#argsObject



73
74
75
# File 'lib/ergane/command.rb', line 73

def args
  @argv
end

#run(*run_args) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/ergane/command.rb', line 77

def run(*run_args)
  if self.class.subcommands.any?
    $stdout.puts HelpFormatter.new(self.class).format
  else
    raise AbstractCommand, "#{self.class.name}#run is not implemented"
  end
end