Class: Rbcli::Command
- Inherits:
-
Object
- Object
- Rbcli::Command
- Defined in:
- lib/rbcli/scriptwrapping/scriptwrapper.rb,
lib/rbcli/engine/command.rb
Overview
RBCli -- A framework for developing command line applications in Ruby # Copyright (C) 2018 Andrew Khoury # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see https://www.gnu.org/licenses/. # # For questions regarding licensing, please contact [email protected] #
Class Method Summary collapse
- .action(&block) ⇒ Object
- .add_command(name, klass) ⇒ Object
- .commands ⇒ Object
- .config_default(*params) ⇒ Object
- .config_defaults(filename) ⇒ Object
-
.description(desc) ⇒ Object
Interface Functions.
-
.descriptions(indent_size, justification) ⇒ Object
Returns all descriptions for display in CLI help.
- .extern(path: nil, envvars: nil, &block) ⇒ Object
- .inherited(subklass) ⇒ Object
- .parameter(name, description, short: nil, type: :boolean, default: nil, required: false, permitted: nil) ⇒ Object
-
.runcmd(cmd, local_params, cliopts) ⇒ Object
Run a given command.
- .usage(usage) ⇒ Object
Instance Method Summary collapse
- #action ⇒ Object
- #description ⇒ Object
- #extern ⇒ Object
- #paramlist ⇒ Object
-
#parseopts(*args) ⇒ Object
This method reads the parameters provided by the class and parses them from the CLI.
- #usage ⇒ Object
Class Method Details
.action(&block) ⇒ Object
50 |
# File 'lib/rbcli/engine/command.rb', line 50 def self.action █ @action = block end |
.add_command(name, klass) ⇒ Object
33 34 35 |
# File 'lib/rbcli/engine/command.rb', line 33 def self.add_command name, klass @commands[name.downcase] = klass.new end |
.commands ⇒ Object
37 38 39 |
# File 'lib/rbcli/engine/command.rb', line 37 def self.commands @commands end |
.config_default(*params) ⇒ Object
71 72 73 |
# File 'lib/rbcli/engine/command.rb', line 71 def self.config_default *params Rbcli::Config::add_default *params end |
.config_defaults(filename) ⇒ Object
67 68 69 |
# File 'lib/rbcli/engine/command.rb', line 67 def self.config_defaults filename Rbcli::Config::add_defaults filename end |
.description(desc) ⇒ Object
Interface Functions
44 |
# File 'lib/rbcli/engine/command.rb', line 44 def self.description desc; @desc = desc end |
.descriptions(indent_size, justification) ⇒ Object
Returns all descriptions for display in CLI help
96 97 98 99 100 101 102 103 104 |
# File 'lib/rbcli/engine/command.rb', line 96 def self.descriptions indent_size, justification #descmap = @commands.map { |name, klass| [name, klass.description] }.to_h @commands.map do |name, cmdobj| desc = '' indent_size.times { desc << ' ' } desc << name.ljust(justification) desc << cmdobj.description end.join("\n") end |
.extern(path: nil, envvars: nil, &block) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/rbcli/scriptwrapping/scriptwrapper.rb', line 22 def self.extern path: nil, envvars: nil, &block if path == :default callerscript = caller_locations.first.absolute_path path = "#{File.dirname(callerscript)}/scripts/#{File.basename(callerscript, ".*")}.sh" end block = nil unless block_given? @extern = Rbcli::Scriptwrapper.new path, envvars, block end |
.inherited(subklass) ⇒ Object
29 30 31 |
# File 'lib/rbcli/engine/command.rb', line 29 def self.inherited subklass @commands[subklass.name.downcase] = subklass.new end |
.parameter(name, description, short: nil, type: :boolean, default: nil, required: false, permitted: nil) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rbcli/engine/command.rb', line 53 def self.parameter name, description, short: nil, type: :boolean, default: nil, required: false, permitted: nil default ||= false if (type == :boolean || type == :bool || type == :flag) @paramlist ||= {} @paramlist[name.to_sym] = { description: description, type: type, default: default, required: required, permitted: permitted, short: short } end |
.runcmd(cmd, local_params, cliopts) ⇒ Object
Run a given command
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/rbcli/engine/command.rb', line 82 def self.runcmd cmd, local_params, cliopts args = local_params.delete :args params = local_params global_opts = cliopts config = Rbcli::config raise Exception.new("Command #{cmd} has both an extern and action defined. Usage is limiated to one at a time.") if @commands[cmd].extern and @commands[cmd].action @commands[cmd].extern.execute params, args, global_opts, config unless @commands[cmd].extern.nil? @commands[cmd].action.call params, args, global_opts, config unless @commands[cmd].action.nil? end |
.usage(usage) ⇒ Object
47 |
# File 'lib/rbcli/engine/command.rb', line 47 def self.usage usage; @usage = usage end |
Instance Method Details
#action ⇒ Object
51 |
# File 'lib/rbcli/engine/command.rb', line 51 def action; self.class.instance_variable_get :@action end |
#description ⇒ Object
45 |
# File 'lib/rbcli/engine/command.rb', line 45 def description; self.class.instance_variable_get :@desc end |
#extern ⇒ Object
31 32 33 34 |
# File 'lib/rbcli/scriptwrapping/scriptwrapper.rb', line 31 def extern @extern ||= nil self.class.instance_variable_get :@extern end |
#paramlist ⇒ Object
65 |
# File 'lib/rbcli/engine/command.rb', line 65 def paramlist; self.class.instance_variable_get :@paramlist end |
#parseopts(*args) ⇒ Object
This method reads the parameters provided by the class and parses them from the CLI
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/rbcli/engine/command.rb', line 109 def parseopts *args params = paramlist command_name = self.class.name.split('::')[-1].downcase command_desc = description command_usage = usage optx = Trollop:: do data = Rbcli.configuration "\#{data[:description]}\nSelected Command:\n \#{command_name.ljust(21)}\#{command_desc}\n\nUsage:\n \#{data[:scriptname]} [options] \#{command_name} [parameters]\n\#{if command_usage then \"\\n\" + command_usage + \"\\n\" end}\nCommand-specific Parameters:\n EOS\n params.each do |name, opts|\n opt name, opts[:description], type: opts[:type], default: opts[:default], required: opts[:required], permitted: opts[:permitted], short: opts[:short]\n end if params.is_a? Hash\n end\n optx[:args] = ARGV\n optx\nend\n" |
#usage ⇒ Object
48 |
# File 'lib/rbcli/engine/command.rb', line 48 def usage; self.class.instance_variable_get :@usage end |