Class: Dracula::Command
- Inherits:
-
Object
- Object
- Dracula::Command
- Defined in:
- lib/dracula/command.rb
Defined Under Namespace
Classes: Desc
Instance Attribute Summary collapse
-
#desc ⇒ Object
readonly
Returns the value of attribute desc.
-
#long_desc ⇒ Object
readonly
Returns the value of attribute long_desc.
-
#method_name ⇒ Object
readonly
Returns the value of attribute method_name.
-
#options ⇒ Object
(also: #flags)
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #arguments ⇒ Object
- #description ⇒ Object
- #full_name ⇒ Object
- #help ⇒ Object
-
#initialize(klass, method_name, desc, long_desc, options) ⇒ Command
constructor
A new instance of Command.
- #name ⇒ Object
- #prefix ⇒ Object
- #run(params) ⇒ Object
Constructor Details
#initialize(klass, method_name, desc, long_desc, options) ⇒ Command
Returns a new instance of Command.
13 14 15 16 17 18 19 |
# File 'lib/dracula/command.rb', line 13 def initialize(klass, method_name, desc, long_desc, ) @klass = klass @method_name = method_name @desc = desc @long_desc = long_desc @options = || [] end |
Instance Attribute Details
#desc ⇒ Object (readonly)
Returns the value of attribute desc.
7 8 9 |
# File 'lib/dracula/command.rb', line 7 def desc @desc end |
#long_desc ⇒ Object (readonly)
Returns the value of attribute long_desc.
9 10 11 |
# File 'lib/dracula/command.rb', line 9 def long_desc @long_desc end |
#method_name ⇒ Object (readonly)
Returns the value of attribute method_name.
6 7 8 |
# File 'lib/dracula/command.rb', line 6 def method_name @method_name end |
#options ⇒ Object (readonly) Also known as: flags
Returns the value of attribute options.
8 9 10 |
# File 'lib/dracula/command.rb', line 8 def @options end |
Instance Method Details
#arguments ⇒ Object
37 38 39 |
# File 'lib/dracula/command.rb', line 37 def arguments @klass.instance_method(@method_name).parameters.select { |p| p[0] == :req }.map { |p| p[1].to_s.upcase } end |
#description ⇒ Object
33 34 35 |
# File 'lib/dracula/command.rb', line 33 def description desc.description end |
#full_name ⇒ Object
25 26 27 |
# File 'lib/dracula/command.rb', line 25 def full_name "#{prefix}#{name}" end |
#help ⇒ Object
41 42 43 |
# File 'lib/dracula/command.rb', line 41 def help CommandHelp.new(self).show end |
#name ⇒ Object
29 30 31 |
# File 'lib/dracula/command.rb', line 29 def name desc.name end |
#prefix ⇒ Object
21 22 23 |
# File 'lib/dracula/command.rb', line 21 def prefix @klass.namespace.prefix end |
#run(params) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/dracula/command.rb', line 45 def run(params) args = params.take_while { |p| p[0] != "-" } if args.count < arguments.count puts Dracula::UI.error("Missing arguments") puts "" help exit(1) end if args.count > arguments.count puts Dracula::UI.error("Too many arguments") puts "" help exit(1) end parsed_flags = parse_flags(params.drop_while { |p| p[0] != "-" }) missing_flags = missing_required_flags(parsed_flags) unless missing_flags.empty? puts Dracula::UI.error("Missing required Parameter: #{missing_flags.first.}") puts "" help exit(1) end @klass.new(parsed_flags).public_send(method_name, *args) rescue OptionParser::MissingArgument => ex flag = flags.find { |f| "--#{f.name}" == ex.args.first } puts Dracula::UI.error("Missing flag parameter: #{flag.}") puts "" help exit(1) rescue OptionParser::InvalidOption => ex puts Dracula::UI.error("Unrecognized Parameter: #{ex.args.first}") puts "" help exit(1) end |