Module: Befog::Commands::Mixins::Command
- Defined in:
- lib/befog/commands/mixins/command.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #command ⇒ Object
- #error(message) ⇒ Object
- #initialize(_options) ⇒ Object
- #log(message) ⇒ Object
-
#process_options(_options) ⇒ Object
TODO: Support multi-line descriptions?.
- #usage ⇒ Object
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
33 34 35 |
# File 'lib/befog/commands/mixins/command.rb', line 33 def @options end |
Class Method Details
.included(target) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/befog/commands/mixins/command.rb', line 8 def self.included(target) target.module_eval do class << self def command(descriptor=nil) descriptor ? (@command = OpenStruct.new(descriptor)) : @command end def option(name,descriptor) << [name,descriptor] end def ; @options||=[] ; end def run() Befog.show_version if [:version] or [:v] new().run end end end end |
Instance Method Details
#command ⇒ Object
41 42 43 |
# File 'lib/befog/commands/mixins/command.rb', line 41 def command self.class.command end |
#error(message) ⇒ Object
77 78 79 |
# File 'lib/befog/commands/mixins/command.rb', line 77 def error() raise CLI::Error.new() end |
#initialize(_options) ⇒ Object
35 36 37 38 39 |
# File 'lib/befog/commands/mixins/command.rb', line 35 def initialize() safely do @options = () end end |
#log(message) ⇒ Object
81 82 83 |
# File 'lib/befog/commands/mixins/command.rb', line 81 def log() $stdout.puts() end |
#process_options(_options) ⇒ Object
TODO: Support multi-line descriptions?
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/befog/commands/mixins/command.rb', line 46 def () if (command.default_to_help and .empty?) or [:help] usage exit(0) end self.class..each do |name,descriptor| short, required, default, type = descriptor.values_at(:short,:required,:default,:type) [name] ||= ([short]||default) .delete(short) if required and not [name] error("Missing required option --#{name}") end end # TODO: add type conversion return end |
#usage ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/befog/commands/mixins/command.rb', line 63 def usage $stderr.puts command.usage usage = [] self.class..each do |name,descriptor| short, required, default, description = descriptor.values_at(:short,:required,:default,:description) required = (required ? "(required) " : "") default = (default ? "(default: #{default}) " : "") usage << "\t%-3s %-20s\t%-40s" % ["-#{short},", "--#{name} #{name.to_s.upcase}", "#{description} #{required}#{default}"] end $stderr.puts *(usage.sort) end |