Class: Waw::Commands::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/waw/commands/command.rb

Direct Known Subclasses

CrawlCommand, ProfileCommand, StartCommand

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommand

Creates an empty command instance



14
15
16
17
# File 'lib/waw/commands/command.rb', line 14

def initialize
  @verbosity = 1
  @buffer = STDOUT
end

Instance Attribute Details

#traceObject

Show stack traces?



11
12
13
# File 'lib/waw/commands/command.rb', line 11

def trace
  @trace
end

#verbosityObject

The verbose level



8
9
10
# File 'lib/waw/commands/command.rb', line 8

def verbosity
  @verbosity
end

Instance Method Details

#__run(requester_file, arguments) ⇒ Object

Runs the sub-class defined command



117
118
119
# File 'lib/waw/commands/command.rb', line 117

def __run(requester_file, arguments)
  raise "Command._run should be overriden"
end

#add_options(opt) ⇒ Object

Contribute to options



103
104
# File 'lib/waw/commands/command.rb', line 103

def add_options(opt)
end

Returns the command banner



107
108
109
# File 'lib/waw/commands/command.rb', line 107

def banner
  raise "Command.banner should be overriden by subclasses"
end

#check_command_policyObject

Checks that the command may be safely executed!



112
113
114
# File 'lib/waw/commands/command.rb', line 112

def check_command_policy
  raise "Command.check_command_policy should be overriden"
end

#exit(msg = nil, show_options = true) ⇒ Object

Exits with a message, showing options if required



91
92
93
94
95
# File 'lib/waw/commands/command.rb', line 91

def exit(msg = nil, show_options=true)
  info msg if msg
  puts options if show_options
  Kernel.exit(-1)
end

#info(msg) ⇒ Object Also known as: error



97
98
99
# File 'lib/waw/commands/command.rb', line 97

def info(msg)
  @buffer << msg.gsub(/^[ \t]+/, '') << "\n"
end

#optionsObject

Parses commandline options provided as an array of Strings.



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/waw/commands/command.rb', line 20

def options
  @options  ||= OptionParser.new do |opt|
    opt.program_name = File.basename $0
    opt.version = Waw::VERSION
    opt.release = nil
    opt.summary_indent = ' ' * 4
    banner = self.banner
    opt.banner = banner.gsub(/^[ \t]+/, "")
    
    opt.separator nil
    opt.separator "Options:"
    
    add_options(opt)

    opt.on("--trace", "Display stack trace on error?") do |value|
      @trace = true
    end
    
    opt.on("--verbose", "Display extra info as we progress") do |value|
      @verbosity = 2
    end
    
    opt.on("--silent", "Be quiet silent") do |value|
      @verbosity = 0
    end
    
    # No argument, shows at tail.  This will print an options summary.
    # Try it and see!
    opt.on_tail("-h", "--help", "Show this message") do
      puts options
      exit
    end

    # Another typical switch to print the version.
    opt.on_tail("--version", "Show version") do
      puts opt.program_name << Waw::VERSION << " (c) University of Louvain, Bernard & Louis Lambeau"
      exit
    end
    
    opt.separator nil
  end
end

#run(requester_file, argv) ⇒ Object

Runs the command



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/waw/commands/command.rb', line 68

def run(requester_file, argv)
  @requester_file = requester_file
  check_command_policy
  __run(requester_file, options.parse!(argv))
rescue OptionParser::InvalidOption => ex
  exit ex.message
rescue SystemExit
rescue Exception => ex
  error <<-EOF
    A severe error occured. Please report this to the developers.
    Try --trace if the trace does not appear
  
    #{ex.class}: #{ex.message}
  EOF
  error ex.backtrace.join("\n") if trace
end

#shell_exec(what) ⇒ Object



85
86
87
88
# File 'lib/waw/commands/command.rb', line 85

def shell_exec(what)
  info "shell: #{what}" if verbose
  info `#{what}`
end

#verboseObject



63
64
65
# File 'lib/waw/commands/command.rb', line 63

def verbose
  verbosity < 0
end