Class: Commands::Command
- Inherits:
-
Object
- Object
- Commands::Command
- Defined in:
- lib/droxi/commands.rb
Overview
A client command. Contains metadata as well as execution procedure.
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
A complete description of the command, suitable for display to the end user.
-
#usage ⇒ Object
readonly
A
String
specifying the usage of the command in the style of a man page synopsis.
Instance Method Summary collapse
-
#exec(client, state, *args) ⇒ Object
Attempt to execute the
Command
. -
#initialize(usage, description, procedure) ⇒ Command
constructor
Create a new
Command
with the given metadata and aProc
specifying its behavior. -
#type_of_arg(index) ⇒ Object
Return a
String
describing the type of argument at the given index.
Constructor Details
#initialize(usage, description, procedure) ⇒ Command
Create a new Command
with the given metadata and a Proc
specifying its behavior. The Proc
will receive three arguments: the DropboxClient
, the State
, and an Array
of command-line arguments.
25 26 27 28 29 |
# File 'lib/droxi/commands.rb', line 25 def initialize(usage, description, procedure) @usage = usage @description = description.squeeze(' ') @procedure = procedure end |
Instance Attribute Details
#description ⇒ Object (readonly)
A complete description of the command, suitable for display to the end user.
20 21 22 |
# File 'lib/droxi/commands.rb', line 20 def description @description end |
#usage ⇒ Object (readonly)
A String
specifying the usage of the command in the style of a man page synopsis. Optional arguments are enclosed in brackets; varargs-style arguments are suffixed with an ellipsis.
16 17 18 |
# File 'lib/droxi/commands.rb', line 16 def usage @usage end |
Instance Method Details
#exec(client, state, *args) ⇒ Object
Attempt to execute the Command
. Raises a UsageError
if an invalid number of command-line arguments is given.
33 34 35 36 |
# File 'lib/droxi/commands.rb', line 33 def exec(client, state, *args) fail UsageError, @usage unless num_args_ok?(args.size) @procedure.yield(client, state, args) end |
#type_of_arg(index) ⇒ Object
Return a String
describing the type of argument at the given index. If the index is out of range, return the type of the final argument. If the Command
takes no arguments, return nil
.
41 42 43 44 45 46 |
# File 'lib/droxi/commands.rb', line 41 def type_of_arg(index) args = @usage.gsub(/\[-.+?\]/, '').split.drop(1) return nil if args.empty? index = [index, args.size - 1].min args[index].tr('[].', '') end |