Class: Rbcli::Command

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rbcli/engine/command.rb', line 112

def self.descriptions indent_size, justification
	#descmap = @commands.map { |name, klass| [name, klass.description] }.to_h
	@commands.map do |name, cmdobj|
		desc = ''
		if Rbcli.configuration[:remote_execution] and cmdobj.remote_permitted?
			indent_size -= 3
			indent_size.times { desc << ' ' }
			desc << '*  '
		else
			indent_size.times { desc << ' ' }
		end
		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

.remote_permitted ⇒ Object



75
76
77
# File 'lib/rbcli/engine/command.rb', line 75

def self.remote_permitted
	@remote_permitted = true
end

.runcmd(cmd, local_params, cliopts) ⇒ Object

Run a given command

Raises:

  • (Exception)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rbcli/engine/command.rb', line 90

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} can only have one of `action`, `script`, or `extern` defined.") if (@commands[cmd].extern or @commands[cmd].script) and @commands[cmd].action

	if cliopts[:remote_exec]
		Rbcli::RemoteExec.new(@commands[cmd], cliopts[:remote_exec], cliopts[:identity], params, args, global_opts, config).run
		#remote_exec @commands[cmd], params, args, global_opts, config
		return
	end

	@commands[cmd].extern.execute params, args, global_opts, config unless @commands[cmd].extern.nil?
	@commands[cmd].script.execute params, args, global_opts, config unless @commands[cmd].script.nil?
	@commands[cmd].action.call params, args, global_opts, config unless @commands[cmd].action.nil?
end

.script(path: nil, envvars: nil) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/rbcli/scriptwrapping/scriptwrapper.rb', line 36

def self.script path: nil, envvars: nil
	if path == :default or path.nil?
		callerscript = caller_locations.first.absolute_path
		path = "#{File.dirname(callerscript)}/scripts/#{File.basename(callerscript, ".*")}.sh"
	end
	@script = Rbcli::Scriptwrapper.new path, envvars, nil, true
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



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/rbcli/engine/command.rb', line 131

def parseopts *args
	params = paramlist
	command_name = self.class.name.split('::')[-1].downcase
	command_desc = description
	command_usage = usage
	optx = Trollop::options do
		data = Rbcli.configuration
		banner <<-EOS
#{data[:description]}
Selected Command:
     #{command_name.ljust(21)}#{command_desc}

Usage:
     #{data[:scriptname]} [options] #{command_name} [parameters]
#{if command_usage then "\n" + command_usage + "\n" end}
Command-specific Parameters:
		EOS
		params.each do |name, opts|
			opt name, opts[:description], type: opts[:type], default: opts[:default], required: opts[:required], permitted: opts[:permitted], short: opts[:short]
		end if params.is_a? Hash
	end
	optx[:args] = ARGV
	optx
end

#remote_permitted? ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/rbcli/engine/command.rb', line 79

def remote_permitted?
	self.class.instance_variable_get :@remote_permitted
end

#script ⇒ Object



44
45
46
47
# File 'lib/rbcli/scriptwrapping/scriptwrapper.rb', line 44

def script
	@script ||= nil
	self.class.instance_variable_get :@script
end

#usage ⇒ Object



48
# File 'lib/rbcli/engine/command.rb', line 48

def      usage;                self.class.instance_variable_get :@usage end