Class: CommandLineParser
- Inherits:
-
Object
- Object
- CommandLineParser
- Defined in:
- lib/command_line_parser.rb
Overview
Parses command line and configure #Trollop
Instance Attribute Summary collapse
-
#initialize_method ⇒ Object
readonly
Returns the value of attribute initialize_method.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(file_parser) ⇒ CommandLineParser
constructor
Generate tool help menu.
- #maybe_help(banner, action_name = nil) ⇒ Object
- #raise_on_action_absence(sub_commands) ⇒ Object
- #run(action) ⇒ Object
- #tool_banner ⇒ Object
Constructor Details
#initialize(file_parser) ⇒ CommandLineParser
Generate tool help menu. IMPORTANT! Should be executed before ARGV.shift
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/command_line_parser.rb', line 12 def initialize(file_parser) @file_parser = file_parser @sub_commands = @file_parser.runnable_methods.map { |m| m.name.to_s } @sub_commands_text = @file_parser.runnable_methods.map do |m| [ m.name.to_s, FileParser.(m).map(&:text).join("\n") ] end.to_h @parser = Trollop::Parser.new @parser.opt(:debug, 'Run in debug mode.', type: :flag) @parser.stop_on @sub_commands @initialize_method = nil end |
Instance Attribute Details
#initialize_method ⇒ Object (readonly)
Returns the value of attribute initialize_method.
7 8 9 |
# File 'lib/command_line_parser.rb', line 7 def initialize_method @initialize_method end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
7 8 9 |
# File 'lib/command_line_parser.rb', line 7 def method @method end |
Class Method Details
.debug=(value) ⇒ Object
31 32 33 34 35 |
# File 'lib/command_line_parser.rb', line 31 def self.debug=(value) return if @debug ENV['CR_DEBUG'] = 'true' @debug = value end |
.debug? ⇒ Boolean
27 28 29 |
# File 'lib/command_line_parser.rb', line 27 def self.debug? @debug end |
Instance Method Details
#maybe_help(banner, action_name = nil) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/command_line_parser.rb', line 48 def maybe_help(, action_name = nil) action = action_name scope = ARGV if action_name action_index = ARGV.index(action) scope = ARGV[0..action_index] if action_index end return unless scope.any? { |a| %w(-h --help).include? a } @parser.("\n" + ) Trollop::with_standard_exception_handling(@parser) { raise Trollop::HelpNeeded } end |
#raise_on_action_absence(sub_commands) ⇒ Object
60 61 62 63 |
# File 'lib/command_line_parser.rb', line 60 def raise_on_action_absence(sub_commands) return if ARGV.any? { |a| sub_commands.include? a } raise RubyFireCLIError, "You must provide one of available actions: #{sub_commands.join ', '}" end |
#run(action) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/command_line_parser.rb', line 65 def run(action) maybe_help(, action ? action.name.to_s : nil) raise RubyFireCLIError, 'Cannot find any @runnable action' unless action raise_on_action_absence @sub_commands @initialize_method ||= MethodParser.new(@file_parser.initialize_method) if @file_parser.initialize_method @method = MethodParser.new action [@initialize_method, @method].each do |method| next unless method method.trollop_opts.each { |a| @parser.opt(*a) } maybe_help(method.text, action.name.to_s) cmd_opts = @parser.parse ARGV given_attrs = cmd_opts.keys.select { |k| k.to_s.include? '_given' }.map { |k| k.to_s.gsub('_given', '').to_sym } method.cmd_opts = cmd_opts.select { |k, _| given_attrs.include? k } method.default_values.each do |k, v| param_name = k.to_sym next if method..map(&:name).include?(param_name.to_s) method.cmd_opts[param_name] ||= v end method.required_parameters.each do |required_param| next if method. required_param next if method.cmd_opts[required_param.to_sym] raise RubyFireCLIError, "You must specify required parameter: #{required_param}" end ARGV.shift end end |
#tool_banner ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/command_line_parser.rb', line 37 def result = FileParser.(@file_parser.clazz).map(&:text).join("\n") result += "\n\nAvailable actions:\n" result += @sub_commands_text.map do |c, text| t = "\t- #{c}" t += "\n\t\t#{text}" if text != '' t end.join("\n") result end |