Module: Mybot::Cli

Extended by:
Cli
Includes:
Fmt
Included in:
Cli
Defined in:
lib/mybot/cli.rb

Constant Summary

Constants included from Fmt

Fmt::WIDTH

Instance Method Summary collapse

Methods included from Fmt

#asterisks, #colored, #print_cmd, #print_cmd!, #print_progress, #print_stderr, #print_stdout, #spaces

Instance Method Details

#parse_args(args) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mybot/cli.rb', line 19

def parse_args(args)
  options = {}

  args.each_with_index do |arg, i|
    case arg # --foo=bar, -foo=bar
    when /\=/
      key, value = *arg.split("=")
      options[key.sub(/^-{1,2}/,"").to_sym] = value
    when /^-{1,2}no-(.+)/ # --no-foo
      options[$1.to_sym] = false
    when /^-{1,2}(.+)/ # --foo bar, --foo, -foo
      key = $1.to_sym
      value = true
      if args[i+1] && args[i+1] !~ /^-{1,2}/
        value = args.delete_at(i+1)
      end
      options[key] = value
    when Hash
      options.merge!(arg)
    end
  end

  options.each do |k, v|
    case v
      when /^true$/i   then options[k] = true
      when /^false$/i  then options[k] = false
      when /^\d+$/     then options[k] = v.to_i
      when /^[\d\.]+$/ then options[k] = v.to_f
      when /,/         then options[k] = v.split(",").map(&:strip)
    end
  end
end

#start(args) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/mybot/cli.rb', line 6

def start(args)
	cmd = args.shift
    options = parse_args(args)

    unless cmd
    	print_cmd! "error", "usage: bot <task> [<options>]", :red, :bold
    	abort
    end

    Base.load_recipes
    Base.run_task cmd, options
end