Module: Rbcli::Core::CmdLibrary

Included in:
Rbcli::Command
Defined in:
lib/rbcli/components/commands/command_old.rb

Overview

Rbcli – A framework for developing command line applications in Ruby #

Copyright (C) 2024 Andrew Khoury <[email protected]>                        #

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object



7
8
9
# File 'lib/rbcli/components/commands/command_old.rb', line 7

def self.extended klass
  klass.instance_variable_set :@commands, {}
end

Instance Method Details

#commandsObject



48
49
50
# File 'lib/rbcli/components/commands/command_old.rb', line 48

def commands
  @commands.select { |name, cmd| !cmd.data[:default] }
end

#dataObject



44
45
46
# File 'lib/rbcli/components/commands/command_old.rb', line 44

def data
  self.instance_variable_get :@data;
end

#default_commandObject



52
53
54
55
56
# File 'lib/rbcli/components/commands/command_old.rb', line 52

def default_command
  default_commands = @commands.select { |_name, cmd| cmd.data[:default] }
  raise Rbcli::CommandError.new "Can't execute - more than one command has been specified as default: #{default_commands.keys}" if default_commands.count > 1
  default_commands.count == 1 ? default_commands.first : nil
end

#inherited(subklass) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rbcli/components/commands/command_old.rb', line 11

def inherited subklass
  raise Rbcli::CommandError.new "The command #{subklass.name.downcase} cannot be defined twice." unless @commands[subklass.name.downcase].nil?
  subklass.instance_variable_set :@data, {
    description: nil,
    helptext: nil,
    default: nil,
    action: nil,
    script: nil,
    usage: nil,
    parameter_prompts: {},
    parser: Optimist::Parser.new
  }
  @commands[subklass.name.downcase] = subklass.new
  Rbcli::Cliopts.register_command subklass.name.downcase
  Rbcli.log.debug "Registered command '#{subklass.name.downcase}'", "CORE"

  Rbcli::Engine.register_operation Proc.new {
    Rbcli.log.debug "Running command '#{Rbcli::Cliopts.cmd[:name]}'", "CORE"
    Rbcli::Cliopts.cmd[:command].data[:action].call(Rbcli::Cliopts.opts,
                                                    Rbcli::Cliopts.cmd[:params],
                                                    Rbcli::Cliopts.cmd[:args],
                                                    (Rbcli::Configurate.constants.include?(:Config) ? Rbcli.configuration(:config)[:config] : Rbcli::Config.new(suppress_errors: true)),
                                                    (Rbcli::Configurate.constants.include?(:Envvars) ? Rbcli.configuration(:envvars)[:envvars] : Rbcli::Config.new(suppress_errors: true))
    ) unless Rbcli::Cliopts.cmd[:command].data[:action].nil?
    Rbcli::Cliopts.cmd[:command].data[:script].execute(Rbcli::Cliopts.opts,
                                                       Rbcli::Cliopts.cmd[:params],
                                                       Rbcli::Cliopts.cmd[:args],
                                                       (Rbcli::Configurate.constants.include?(:Config) ? Rbcli.configuration(:config)[:config] : Rbcli::Config.new(suppress_errors: true)),
                                                       (Rbcli::Configurate.constants.include?(:Envvars) ? Rbcli.configuration(:envvars)[:envvars] : Rbcli::Config.new(suppress_errors: true))
    ) unless Rbcli::Cliopts.cmd[:command].data[:script].nil?
  }, name: :run_command, priority: 100 if @commands.count == 1
end