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, flag, option

Methods included from DSL::Macros

#dsl_value

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.



105
106
107
108
# File 'lib/ergane/command.rb', line 105

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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.argument_required?(index) ⇒ Boolean

Effective required-ness of the positional argument at index. An explicit DSL ‘required:` (true/false) wins; otherwise it’s derived from the run method’s matching positional parameter: a required parameter (‘run(name)`) means required, while an optional one (`run(name = nil)`) or a splat (`run(*)`) means optional.

Returns:

  • (Boolean)


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

def argument_required?(index)
  defn = argument_definitions[index]
  return false unless defn
  return defn.required unless defn.required.nil?

  param = run_positional_parameters[index]
  param ? param.first == :req : false
end

.command_nameObject



17
18
19
# File 'lib/ergane/command.rb', line 17

def command_name
  @command_name || derive_command_name
end

.command_name=(name) ⇒ Object



12
13
14
15
# File 'lib/ergane/command.rb', line 12

def command_name=(name)
  @command_name = name&.to_sym
  register!
end

.inherited(subclass) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/ergane/command.rb', line 43

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, {})
  subclass.send(:register!)
end

.subcommandsObject



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

def subcommands
  @subcommands ||= {}
end

.termsObject



21
22
23
# File 'lib/ergane/command.rb', line 21

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

Instance Method Details

#abbreviate_path(path) ⇒ Object



101
102
103
# File 'lib/ergane/command.rb', line 101

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

#argsObject



110
111
112
# File 'lib/ergane/command.rb', line 110

def args
  @argv
end

#run(*run_args) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/ergane/command.rb', line 114

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