Class: Awshucks::Command
Overview
wraps up command parsing functions. Command class has class-level accessor for registering commands.
Direct Known Subclasses
BackupCommand, BackupsCommand, HelpCommand, ListCommand, NewConfigCommand, ResetMetadataCacheCommand, RestoreCommand
Class Attribute Summary collapse
-
.command ⇒ Object
Returns the value of attribute command.
-
.description ⇒ Object
Returns the value of attribute description.
-
.usage ⇒ Object
Returns the value of attribute usage.
Class Method Summary collapse
- .commands ⇒ Object
- .execute(args, config) ⇒ Object
- .inherited(klass) ⇒ Object
- .option_string ⇒ Object
- .parse_and_execute(args, config) ⇒ Object
Instance Method Summary collapse
-
#execute(args, config) ⇒ Object
instance methods ####.
- #option_parser ⇒ Object
Class Attribute Details
.command ⇒ Object
Returns the value of attribute command.
50 51 52 |
# File 'lib/awshucks/command.rb', line 50 def command @command end |
.description ⇒ Object
Returns the value of attribute description.
52 53 54 |
# File 'lib/awshucks/command.rb', line 52 def description @description end |
.usage ⇒ Object
Returns the value of attribute usage.
51 52 53 |
# File 'lib/awshucks/command.rb', line 51 def usage @usage end |
Class Method Details
.commands ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/awshucks/command.rb', line 34 def commands @commands ||= returning({}) do |commands| inherited_commands.each do |command| commands[command.command] = command end end end |
.execute(args, config) ⇒ Object
46 47 48 |
# File 'lib/awshucks/command.rb', line 46 def execute(args, config) new.execute(args, config) end |
.inherited(klass) ⇒ Object
20 21 22 23 |
# File 'lib/awshucks/command.rb', line 20 def inherited(klass) # save it for later, klass doesn't get fully defined until after this callback inherited_commands << klass end |
.option_string ⇒ Object
42 43 44 |
# File 'lib/awshucks/command.rb', line 42 def option_string new.option_parser.to_s # ehhh not a huge fan of this... end |
.parse_and_execute(args, config) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/awshucks/command.rb', line 25 def parse_and_execute(args, config) key = args.shift if key && commands[key] # be explicit about nil, no invalid command inheritance hijinks allowed commands[key].execute(args, config) else raise UnknownCommandError, key end end |
Instance Method Details
#execute(args, config) ⇒ Object
instance methods ####
66 67 68 |
# File 'lib/awshucks/command.rb', line 66 def execute(args, config) raise CommandError, "must implement the execute method!" end |
#option_parser ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/awshucks/command.rb', line 70 def option_parser @option_parser ||= OptionParser.new do |opts| opts. = self.class.usage opts.separator self.class.description opts.separator "" if respond_to?(:custom_options) opts.separator "Options:" (opts) else opts.separator "No options." end end end |