Class: Waw::Commands::Command
Direct Known Subclasses
Instance Attribute Summary collapse
-
#trace ⇒ Object
Show stack traces?.
-
#verbosity ⇒ Object
The verbose level.
Instance Method Summary collapse
-
#__run(requester_file, arguments) ⇒ Object
Runs the sub-class defined command.
-
#add_options(opt) ⇒ Object
Contribute to options.
-
#banner ⇒ Object
Returns the command banner.
-
#check_command_policy ⇒ Object
Checks that the command may be safely executed!.
-
#exit(msg = nil, show_options = true) ⇒ Object
Exits with a message, showing options if required.
- #info(msg) ⇒ Object (also: #error)
-
#initialize ⇒ Command
constructor
Creates an empty command instance.
-
#options ⇒ Object
Parses commandline options provided as an array of Strings.
-
#run(requester_file, argv) ⇒ Object
Runs the command.
- #shell_exec(what) ⇒ Object
- #verbose ⇒ Object
Constructor Details
#initialize ⇒ Command
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
#trace ⇒ Object
Show stack traces?
11 12 13 |
# File 'lib/waw/commands/command.rb', line 11 def trace @trace end |
#verbosity ⇒ Object
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 (opt) end |
#banner ⇒ Object
Returns the command banner
107 108 109 |
# File 'lib/waw/commands/command.rb', line 107 def raise "Command.banner should be overriden by subclasses" end |
#check_command_policy ⇒ Object
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, =true) info msg if msg puts if 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 |
#options ⇒ Object
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 ||= OptionParser.new do |opt| opt.program_name = File.basename $0 opt.version = Waw::VERSION opt.release = nil opt.summary_indent = ' ' * 4 = self. opt. = .gsub(/^[ \t]+/, "") opt.separator nil opt.separator "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 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, .parse!(argv)) rescue OptionParser::InvalidOption => ex exit ex. 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.} 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 |
#verbose ⇒ Object
63 64 65 |
# File 'lib/waw/commands/command.rb', line 63 def verbose verbosity < 0 end |