Class: Sshez::Command
- Inherits:
-
Object
- Object
- Sshez::Command
- Defined in:
- lib/sshez/command.rb
Overview
Keeps track of the which command the user called
Constant Summary collapse
- PRINTER =
PrintingManager.instance
- ALL =
a list of all commands
{ 'add' => Command.new('add', (proc { |args| (args.length == 2) && (args[1].include?('@')) }), 'sshez add <alias> (role@host) [options]', (proc { |alias_name, role_host| [alias_name] + role_host.split('@') })), 'remove' => Command.new('remove', (proc { |args| args.length == 1 }), 'sshez remove <alias>'), 'rm' => Command.new('remove', (proc { |args| args.length == 1 }), 'sshez rm <alias>'), 'connect' => Command.new('connect', (proc { |args| args.length == 1 }), 'sshez connect <alias>'), 'reset' => Command.new('reset', (proc { |args| args.length == 0 }), 'sshez reset'), 'list' => Command.new('list', (proc { |args| args.empty? }), 'sshez list') }
Instance Attribute Summary collapse
-
#args ⇒ Object
Exposes the name and arguments.
-
#name ⇒ Object
readonly
Exposes the name and arguments.
Instance Method Summary collapse
-
#error ⇒ Object
returns the text that should appear for a user in case of invalid input for this command.
-
#initialize(name, validator, correct_format, args_processor = nil) ⇒ Command
constructor
no one should create a command except from this class!.
-
#valid?(args) ⇒ Boolean
validateds the args using the proc previously defined.
Constructor Details
#initialize(name, validator, correct_format, args_processor = nil) ⇒ Command
no one should create a command except from this class!
name: can only be one of these [add, remove, list] validator: a proc that returns true only if the input is valid for the command args: the args it self! correct_format: the way the user should run this command args_processor: (optional) a proc that will process the args before setting them
19 20 21 22 23 24 25 |
# File 'lib/sshez/command.rb', line 19 def initialize(name, validator, correct_format, args_processor = nil) @name = name @validator = validator @args = [] @correct_format = correct_format @args_processor = args_processor end |
Instance Attribute Details
#args ⇒ Object
Exposes the name and arguments
8 9 10 |
# File 'lib/sshez/command.rb', line 8 def args @args end |
#name ⇒ Object (readonly)
Exposes the name and arguments
8 9 10 |
# File 'lib/sshez/command.rb', line 8 def name @name end |
Instance Method Details
#error ⇒ Object
returns the text that should appear for a user in case of invalid input for this command
60 61 62 |
# File 'lib/sshez/command.rb', line 60 def error "Invalid input `#{args.join(',')}` for #{@name}.\nUsage: #{@correct_format}" end |
#valid?(args) ⇒ Boolean
validateds the args using the proc previously defined
53 54 55 |
# File 'lib/sshez/command.rb', line 53 def valid?(args) @validator.call(args) end |