Class: Rulebow::CLI

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

Overview

Rulebow’s command line interface.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = ARGV) ⇒ void

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

Parameters:

  • argv (defaults to: ARGV)

    Command line argument. [Array<String>]



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

def initialize(argv=ARGV)
  #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/rulebow/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.



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rulebow/cli.rb', line 80

def cli_parse
  @command = nil

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

#debug!Boolean

Set debug flag to true.

Returns:



134
135
136
# File 'lib/rulebow/cli.rb', line 134

def debug! 
  $DEBUG = true
end

#debug?Boolean

Is debug mode on?

Returns:



127
128
129
# File 'lib/rulebow/cli.rb', line 127

def debug?
  $DEBUG
end

#ensure_options(args) ⇒ Object



103
104
105
106
107
108
# File 'lib/rulebow/cli.rb', line 103

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
75
76
# File 'lib/rulebow/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)
  when :help
    print_help(*args)
  else
    runner.run(args.first)
  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/rulebow/cli.rb', line 48

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

#fresh!Boolean

Set fresh flag to true.

Returns:



120
121
122
# File 'lib/rulebow/cli.rb', line 120

def fresh! 
  @fresh = true
end

#fresh?Boolean

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

Returns:



113
114
115
# File 'lib/rulebow/cli.rb', line 113

def fresh?
  @fresh
end

#init_project(*args) ⇒ void

This method returns an undefined value.

Initialize project for rulebow.



156
157
158
# File 'lib/rulebow/cli.rb', line 156

def init_project(*args)
  # anything to do?
end


93
94
95
96
97
98
99
100
# File 'lib/rulebow/cli.rb', line 93

def print_help(*names)
  puts "-R --rules            list ruleset descriptions"
  puts "-H --help             list these help options"
  puts "-a --auto [TIME]      autorun every so many seconds"
  puts "-f --fresh            clear digest for fresh run"
  puts "-s --script [SCRIPT]  use alternate script"
  puts "-D --debug            extra error information"
end

This method returns an undefined value.

Print out a list of availabe manual triggers.



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/rulebow/cli.rb', line 163

def print_rules(*names)
  names = nil if names.empty?
  puts "(#{runner.root})"
  runner.rulesets.each do |name, set|
    next unless names.member?(name.to_s) if names
    print "#{name}"
    print " (#{set.chain.join(' ')})" unless set.chain.empty?
    puts
    set.docs.each_with_index do |d, i|
      puts "  * #{d}"
    end
  end

  #exit
end

#runnerRunner

Returns runner instance. [Runner]

Returns:



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

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

#script=(script) ⇒ Array

Use alternate rulebow script.

Returns:



149
150
151
# File 'lib/rulebow/cli.rb', line 149

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[



142
143
144
# File 'lib/rulebow/cli.rb', line 142

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