Module: Xively::Command

Defined in:
lib/xively-cli/command.rb,
lib/xively-cli/command/base.rb

Defined Under Namespace

Classes: Base, CommandFailed, Feeds, Help, Subscribe, Version

Constant Summary collapse

BaseWithApp =
Base

Class Method Summary collapse

Class Method Details

.command_aliasesObject



18
19
20
# File 'lib/xively-cli/command.rb', line 18

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

.commandsObject



14
15
16
# File 'lib/xively-cli/command.rb', line 14

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

.current_commandObject



38
39
40
# File 'lib/xively-cli/command.rb', line 38

def self.current_command
  @current_command
end

.current_command=(new_current_command) ⇒ Object



42
43
44
# File 'lib/xively-cli/command.rb', line 42

def self.current_command=(new_current_command)
  @current_command = new_current_command
end

.display_warningsObject



79
80
81
82
83
# File 'lib/xively-cli/command.rb', line 79

def self.display_warnings
  unless warnings.empty?
    $stderr.puts(warnings.map {|warning| " !    #{warning}"}.join("\n"))
  end
end

.filesObject



22
23
24
# File 'lib/xively-cli/command.rb', line 22

def self.files
  @@files ||= Hash.new {|hash,key| hash[key] = File.readlines(key).map {|line| line.strip}}
end

.global_option(name, *args, &blk) ⇒ Object



85
86
87
# File 'lib/xively-cli/command.rb', line 85

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

.global_optionsObject



46
47
48
# File 'lib/xively-cli/command.rb', line 46

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

.invalid_argumentsObject



50
51
52
# File 'lib/xively-cli/command.rb', line 50

def self.invalid_arguments
  @invalid_arguments
end

.loadObject



8
9
10
11
12
# File 'lib/xively-cli/command.rb', line 8

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

.namespacesObject



26
27
28
# File 'lib/xively-cli/command.rb', line 26

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

.parse(cmd) ⇒ Object



163
164
165
# File 'lib/xively-cli/command.rb', line 163

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

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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/xively-cli/command.rb', line 92

def self.prepare_run(cmd, args=[])
  command = parse(cmd)

  unless command
    if %w( -v --version ).include?(cmd)
      command = parse('version')
    else
      $stderr.puts(["`#{cmd}` is not a xively command.", "See `xively help` for a list of available commands."].join("\n"))
      exit(1)
    end
  end

  @current_command = cmd

  opts = {}
  invalid_options = []

  parser = OptionParser.new do |parser|
    # overwrite OptionParsers Officious['version'] to avoid conflicts
    # see: https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L814
    parser.on("--version") do |value|
      invalid_options << "--version"
    end
    global_options.each do |global_option|
      parser.on(*global_option[:args]) do |value|
        global_option[:proc].call(value) if global_option[:proc]
        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

  if opts[:help]
    args.unshift cmd unless cmd =~ /^-.*/
    cmd = "help"
    command = parse(cmd)
  end

  args.concat(invalid_options)

  @current_args = args
  @current_options = opts
  @invalid_arguments = invalid_options

  [ command[:klass].new(args.dup, opts.dup), command[:method] ]
end

.register_command(command) ⇒ Object



30
31
32
# File 'lib/xively-cli/command.rb', line 30

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

.register_namespace(namespace) ⇒ Object



34
35
36
# File 'lib/xively-cli/command.rb', line 34

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

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



152
153
154
155
156
157
158
159
160
161
# File 'lib/xively-cli/command.rb', line 152

def self.run(cmd, arguments=[])
  object, method = prepare_run(cmd, arguments.dup)
  object.send(method)
rescue CommandFailed => e
  $stderr.puts e.message
rescue OptionParser::ParseError
  commands[cmd] ? run("help", [cmd]) : run("help")
ensure
  display_warnings
end

.shift_argumentObject



54
55
56
# File 'lib/xively-cli/command.rb', line 54

def self.shift_argument
  @invalid_arguments.shift rescue nil
end

.validate_arguments!Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/xively-cli/command.rb', line 58

def self.validate_arguments!
  unless invalid_arguments.empty?
    arguments = invalid_arguments.map {|arg| "\"#{arg}\""}
    if arguments.length == 1
      message = "Invalid argument: #{arguments.first}"
    elsif arguments.length > 1
      message = "Invalid arguments: "
      message << arguments[0...-1].join(", ")
      message << " and "
      message << arguments[-1]
    end
    $stderr.puts(message)
    run(current_command, ["--help"])
    exit(1)
  end
end

.warningsObject



75
76
77
# File 'lib/xively-cli/command.rb', line 75

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