Class: Rbcli::Command
- Inherits:
-
Object
- Object
- Rbcli::Command
- Extended by:
- Rbcli::Core::CmdLibrary
- Defined in:
- lib/rbcli/components/commands/command.rb,
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]> #
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Class Method Summary collapse
- .action(&block) ⇒ Object
- .default ⇒ Object
- .description(desc = nil) ⇒ Object
- .helptext(text = nil) ⇒ Object
- .parameter(*args) ⇒ Object
- .script(inline: nil, path: nil, vars: nil) ⇒ Object
- .usage(text = nil) ⇒ Object
Instance Method Summary collapse
- #action(&block) ⇒ Object
- #data ⇒ Object
- #default ⇒ Object
- #default? ⇒ Boolean
- #description(text = nil) ⇒ Object
- #external_script(path: nil, vars: nil) ⇒ Object
- #has_function? ⇒ Boolean
- #helptext(text = nil) ⇒ Object
-
#initialize(name) ⇒ Command
constructor
A new instance of Command.
- #inline_script(inline: nil, vars: nil) ⇒ Object
- #parameter(*args) ⇒ Object
- #run!(opts: nil, params: nil, args: nil, config: nil, env: nil) ⇒ Object
- #usage(text = nil) ⇒ Object
Constructor Details
#initialize(name) ⇒ Command
Returns a new instance of Command.
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/rbcli/components/commands/command.rb', line 7 def initialize name raise Rbcli::CommandError.new "Command name can only contain the characters [A-Za-z0-9_]+" unless /[A-Za-z0-9_]+/.match?(name) @name = name.to_s.downcase @default = false @description = nil @helptext = nil @usage = nil @action = nil @script = nil @params = [] end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
19 20 21 |
# File 'lib/rbcli/components/commands/command.rb', line 19 def name @name end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
19 20 21 |
# File 'lib/rbcli/components/commands/command.rb', line 19 def params @params end |
Class Method Details
.action(&block) ⇒ Object
90 91 92 |
# File 'lib/rbcli/components/commands/command_old.rb', line 90 def self.action & block @data[:action] = block end |
.default ⇒ Object
78 79 80 |
# File 'lib/rbcli/components/commands/command_old.rb', line 78 def self.default @data[:default] = true end |
.description(desc = nil) ⇒ Object
66 67 68 |
# File 'lib/rbcli/components/commands/command_old.rb', line 66 def self.description desc = nil desc.nil? ? @data[:description] : @data[:description] = desc end |
.helptext(text = nil) ⇒ Object
70 71 72 |
# File 'lib/rbcli/components/commands/command_old.rb', line 70 def self.helptext text = nil text.nil? ? @data[:helptext] : @data[:helptext] = text end |
.parameter(*args) ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/rbcli/components/commands/command_old.rb', line 82 def self.parameter * args if !args[2].nil? && !args[2].empty? && args[2][:prompt].is_a?(String) @data[:parameter_prompts][args[0]] = { prompt: args[2][:prompt], default: args[2][:default], type: args[2][:type].nil? ? nil : args[2][:type].to_s.chomp('s').to_sym } args[2][:required] = false end @data[:parser].opt *args end |
.script(inline: nil, path: nil, vars: nil) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/rbcli/components/commands/command_old.rb', line 94 def self.script inline: nil, path: nil, vars: nil if !inline.nil? && !path.nil? raise Rbcli::CommandError.new "Can't assign both an inline script and an external path to command '#{self.name}'" end if path == :builtin or path.nil? callerscript = caller_locations.first.absolute_path path = "#{File.dirname(callerscript)}/scripts/#{File.basename(callerscript, ".*")}.sh" end require File.join(RBCLI_LIBDIR, 'components', 'scriptwrapper', 'scriptwrapper.rb') @data[:script] = Rbcli::ScriptWrapper.new inline: inline, path: path, vars: vars end |
.usage(text = nil) ⇒ Object
74 75 76 |
# File 'lib/rbcli/components/commands/command_old.rb', line 74 def self.usage text = nil text.nil? ? @data[:usage] : @data[:usage] = text end |
Instance Method Details
#action(&block) ⇒ Object
49 50 51 52 |
# File 'lib/rbcli/components/commands/command.rb', line 49 def action & block @action = block @script = nil end |
#data ⇒ Object
62 63 64 |
# File 'lib/rbcli/components/commands/command_old.rb', line 62 def data self.class.data end |
#default ⇒ Object
36 37 38 |
# File 'lib/rbcli/components/commands/command.rb', line 36 def default @default = true end |
#default? ⇒ Boolean
40 41 42 |
# File 'lib/rbcli/components/commands/command.rb', line 40 def default? @default end |
#description(text = nil) ⇒ Object
21 22 23 24 |
# File 'lib/rbcli/components/commands/command.rb', line 21 def description text = nil return @description if text.nil? @description = text end |
#external_script(path: nil, vars: nil) ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/rbcli/components/commands/command.rb', line 60 def external_script path: nil, vars: nil unless File.exist?(path) && File.readable?(path) && File.executable?(path) Rbcli.log.fatal "Command #{@name} expects a script to be located at: #{path}. Please make sure it exists and is both readable and executable by the current user." Rbcli::exit 1 end require_relative 'scriptwrapper/scriptwrapper' @action = nil @script = Rbcli::ScriptWrapper.new inline: nil, path: path, vars: vars end |
#has_function? ⇒ Boolean
70 71 72 |
# File 'lib/rbcli/components/commands/command.rb', line 70 def has_function? !(@action.nil? && @script.nil?) end |
#helptext(text = nil) ⇒ Object
26 27 28 29 |
# File 'lib/rbcli/components/commands/command.rb', line 26 def helptext text = nil return @helptext if text.nil? @helptext = text end |
#inline_script(inline: nil, vars: nil) ⇒ Object
54 55 56 57 58 |
# File 'lib/rbcli/components/commands/command.rb', line 54 def inline_script inline: nil, vars: nil require_relative 'scriptwrapper/scriptwrapper' @action = nil @script = Rbcli::ScriptWrapper.new inline: inline, path: nil, vars: vars end |
#parameter(*args) ⇒ Object
44 45 46 47 |
# File 'lib/rbcli/components/commands/command.rb', line 44 def parameter * args args[2][:required] = false if !args[2].nil? && !args[2].empty? && args[2][:prompt].is_a?(String) @params.push(args) end |
#run!(opts: nil, params: nil, args: nil, config: nil, env: nil) ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/rbcli/components/commands/command.rb', line 74 def run! opts: nil, params: nil, args: nil, config: nil, env: nil unless self.has_function? raise Rbcli::CommandError.new "Command #{@name} does not have a function. An action, inline script, or external script must be defined." end # Rbcli.log.debug "Running command '#{@name}'", "CORE" @action.call(opts, params, args, config, env) unless @action.nil? @script.execute(opts, params, args, config, env) unless @script.nil? end |
#usage(text = nil) ⇒ Object
31 32 33 34 |
# File 'lib/rbcli/components/commands/command.rb', line 31 def usage text = nil return @usage if text.nil? @usage = text end |