module Thunder
Provides a simple, yet powerful ability to quickly and easily tie Ruby methods with command line actions.
The syntax is very similar to Thor, so switching over should be extremely easy
Constants
- VERSION
Version string for gemspec
Public Class Methods
@api private Automatically extends the singleton with {ClassMethods}
# File lib/thunder.rb, line 117 def self.included(base) base.send :extend, ClassMethods end
Public Instance Methods
Start the object as a command line program, processing the given arguments and using the provided options.
@param args [<String>] (ARGV) the command line arguments @param options [{Symbol => *}] ({}) the default options to use
# File lib/thunder.rb, line 15 def start(args = ARGV.dup, options = {}) command_spec = determine_command(args) return unless command_spec if command_spec[:name] == :help && command_spec[:default_help] return get_help(args, options) end parsed_options = process_options(args, command_spec) options.merge!(parsed_options) if parsed_options return command_spec[:subcommand].start(args, options) if command_spec[:subcommand] args << options if parsed_options if command_spec[:params] min = command_spec[:params].count { |param| param.first == :req} if args.size < min ARGV.insert((ARGV.size - args.size) - 1, "help") puts help_command(command_spec) return end max = if command_spec[:params].map(&:first).include?(:rest) nil else command_spec[:params].size end if !max.nil? && args.size > max ARGV.insert((ARGV.size - args.size) - 1, "help") puts help_command(command_spec) return end end return send command_spec[:name], *args end
Protected Instance Methods
Determine the command to use from the given arguments
@param args [<String>] the arguments to process @return [Hash,nil] the command specification for the given arguments,
or nil if there is no appropriate command
# File lib/thunder.rb, line 57 def determine_command(args) if args.empty? return self.class.thunder[:commands][self.class.thunder[:default_command]] end command_name = args.first.to_sym command_spec = self.class.thunder[:commands][command_name] if command_spec args.shift else command_spec = self.class.thunder[:commands][self.class.thunder[:default_command]] end return command_spec end
get help on the provided subjects
@param args [<String>] the arguments list @param options [Hash] any included options
# File lib/thunder.rb, line 90 def get_help(args, options) if args.empty? puts help_list(self.class.thunder[:commands]) else puts help_command(determine_command(args)) end end
Render detailed help on a specific command
@param command_spec [Hash] the command to render detailed help for @return [String] the rendered help
# File lib/thunder.rb, line 110 def help_command(command_spec) self.class.get_help_formatter.help_command(command_spec) end
Render a usage list of the given commands
@param commands [<Hash>] the commands to list @return [String] the rendered help
# File lib/thunder.rb, line 102 def help_list(commands) self.class.get_help_formatter.help_list(commands) end
Process command line options from the given argument list
@param args [<String>] the argument list to process @param command_spec [Hash] the command specification to use @return [{Symbol => *},nil] the options
# File lib/thunder.rb, line 76 def process_options(args, command_spec) return nil unless command_spec[:options] unless self.class.thunder[:options_processor] require File.expand_path("../thunder/options/optparse", __FILE__) self.class.thunder[:options_processor] = Thunder::OptParseAdapter end self.class.thunder[:options_processor].process_options(args, command_spec) end