Class: FluidCLI::Command

Inherits:
CLI::Kit::BaseCommand
  • Object
show all
Defined in:
lib/fluid_cli/command.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ctx = nil) ⇒ Command

Returns a new instance of Command.



49
50
51
52
53
# File 'lib/fluid_cli/command.rb', line 49

def initialize(ctx = nil)
  super()
  @ctx = ctx || FluidCLI::Context.new
  self.options = Options.new
end

Class Attribute Details

.ctx=(value) ⇒ Object (writeonly)

Sets the attribute ctx

Parameters:

  • value

    the value to set the attribute ctx to.



10
11
12
# File 'lib/fluid_cli/command.rb', line 10

def ctx=(value)
  @ctx = value
end

Instance Attribute Details

#ctx=(value) ⇒ Object (writeonly)

Sets the attribute ctx

Parameters:

  • value

    the value to set the attribute ctx to.



6
7
8
# File 'lib/fluid_cli/command.rb', line 6

def ctx=(value)
  @ctx = value
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/fluid_cli/command.rb', line 7

def options
  @options
end

Class Method Details

.call(args, command_name) ⇒ Object



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

def call(args, command_name, *)
  subcommand, resolved_name = subcommand_registry.lookup_command(args.first)
  if subcommand
    subcommand.ctx = @ctx
    subcommand.call(args.drop(1), resolved_name, command_name)
  else
    cmd = new(@ctx)
    cmd.options.parse(@_options, args)
    cmd.call(args, command_name)
  end
end

.call_help(*cmds) ⇒ Object



43
44
45
46
# File 'lib/fluid_cli/command.rb', line 43

def call_help(*cmds)
  help = Commands::Help.new(@ctx)
  help.call(cmds, nil)
end

.options(&block) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/fluid_cli/command.rb', line 24

def options(&block)
  existing_options = @_options
  # We prevent new options calls to override existing blocks by nesting them.
  @_options = ->(parser, flags) {
    existing_options&.call(parser, flags)
    block.call(parser, flags)
  }
end

.subcommand(const, cmd, path = nil) ⇒ Object



33
34
35
36
# File 'lib/fluid_cli/command.rb', line 33

def subcommand(const, cmd, path = nil)
  autoload(const, path) if path
  subcommand_registry.add(->() { const_get(const) }, cmd.to_s)
end

.subcommand_registryObject



38
39
40
41
# File 'lib/fluid_cli/command.rb', line 38

def subcommand_registry
  @subcommand_registry ||= CLI::Kit::CommandRegistry.new(
    default: nil)
end