Module: HmxClient::Command

Extended by:
Helpers
Defined in:
lib/hmx/command.rb,
lib/hmx/command/fn.rb,
lib/hmx/command/get.rb,
lib/hmx/command/dump.rb,
lib/hmx/command/task.rb,
lib/hmx/command/type.rb,
lib/hmx/command/user.rb,
lib/hmx/command/view.rb,
lib/hmx/command/check.rb,
lib/hmx/command/clone.rb,
lib/hmx/command/query.rb,
lib/hmx/command/config.rb,
lib/hmx/command/session.rb,
lib/hmx/command/fountain.rb,
lib/hmx/command/bootstrap.rb,
lib/hmx/command/partition.rb,
lib/hmx/command/getContent.rb

Defined Under Namespace

Classes: Base, Bootstrap, Check, Clone, CommandFailed, Config, Dump, Evaluator, Fn, Fountain, Get, GetContent, Help, InvalidCommand, Partition, Query, ScriptHelper, Session, Task, Type, User, View

Class Method Summary collapse

Methods included from Helpers

display, display_row, display_tab, display_table, error, getFromUser, longest

Class Method Details

.command_aliasesObject



21
22
23
# File 'lib/hmx/command.rb', line 21

def self.command_aliases
  @@command_aliases ||= {}
end

.commandsObject



17
18
19
# File 'lib/hmx/command.rb', line 17

def self.commands
  @@command ||= {}
end

.current_argsObject



41
42
43
# File 'lib/hmx/command.rb', line 41

def self.current_args
  @current_args
end

.current_commandObject



37
38
39
# File 'lib/hmx/command.rb', line 37

def self.current_command
  @current_command
end

.current_optionsObject



45
46
47
# File 'lib/hmx/command.rb', line 45

def self.current_options
  @current_options
end

.debug?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/hmx/command.rb', line 105

def self.debug?
  @current_options.has_key?(:debug)
end

.extract_error(body, options = {}) ⇒ Object



134
135
136
# File 'lib/hmx/command.rb', line 134

def self.extract_error(body, options={})
  default_error = block_given ? yield : "Internal server error"
end

.fileInObject



109
110
111
# File 'lib/hmx/command.rb', line 109

def self.fileIn
  @current_options[:fileIn]
end

.fileOutObject



113
114
115
# File 'lib/hmx/command.rb', line 113

def self.fileOut
  @current_options[:fileOut]
end

.global_option(name, *args) ⇒ Object



53
54
55
# File 'lib/hmx/command.rb', line 53

def self.global_option(name, *args) 
  global_options << { :name => name, :args => args }
end

.global_optionsObject



49
50
51
# File 'lib/hmx/command.rb', line 49

def self.global_options
  @global_options ||= []
end

.loadObject



11
12
13
14
15
# File 'lib/hmx/command.rb', line 11

def self.load
  Dir[File.join(File.dirname(__FILE__), "command", "*.rb")].each do | file |
      require file
  end
end

.namespacesObject



25
26
27
# File 'lib/hmx/command.rb', line 25

def self.namespaces
  @@namespaces ||= {}
end

.parse(cmd) ⇒ Object



130
131
132
# File 'lib/hmx/command.rb', line 130

def self.parse(cmd)
  commands[cmd] || commands[command_aliases[cmd]]
end

.prepare_run(cmd, args = []) ⇒ Object

Raises:

  • (OptionParser::ParseError)


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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/hmx/command.rb', line 61

def self.prepare_run(cmd, args=[])
   command = parse(cmd)
   
   unless command
	 error " ! #{cmd} is not a hmx command. See 'hmx help'."
     return
   end

   @current_command = cmd
   opts = {}
   invalid_options = []
   
   parser = OptionParser.new do | parser |
      global_options.each do | global_option |
        parser.on(*global_option[:args]) do | value |
          opts[global_option[:name]] = value
        end
      end
      command[:options].each do | name, option |
        parser.on("-#{option[:short]}", "--#{option[:long]}", option[:desc]) do | value |
           opts[name.gsub("-","_").to_sym] = value
        end
      end
   end

   begin
     parser.order!(args) do |nonopt|
       invalid_options << nonopt
     end
   rescue OptionParser::InvalidOption => ex
     invalid_options << ex.args.first
     retry
   end
 
   raise OptionParser::ParseError if opts[:help]

   args.concat(invalid_options)

   @current_args = args
   @current_options = opts
 
   [ command[:klass].new(args.dup, opts.dup), command[:method] ]
end

.register_command(command) ⇒ Object



29
30
31
# File 'lib/hmx/command.rb', line 29

def self.register_command(command)
  commands[command[:command]] = command
end

.register_namespace(namespace) ⇒ Object



33
34
35
# File 'lib/hmx/command.rb', line 33

def self.register_namespace(namespace)
  namespaces[namespace[:name]] = namespace
end

.run(cmd, arguments = []) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/hmx/command.rb', line 117

def self.run(cmd, arguments=[])
  object, method = prepare_run(cmd, arguments.dup)
  object.send(method)
rescue InvalidCommand
  error "Unknown command. Run 'hmx help' for usage information."
rescue CommandFailed => e
  error e.message
rescue OptionParser::ParseError => ex
  commands[cmd] ? run("help", [cmd]) : run("help")
rescue Interrupt => e
  error "\n[cancelled]"
end