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
Stringspecifying 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, yielding lines of output if a block is given. -
#initialize(usage, description, procedure) ⇒ Command
constructor
Create a new
Commandwith the given metadata and aProcspecifying its behavior. -
#type_of_arg(index) ⇒ Object
Return a
Stringdescribing 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 four arguments: the DropboxClient, the State, an Array of command-line arguments, and a Proc to be called for output.
27 28 29 30 31 |
# File 'lib/droxi/commands.rb', line 27 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.
21 22 23 |
# File 'lib/droxi/commands.rb', line 21 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.
17 18 19 |
# File 'lib/droxi/commands.rb', line 17 def usage @usage end |
Instance Method Details
#exec(client, state, *args) ⇒ Object
Attempt to execute the Command, yielding lines of output if a block is given. Raises a UsageError if an invalid number of command-line arguments is given.
36 37 38 39 40 41 42 43 |
# File 'lib/droxi/commands.rb', line 36 def exec(client, state, *args) if num_args_ok?(args.length) block = proc { |line| yield line if block_given? } @procedure.yield(client, state, args, block) else raise UsageError.new(@usage) end 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.
48 49 50 51 52 53 54 55 56 |
# File 'lib/droxi/commands.rb', line 48 def type_of_arg(index) args = @usage.split.drop(1) if args.empty? nil else index = [index, args.length - 1].min args[index].tr('[].', '') end end |