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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 324b6b2410dcad3c02f00d4cae6bfd73c353c43b
4
- data.tar.gz: b8f40d069aec62595cc75608bbaa2f1b59f8e1d9
3
+ metadata.gz: e978670a0d9f1fa5854543027f3e737d33b1730a
4
+ data.tar.gz: e77dc5ea5ddc94e142ee6020b9ac35b9f6224d1c
5
5
  SHA512:
6
- metadata.gz: 24cd3d1bd2049ae0c9417ffb3f495611bcb39d912f7f289cbfd01b93dbca5f3feb049bea137bf235c66bb2847469e91ef5c5922fbe2f959f129bf4ba9be9455e
7
- data.tar.gz: 2ce249177a0213d3cf07ef1419bda487bc729551c40e77dfaa473cef7a428745501d011129dba9656f9959e1937d365a12ef60269f764e70e5d0e3b52b378d2e
6
+ metadata.gz: e0bb91b568388bbbf2c481b30001d4ac9745c0617fe4c598fc2473971453aebfec3ad3a5c794e4747e66372faab9880019829023eddc936c3438340049e90396
7
+ data.tar.gz: 540f4dc2cf8eb1bafeb0f36efcecfbca7f9e2a19666300e618bd33b26754e1de35fb7b32014900d2c111ef108931b6501937a38cc889b4e507302898e25b2b97
@@ -4,7 +4,8 @@ require 'colorize'
4
4
 
5
5
  # Parses command line and configure #Trollop
6
6
  class CommandLineParser
7
- attr_reader :method, :init_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
- @init_method = nil
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\n\tAvailable actions:\n"
33
+ result += "\n\nAvailable actions:\n"
28
34
  result += @sub_commands_text.map do |c, text|
29
- t = "\t\t- #{c}"
30
- t += "\n\t\t\t#{text}" if text != ''
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 = ARGV
44
+ scope = ARGV
39
45
  if action_name
40
46
  action_index = ARGV.index(action)
41
- scope = ARGV[0..action_index] if action_index
47
+ scope = ARGV[0..action_index] if action_index
42
48
  end
43
- return unless scope.any?{|a| %w(-h --help).include? a }
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
- @init_method ||= MethodParser.new(@file_parser.initialize_method) if @file_parser.initialize_method
58
- @method = MethodParser.new action
59
- [@init_method, @method].each do |method|
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)
@@ -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 = Time.now
9
+ start_time = Time.now
9
10
  success_status = true
10
- puts "===================================
11
- Start Time: #{start_time}".blue
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
- puts '======================================================='
32
- puts 'Global options:'
33
- if file_parser.initialize_method
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
- puts "Subcommand: #{action.name}"
39
- puts 'Subcommand options:'
40
- puts c_line_parser.method.cmd_opts.map { |k, v| " #{k} = #{v}" }.join("\n")
41
- puts "Remaining arguments: #{ARGV.inspect}" if ARGV != []
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.init_method
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
- raise e
64
+ raise e
67
65
  ensure
68
66
  finish_time = Time.now
69
- status = success_status ? 'Success'.green : 'Error'.red
67
+ status = success_status ? 'Success'.green : 'Error'.red
70
68
  puts 'Execution status: '.blue + status
71
- puts "===================================
72
- Finish Time: #{finish_time} (Duration: #{((finish_time - start_time) / 60).round(2) } minutes)
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
@@ -1,3 +1,3 @@
1
1
  module ConsoleRunner
2
- VERSION = '0.1.17'
2
+ VERSION = '0.1.18'
3
3
  end
@@ -1,8 +1,10 @@
1
1
  class ConsoleRunnerError < StandardError
2
2
 
3
- unless ENV['DEBUG'].to_s == 'true'
4
- def backtrace
3
+ def backtrace
4
+ if CommandLineParser.debug?
5
5
  @object
6
+ else
7
+ super
6
8
  end
7
9
  end
8
10
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: console_runner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.17
4
+ version: 0.1.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yury Karpovich