Module: Rbcli::Parser
- Defined in:
- lib/rbcli/components/parser/parser.rb
Class Method Summary collapse
Class Method Details
.add_opt(*args) ⇒ Object
10 11 12 13 14 |
# File 'lib/rbcli/components/parser/parser.rb', line 10 def self.add_opt * args Rbcli.log.debug "Set top-level opt argument: #{args}", "OPTS" Rbcli::Warehouse.set(:opts, [], :unparsedopts) if Rbcli::Warehouse.get(:opts, :unparsedopts).nil? Rbcli::Warehouse.get(:opts, :unparsedopts) << args end |
.parse ⇒ Object
16 17 18 19 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 62 63 64 65 66 67 68 |
# File 'lib/rbcli/components/parser/parser.rb', line 16 def self.parse appinfo = Rbcli::Warehouse.get(nil, :appinfo) || {} = "#{appinfo[:appname] || EXECUTABLE}" + (appinfo[:version].nil? ? "\n" : " version: #{appinfo[:version]}\n") unless appinfo[:copyright_year].nil? && appinfo[:author].nil? += "Copyright (C) #{appinfo[:copyright_year]} " unless appinfo[:copyright_year].nil? += "by #{appinfo[:author]}" if appinfo[:author].is_a? String += "by #{appinfo[:author].join(', ')}" if appinfo[:author].is_a? Array += " <#{appinfo[:email]}>" unless appinfo[:author].nil? || appinfo[:email].nil? += "\n" end unless appinfo[:compatibility].nil? || appinfo[:compatibility].empty? += "Compatiblity: " if appinfo[:compatibility].length == 2 += appinfo[:compatibility].join(' and ') + "\n" else += appinfo[:compatibility][0..-2].join(', ') + ', and ' + appinfo[:compatibility][-1] + "\n" end end += "License: #{appinfo[:license]}\n" unless appinfo[:license].nil? += "\n" += appinfo[:helptext].chomp + "\n\n" unless appinfo[:helptext].nil? += "Usage:\n #{EXECUTABLE} [options] command [parameters]\n\n" non_default_commands = Rbcli::Warehouse.get(:commands).select { |_name, cmd| !cmd.default? } if non_default_commands.count > 0 += "For more information on individual commands, run `#{EXECUTABLE} <command> -h`\n\n" += "Commands:\n" leftcol_width = non_default_commands.keys.map(&:length).max + 6 += non_default_commands.map { |name, cmd| " " + name + (" " * (leftcol_width - name.length)) + (cmd.description || '') }.join("\n") end @parser.synopsis (Rbcli::Warehouse.get(:opts, :unparsedopts) || []).each { |args| @parser.opt *args } @parser.stop_on(non_default_commands.select { |_name, cmd| !cmd.default? }.keys) begin opts = @parser.parse Rbcli::Warehouse.set(:opts, opts, :parsedopts) Rbcli.log.debug "Found options: #{opts}", "OPTS" rescue Optimist::CommandlineError => e Rbcli.log.fatal e. Rbcli.log.fatal "Run `#{EXECUTABLE} -h` for more information" Rbcli::exit 1 rescue Optimist::HelpNeeded stream = StringIO.new @parser.educate(stream) Rbcli.log.info stream.string, "OPTS" Rbcli::exit 0 rescue Optimist::VersionNeeded Rbcli.log.info .lines.first.chomp, "OPTS" Rbcli::exit 0 end end |
.parse_command ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/rbcli/components/parser/parser.rb', line 70 def self.parse_command cmd = { name: nil, command: nil, params: {}, args: [] } cmd[:name] = !ARGV.first.nil? && !ARGV.first.start_with?('-') ? ARGV.shift.downcase : nil default_command = Rbcli::Warehouse.get(:commands).values.select { |c| c.default? }.first if cmd[:name].nil? && default_command.nil? # CLI: Not given, Default: Not Set -- Educate Rbcli.log.debug "Command not given, and no default command set. Displaying help.", "OPTS" stream = StringIO.new @parser.educate(stream) Rbcli.log.info stream.string, "OPTS" Rbcli::exit 0 elsif cmd[:name].nil? # CLI: Not given, Default: Set -- Use Default cmd[:command] = default_command Rbcli.log.debug "Using default command: '#{cmd[:command].name}'", "OPTS" elsif Rbcli::Warehouse.get(:commands).key?(cmd[:name]) # CLI: Given, Default: Not Set, Command: Found -- Use Command cmd[:command] = Rbcli::Warehouse.get(:commands)[cmd[:name]] Rbcli.log.debug "Using command: '#{cmd[:command].name}'", "OPTS" elsif !default_command.nil? # CLI: Given, Default: Set, Command: Not Found -- Pass it as an argument cmd[:args] << cmd[:name] while !ARGV.first.nil? && !ARGV.first.start_with?('-') cmd[:args] << ARGV.shift end cmd[:command] = default_command Rbcli.log.debug "Using default command: '#{cmd[:command].name}' with args: #{cmd[:args]}", "OPTS" else # CLI: Given, Default: Not Set, Command: Not Found -- Fail Rbcli.log.fatal "Unknown command: '#{cmd[:name]}'", "OPTS" Rbcli.log.fatal "Run `#{EXECUTABLE} -h` for more information", "OPTS" Rbcli::exit 1 end Rbcli::Warehouse.set(:command, cmd[:command], :parsedopts) parser = Optimist::Parser.new # We set the educate text appinfo = Rbcli::Warehouse.get(nil, :appinfo) || {} = "#{appinfo[:appname] || EXECUTABLE}" + (appinfo[:version].nil? ? "\n" : " version: #{appinfo[:version]}\n") += "\n" += "Command: #{cmd[:command].name}\n\n" += cmd[:command].helptext.chomp + "\n\n" unless cmd[:command].helptext.nil? += "Usage:\n #{EXECUTABLE} #{cmd[:command].name} #{cmd[:command].usage.chomp}" unless cmd[:command].usage.nil? parser.synopsis() # Then we add the options from the command cmd[:command].params.each { |param| parser.opt *param } begin cmd[:params] = parser.parse Rbcli::Warehouse.set(:params, cmd[:params], :parsedopts) Rbcli.log.debug "Found parameters for command '#{cmd[:command].name}': #{cmd[:params]}", "OPTS" rescue Optimist::CommandlineError => e Rbcli.log.fatal e. Rbcli.log.fatal "Run `#{EXECUTABLE} #{cmd[:command].name} -h` for more information", "OPTS" Rbcli::exit 1 rescue Optimist::HelpNeeded stream = StringIO.new parser.educate(stream) Rbcli.log.info stream.string, "OPTS" Rbcli::exit 0 end cmd[:args].concat(ARGV) Rbcli::Warehouse.set(:args, cmd[:args], :parsedopts) ARGV.clear end |
.prompt_for_missing ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/rbcli/components/parser/parser.rb', line 139 def self.prompt_for_missing Rbcli::Warehouse.get(:command, :parsedopts).params .select { |args| !args[2].nil? && !args[2].empty? && args[2][:prompt].is_a?(String) } .reject { |args| Rbcli::Warehouse.get(:params, :parsedopts).key?((args[0].to_s + '_given').to_sym) } .map { |args| { name: args[0].to_sym, prompt: args[2][:prompt], default: args[2][:default], type: args[2][:type].nil? ? nil : args[2][:type].to_s.chomp('s').to_sym } } .each do |opt| prompt_string = opt[:prompt].strip param_value = Rbcli::Warehouse.get(:params, :parsedopts)[opt[:name]] if param_value.is_a?(TrueClass) || param_value.is_a?(FalseClass) prompt_string += ' (' + (param_value ? 'Y' : 'y') + '/' + (param_value ? 'n' : 'N') + ')' opt[:default] = param_value if opt[:default].nil? elsif !opt[:default].nil? prompt_string != " (default: #{opt[:default]})" end prompt_string += ':' unless %w(. ? ! :).include?(opt[:prompt][-1]) prompt_string += ' ' answer = nil while answer.nil? Rbcli.log.info prompt_string, "OPTS" answer = gets.chomp if [nil, :flag, :bool, :boolean, :trueclass, :falseclass].include?(opt[:type]) answer = { 'y' => true, 'n' => false, '' => opt[:default] }[(answer[0] || '').downcase] next end if answer == '' Rbcli.log.debug "Using default value of #{opt[:default]} for --#{opt[:name].to_s}", "OPTS" answer = opt[:default] end begin parser = Optimist::Parser.new parser.opt(opt[:name], '', type: opt[:type]) newopts = parser.parse ["--#{opt[:name].to_s}", answer] rescue Optimist::CommandlineError, Optimist::HelpNeeded, Optimist::VersionNeeded Rbcli.log.debug "Invalid answer for type #{opt[:type]}", "OPTS" answer = nil else answer = newopts[opt[:name]] end end Rbcli::Warehouse.get(:params, :parsedopts)[opt[:name]] = answer end end |