Module: PointCLI

Extended by:
PointCLI
Included in:
PointCLI
Defined in:
lib/point_cli.rb,
lib/point_cli/command.rb

Defined Under Namespace

Classes: Command

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Instance Method Details

#command(command, options = {}, &block) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/point_cli.rb', line 48

def command(command, options = {}, &block)
  @commands = Hash.new if @commands.nil?
  @commands[command] = Hash.new
  @commands[command][:description] = @next_description
  @commands[command][:usage] = @next_usage
  @commands[command][:flags] = @next_flags
  @commands[command][:required_args] = (options[:required_args] || 0)
  @commands[command][:block] = Command.new(block)
  @next_usage, @next_description, @next_flags = nil, nil, nil
end

#commandsObject



59
60
61
# File 'lib/point_cli.rb', line 59

def commands
  @commands
end

#desc(value) ⇒ Object



63
64
65
# File 'lib/point_cli.rb', line 63

def desc(value)
  @next_description = value
end

#flags(key, value) ⇒ Object



71
72
73
74
# File 'lib/point_cli.rb', line 71

def flags(key, value)
  @next_flags = Hash.new if @next_flags.nil?
  @next_flags[key] = value
end

#load_commandsObject



76
77
78
79
80
# File 'lib/point_cli.rb', line 76

def load_commands
  Dir[File.join(File.dirname(__FILE__), 'commands', '*.rb')].each do |path|
    PointCLI.module_eval File.read(path), path
  end
end

#parse_options(args) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/point_cli.rb', line 82

def parse_options(args)
  idx = 0
  args.clone.inject({}) do |memo, arg|
    case arg
    when /^--(.+?)=(.*)/
      args.delete_at(idx)
      memo.merge($1.to_sym => $2)
    when /^--(.+)/
      args.delete_at(idx)
      memo.merge($1.to_sym => true)
    when "--"
      args.delete_at(idx)
      return memo
    else
      idx += 1
      memo
    end
  end
end

#run(command, args = []) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/point_cli.rb', line 27

def run(command, args = [])
  load_commands
  command = 'help' if command.nil?
  if @commands[command]
    @options = parse_options(args)
    if args.size < @commands[command][:required_args]
      puts "usage: #{@commands[command][:usage]}"
    else
      @commands[command][:block].call(*args)
    end
  else
    puts "Command not found. Check 'point help' for full information."
  end
rescue Point::Errors::AccessDenied
  puts "Access Denied. The username & API key stored for your account was invalid. Have you run 'point setup'?"
  Process.exit(1)
rescue Point::Error
  puts "An error occured with your point request."
  Process.exit(1)
end

#usage(value) ⇒ Object



67
68
69
# File 'lib/point_cli.rb', line 67

def usage(value)
  @next_usage = value
end