Class: GridCLI::BaseCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/gridcli/commands/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd, desc, default_opts = nil) ⇒ BaseCommand

Returns a new instance of BaseCommand.



8
9
10
11
12
13
14
15
16
17
# File 'lib/gridcli/commands/base.rb', line 8

def initialize(cmd, desc, default_opts = nil)
  @config = GridCLI.config
  @stats = GridCLI.stats
  @cmd = cmd
  @desc = desc
  @opts = default_opts || {}
  @optp = OptionParser.new { |opts|
    opts.on("-v", "--verbose", "Run verbosely") { |v| @opts[:verbose] = v }
  }
end

Instance Attribute Details

#cmdObject

Returns the value of attribute cmd.



6
7
8
# File 'lib/gridcli/commands/base.rb', line 6

def cmd
  @cmd
end

#descObject

Returns the value of attribute desc.



6
7
8
# File 'lib/gridcli/commands/base.rb', line 6

def desc
  @desc
end

Instance Method Details

#add_format_optionObject



27
28
29
30
31
32
# File 'lib/gridcli/commands/base.rb', line 27

def add_format_option
  msg = "Specify output format (json, cmdcolor, cmd, textline)"
  add_option("-o format", "--output-format format", msg) { |o|
    @opts[:output_format] = o
  }
end

#add_option(*args) ⇒ Object



19
20
21
# File 'lib/gridcli/commands/base.rb', line 19

def add_option(*args) 
  @optp.on(*args) { |u| yield u }
end

#error(msg) ⇒ Object



49
50
51
52
# File 'lib/gridcli/commands/base.rb', line 49

def error(msg)
  puts msg
  exit 1
end

#log(msg) ⇒ Object



75
76
77
78
# File 'lib/gridcli/commands/base.rb', line 75

def log(msg)
  return unless @opts[:verbose]
  puts "[debug]: #{msg}"
end

#output_formatObject



34
35
36
# File 'lib/gridcli/commands/base.rb', line 34

def output_format
  @opts[:output_format] || "cmdcolor"
end

#parse_dates(datestring) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/gridcli/commands/base.rb', line 54

def parse_dates(datestring)
  dates = [nil, nil]
  return dates if datestring.nil? or datestring.strip == ""

  # get start / end parts
  parts = datestring.downcase.split(' to ')
  parts << parts.first if parts.length == 1

  dates[0] = (parts[0].nil? or parts[0].strip == "") ? nil : Chronic.parse(parts[0], :context => :past)
  dates[1] = (parts.length < 2 or parts[1].strip == "") ? nil : Chronic.parse(parts[1], :context => :past)
  dates.map { |d| d.nil? ? nil : Date.parse(d.to_s) }
end

#parse_opts(args) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/gridcli/commands/base.rb', line 67

def parse_opts(args)
  begin
    @optp.parse(args)
  rescue 
    usage
  end
end

#pop_arg(args, default = nil) ⇒ Object



23
24
25
# File 'lib/gridcli/commands/base.rb', line 23

def pop_arg(args, default=nil)
  (args.length != 0 and not args.first.start_with?('-')) ? args.shift : default
end

#pprint(json) ⇒ Object



38
39
40
# File 'lib/gridcli/commands/base.rb', line 38

def pprint(json)
  puts PrettyPrinter.new(json, output_format)
end

#usage(cmd_opts = nil) ⇒ Object



42
43
44
45
46
47
# File 'lib/gridcli/commands/base.rb', line 42

def usage(cmd_opts=nil)
  cmd_opts ||= ""
  @optp.banner = "Usage: grid #{@cmd} #{cmd_opts} [options]"
  puts @optp
  exit
end