Class: Ergo::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/ergo/cli.rb

Overview

Fire’s command line interface.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = nil) ⇒ void

Initialize new instance of Ergo::CLI. If ‘argv` is not provided than ARGV is uses.

Parameters:

  • argv (defaults to: nil)

    Command line argument. [Array<String>]



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ergo/cli.rb', line 19

def initialize(argv=nil)
  begin
    require 'dotopts'
  rescue LoadError
  end

  @argv = Array(argv || ARGV)

  @script = nil
  @watch  = nil
  @fresh  = false
end

Class Method Details

.fire!(argv = ARGV) ⇒ Object

Fire her up!



9
10
11
# File 'lib/ergo/cli.rb', line 9

def self.fire!(argv=ARGV)
  new(argv).fire!
end

Instance Method Details

#cli_parseObject

Parse command line arguments with just the prettiest little CLI parser there ever was.



78
79
80
81
82
83
84
85
86
87
# File 'lib/ergo/cli.rb', line 78

def cli_parse
  @command = nil

  cli @argv,
    "-R --rules"  => lambda{ @command = :list },
    "-a --auto"   => method(:watch=),
    "-f --fresh"  => method(:fresh!),
    "-s --script" => method(:script=),
    "   --debug"  => method(:debug!)
end

#debug!Boolean

Set debug flag to true.

Returns:



121
122
123
# File 'lib/ergo/cli.rb', line 121

def debug! 
  @debug = true
end

#debug?Boolean

Shall we make a fresh start of it, and remove all digests?

Returns:



114
115
116
# File 'lib/ergo/cli.rb', line 114

def debug?
  @debug
end

#ensure_options(args) ⇒ Object



90
91
92
93
94
95
# File 'lib/ergo/cli.rb', line 90

def ensure_options(args)
  erropts = args.select{ |a| a.start_with?('-') }
  unless erropts.empty?
    raise "unsupported options #{erropts.join(' ')}" 
  end
end

#fireObject

Fire her up!



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

def fire
  args = cli_parse

  ensure_options(args)

  #if args.first == 'init' && !runner.root?
  #  init_project(*args)
  #end

  case @command
  when :list
    print_rules(*args)
  else
    runner.run(*args)
  end
end

#fire!(argv = ARGV) ⇒ void

This method returns an undefined value.

Execute command.

Parameters:

  • command

    Which command to execute.



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

def fire!(argv=ARGV)
  $DEBUG = argv.include?('--debug') || $DEBUG
  return fire if $DEBUG
  begin
    fire
  rescue => err
    puts "ergo: error #{err}"
  end
end

#fresh!Boolean

Set fresh flag to true.

Returns:



107
108
109
# File 'lib/ergo/cli.rb', line 107

def fresh! 
  @fresh = true
end

#fresh?Boolean

Shall we make a fresh start of it, and remove all digests?

Returns:



100
101
102
# File 'lib/ergo/cli.rb', line 100

def fresh?
  @fresh
end

#init_project(*args) ⇒ Object

Returns nothing.



143
144
145
# File 'lib/ergo/cli.rb', line 143

def init_project(*args)
  FileUtils.mkdir_p('.ergo')
end

This method returns an undefined value.

Print out a list of availabe manual triggers.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/ergo/cli.rb', line 150

def print_rules(*names)
  names = nil if names.empty?

  list = []
  runner.rules.each do |rule|
    if Book === rule
      rule.rules.each do |r|
        next unless names.any?{ |n| r.mark?(n) } if names
        list << r.to_a
      end
    else         
      next unless names.any?{ |n| rule.mark?(n) } if names
      list << rule.to_a
    end
  end

  list.reject!{ |desc, marks, prv| desc.to_s == "" }

  puts "(#{runner.root})"

  i = 1
  list.each do |desc, marks, prv|
    if marks.empty?
      puts "%4d. %s%s" % [i, desc, prv ? '*' : '']
    else
      puts "%4d. %s%s (%s)" % [i, desc, prv ? '*' : '', marks.join(' ')]
    end
    i += 1
  end

  exit
end

#runnerRunner

Returns runner instance. [Runner]

Returns:



33
34
35
36
37
38
39
40
41
# File 'lib/ergo/cli.rb', line 33

def runner
  @runner ||= (
    Runner.new(
      :script => @script,
      :fresh  => @fresh,
      :watch  => @watch
    )  
  )
end

#script=(script) ⇒ Array

Use alternate ergo script.

Returns:



136
137
138
# File 'lib/ergo/cli.rb', line 136

def script=(script)
  @script = script.to_s
end

#watch=(seconds) ⇒ Fixnum[

Set the “watch” period –the rate at which autofiring of occurs.

Returns:

  • (Fixnum[)

    Fixnum[



129
130
131
# File 'lib/ergo/cli.rb', line 129

def watch=(seconds)
  @watch = seconds.to_i
end