Class: Cowtech::Lib::Script
Overview
Class which implements a script to execute general tasks.
Instance Attribute Summary collapse
-
#console ⇒ Object
readonly
Console object.
-
#options_parser ⇒ Object
readonly
Options parser.
-
#shell ⇒ Object
readonly
Shell object.
Instance Method Summary collapse
-
#add_options ⇒ Object
Adds the command line options.
- #get_binding ⇒ Object
-
#initialize(args) ⇒ Script
constructor
Creates a new script.
-
#run ⇒ Object
Run the script.
-
#task(args) ⇒ Object
Execute a task, showing a message.
Constructor Details
#initialize(args) ⇒ Script
Creates a new script.
Arguments:
-
name: Script name
-
version: Script version
-
name: Script description
-
name: Script usage
-
name: Script message for help switch. Supported keys are
-
pre_usage: Message to print before the usage string
-
pre_options: Message to print before the options list
-
post_options: Message to print after the options list
-
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/cowtech-lib/script.rb', line 33 def initialize(args) @console = Console.new @shell = Shell.new(@console) @options_parser = OptionParser.new(args) self.() @options_parser << [ {name: "command-echo", short: "-z", long: "--command-echo", type: :bool, help: "Show executed commands."}, {name: "command-show", short: "-V", long: "--command-show", type: :bool, help: "Show executed commands' output."}, {name: "command-skip", short: "-Z", long: "--command-skip", type: :bool, help: "Don't really execut commands, only print them."} ] @options_parser.parse @console.show_commands = @options_parser["command-echo"] @console.show_outputs = @options_parser["command-show"] @console.skip_commands = @options_parser["command-skip"] self.run end |
Instance Attribute Details
#console ⇒ Object (readonly)
Console object
14 15 16 |
# File 'lib/cowtech-lib/script.rb', line 14 def console @console end |
#options_parser ⇒ Object (readonly)
Options parser
20 21 22 |
# File 'lib/cowtech-lib/script.rb', line 20 def @options_parser end |
#shell ⇒ Object (readonly)
Shell object
17 18 19 |
# File 'lib/cowtech-lib/script.rb', line 17 def shell @shell end |
Instance Method Details
#add_options ⇒ Object
Adds the command line options. MUST BE OVERRIDEN BY SUBCLASSES!
89 90 91 |
# File 'lib/cowtech-lib/script.rb', line 89 def self.console.fatal("Cowtech::Lib::Script#add_options must be overidden by subclasses.") end |
#get_binding ⇒ Object
93 94 95 |
# File 'lib/cowtech-lib/script.rb', line 93 def get_binding binding end |
#run ⇒ Object
Run the script. MUST BY OVERRIDEN BY SUBCLASSES!
83 84 85 |
# File 'lib/cowtech-lib/script.rb', line 83 def run self.console.fatal("Cowtech::Lib::Script#run must be overidden by subclasses.") end |
#task(args) ⇒ Object
Execute a task, showing a message.
Arguments:
-
msg: The message to show
-
show_message: If show task description
-
show_end: If show message exit status
-
go_up: If go up one line to show exit status
-
dots: If show dots after message
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/cowtech-lib/script.rb', line 62 def task(args) if args.fetch(:show_msg, true) then @console.write(begin: args[:msg], dots: args[:dots]) @console.indent_set(3) end # Run the block rv = yield rv = :ok if !rv.is_a?(Symbol) rv = [rv, true] if !rv.is_a?(Array) # Show the result @console.status(status: rv[0], fatal: false) if args.fetch(:show_result, true) @console.indent_set(-3) if args.fetch(:show_msg, true) exit(1) if rv[0] != :ok && rv[1] rv end |