console_runner 0.1.17 → 0.1.18
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/command_line_parser.rb +20 -13
- data/lib/console_runner.rb +18 -20
- data/lib/console_runner/version.rb +1 -1
- data/lib/console_runner_error.rb +4 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e978670a0d9f1fa5854543027f3e737d33b1730a
|
4
|
+
data.tar.gz: e77dc5ea5ddc94e142ee6020b9ac35b9f6224d1c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e0bb91b568388bbbf2c481b30001d4ac9745c0617fe4c598fc2473971453aebfec3ad3a5c794e4747e66372faab9880019829023eddc936c3438340049e90396
|
7
|
+
data.tar.gz: 540f4dc2cf8eb1bafeb0f36efcecfbca7f9e2a19666300e618bd33b26754e1de35fb7b32014900d2c111ef108931b6501937a38cc889b4e507302898e25b2b97
|
data/lib/command_line_parser.rb
CHANGED
@@ -4,7 +4,8 @@ require 'colorize'
|
|
4
4
|
|
5
5
|
# Parses command line and configure #Trollop
|
6
6
|
class CommandLineParser
|
7
|
-
attr_reader :method, :
|
7
|
+
attr_reader :method, :initialize_method
|
8
|
+
@debug = false
|
8
9
|
|
9
10
|
# Generate tool help menu.
|
10
11
|
# IMPORTANT! Should be executed before ARGV.shift
|
@@ -18,16 +19,21 @@ class CommandLineParser
|
|
18
19
|
]
|
19
20
|
end.to_h
|
20
21
|
@parser = Trollop::Parser.new
|
22
|
+
@parser.opt(:debug, 'Run in debug mode.', type: :flag)
|
21
23
|
@parser.stop_on @sub_commands
|
22
|
-
@
|
24
|
+
@initialize_method = nil
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.debug?
|
28
|
+
@debug
|
23
29
|
end
|
24
30
|
|
25
31
|
def tool_banner
|
26
32
|
result = FileParser.select_runnable_tags(@file_parser.clazz).map(&:text).join("\n")
|
27
|
-
result += "\n\
|
33
|
+
result += "\n\nAvailable actions:\n"
|
28
34
|
result += @sub_commands_text.map do |c, text|
|
29
|
-
t = "\t
|
30
|
-
t += "\n\t\t
|
35
|
+
t = "\t- #{c}"
|
36
|
+
t += "\n\t\t#{text}" if text != ''
|
31
37
|
t
|
32
38
|
end.join("\n")
|
33
39
|
result
|
@@ -35,28 +41,29 @@ class CommandLineParser
|
|
35
41
|
|
36
42
|
def maybe_help(banner, action_name = nil)
|
37
43
|
action = action_name
|
38
|
-
scope
|
44
|
+
scope = ARGV
|
39
45
|
if action_name
|
40
46
|
action_index = ARGV.index(action)
|
41
|
-
scope
|
47
|
+
scope = ARGV[0..action_index] if action_index
|
42
48
|
end
|
43
|
-
|
49
|
+
@debug = scope.any? { |a| %w(-d --debug).include? a }
|
50
|
+
return unless scope.any? { |a| %w(-h --help).include? a }
|
44
51
|
@parser.banner("\n" + banner)
|
45
52
|
Trollop::with_standard_exception_handling(@parser) { raise Trollop::HelpNeeded }
|
46
53
|
end
|
47
54
|
|
48
55
|
def raise_on_action_absence(sub_commands)
|
49
|
-
return if ARGV.any? {|a| sub_commands.include? a }
|
56
|
+
return if ARGV.any? { |a| sub_commands.include? a }
|
50
57
|
raise ConsoleRunnerError, "You must provide one of available actions: #{sub_commands.join ', '}"
|
51
58
|
end
|
52
59
|
|
53
60
|
def run(action)
|
54
|
-
maybe_help(tool_banner, action ? action.name.to_s : nil
|
61
|
+
maybe_help(tool_banner, action ? action.name.to_s : nil)
|
55
62
|
raise ConsoleRunnerError, 'Cannot find any @runnable action' unless action
|
56
63
|
raise_on_action_absence @sub_commands
|
57
|
-
@
|
58
|
-
@method
|
59
|
-
[@
|
64
|
+
@initialize_method ||= MethodParser.new(@file_parser.initialize_method) if @file_parser.initialize_method
|
65
|
+
@method = MethodParser.new action
|
66
|
+
[@initialize_method, @method].each do |method|
|
60
67
|
next unless method
|
61
68
|
method.trollop_opts.each { |a| @parser.opt(*a) }
|
62
69
|
maybe_help(method.text, action.name.to_s)
|
data/lib/console_runner.rb
CHANGED
@@ -4,11 +4,12 @@ require 'console_runner/version'
|
|
4
4
|
|
5
5
|
# console_runner logic is here
|
6
6
|
module ConsoleRunner
|
7
|
+
SEPARATOR = '==================================='.freeze
|
7
8
|
begin
|
8
|
-
start_time
|
9
|
+
start_time = Time.now
|
9
10
|
success_status = true
|
10
|
-
puts "
|
11
|
-
|
11
|
+
puts "#{SEPARATOR}
|
12
|
+
Start Time: #{start_time}".blue
|
12
13
|
|
13
14
|
file_from_arg = ARGV.shift
|
14
15
|
raise ConsoleRunnerError, 'Specify file to be executed' unless file_from_arg
|
@@ -27,19 +28,16 @@ module ConsoleRunner
|
|
27
28
|
c_line_parser = CommandLineParser.new(file_parser)
|
28
29
|
c_line_parser.run(action)
|
29
30
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
puts "INIT: #{file_parser.initialize_method.name}"
|
35
|
-
puts 'INIT options:'
|
36
|
-
puts c_line_parser.init_method.cmd_opts.map { |k, v| " #{k} = #{v}" }.join("\n")
|
31
|
+
debug_message = "#{SEPARATOR}\n"
|
32
|
+
if c_line_parser.initialize_method
|
33
|
+
debug_message += ":initialize method:\n"
|
34
|
+
debug_message += c_line_parser.initialize_method.cmd_opts.map { |k, v| " #{k} = #{v}" }.join("\n")
|
37
35
|
end
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
puts
|
36
|
+
debug_message += "#{action.name} method:\n"
|
37
|
+
debug_message += c_line_parser.method.cmd_opts.map { |k, v| " #{k} = #{v}" }.join("\n")
|
38
|
+
debug_message += "Remaining arguments: #{ARGV.inspect}" if ARGV != []
|
39
|
+
debug_message += SEPARATOR
|
40
|
+
puts debug_message if CommandLineParser.debug?
|
43
41
|
|
44
42
|
|
45
43
|
require file_path
|
@@ -53,7 +51,7 @@ module ConsoleRunner
|
|
53
51
|
when :class
|
54
52
|
klass_obj.send(action.name, *method_params)
|
55
53
|
when :instance
|
56
|
-
init_method = c_line_parser.
|
54
|
+
init_method = c_line_parser.initialize_method
|
57
55
|
init_params = []
|
58
56
|
init_params = init_method.params_array if init_method
|
59
57
|
obj = klass_obj.new(*init_params)
|
@@ -63,13 +61,13 @@ module ConsoleRunner
|
|
63
61
|
end
|
64
62
|
rescue => e
|
65
63
|
success_status = false
|
66
|
-
|
64
|
+
raise e
|
67
65
|
ensure
|
68
66
|
finish_time = Time.now
|
69
|
-
status
|
67
|
+
status = success_status ? 'Success'.green : 'Error'.red
|
70
68
|
puts 'Execution status: '.blue + status
|
71
|
-
puts "
|
72
|
-
|
69
|
+
puts "#{SEPARATOR}
|
70
|
+
Finish Time: #{finish_time} (Duration: #{((finish_time - start_time) / 60).round(2) } minutes)
|
73
71
|
".blue
|
74
72
|
end
|
75
73
|
end
|
data/lib/console_runner_error.rb
CHANGED