Class: DenkoCLI::Parser

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/denko_cli/parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper

#error, #missing_files, #targets, #usage

Constructor Details

#initialize(options = {}) ⇒ Parser

Returns a new instance of Parser.



5
6
7
# File 'lib/denko_cli/parser.rb', line 5

def initialize(options={})
  @options = options
end

Class Method Details

.run(options = {}) ⇒ Object



9
10
11
# File 'lib/denko_cli/parser.rb', line 9

def self.run(options={})
  self.new(options).parse
end

Instance Method Details

#parseObject



13
14
15
16
17
18
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
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/denko_cli/parser.rb', line 13

def parse
  # Ensure we have arguments.
  args = @options[:args].dup
  error("No arguments given") if args.empty?

  # Ensure task is the first argument.
  @options[:task] = args.shift

  # If the task is a help command, just run it, don't return.
  usage if @options[:task].match /help|usage/
  targets if @options[:task].match /targets/

  # Ensure there's only one task
  error "Invalid task '#{@options[:task]}'" unless DenkoCLI::TASKS.include? @options[:task]

  # Parse the rest loosely.
  loop do
    case args[0]
      when 'serial'
        args.shift; set_sketch("serial")
      when 'ethernet'
        args.shift; set_sketch("ethernet")
      when 'wifi'
        args.shift; set_sketch("wifi")
      when '--baud'
        args.shift; @options[:baud] = args.shift
      when '--target'
        args.shift; set_target(args.shift)
      when '--mac'
        args.shift; @options[:mac] = args.shift
      when '--ip'
        args.shift; @options[:ip] = args.shift
      when '--ssid'
        args.shift; @options[:ssid] = args.shift
      when '--password'
        args.shift; @options[:password] = args.shift
      when '--port'
        args.shift; @options[:port] = args.shift
      when '--debug'
        args.shift; @options[:debug] = true
      when /^-/
        error "Invalid argument '#{args[0]}'"
      else break
    end
  end
  error "No valid sketch specified" if @options[:sketch_name].nil?

  @options
end

#set_sketch(name) ⇒ Object



63
64
65
66
# File 'lib/denko_cli/parser.rb', line 63

def set_sketch(name)
  error "More than one sketch specified" unless @options[:sketch_name].nil?
  @options[:sketch_name] = "denko_#{name}"
end

#set_target(target) ⇒ Object



68
69
70
71
72
# File 'lib/denko_cli/parser.rb', line 68

def set_target(target)
  targets = DenkoCLI::Generator::TARGETS.each_key.map{|a| a.to_s}
  error "Invalid target '#{target}'", :targets unless targets.include?(target)
  @options[:target] = target.to_sym
end