lib/command_lion/app.rb in command_lion-1.0.2 vs lib/command_lion/app.rb in command_lion-1.0.3geff

- old
+ new

@@ -98,14 +98,15 @@ end end max_flag = flagz.map(&:length).max + 2 max_desc = app.commands.values.map(&:description).select{|d| d unless d.nil? }.map(&:length).max puts app.name + puts if app.version? - puts puts "VERSION" puts app.version + puts end if app.description? puts puts "DESCRIPTION" puts app.description @@ -139,21 +140,37 @@ end puts end end + # This run method is a pretty important method when using command lion typically. + # + # Under the hood, an application object is initialized. The block of code passed to + # this method is then used as the code that is ran in the context of a application + # object. So all of those methods will be available. + # def self.run(&block) + # Initialize an instance of an App object. app = new + # Evaluate the block of code within the context of that App object. app.instance_eval(&block) + # Parse the application logic out. + app.parse + # Sometimes a command-line application is run without being given any arguments. if ARGV.empty? - if app.help? - puts app.help + # Default to a help menu. + if cmd = app.commands[:help] + cmd.before.call if cmd.before? + cmd.action.call if cmd.action? + cmd.after.call if cmd.after? + # maybe exit? else - default_help(app) + # Use the default help menu for the application unless that's been + # explictly removed by the author for whatever reason. + default_help(app) unless app.default_help_menu_removed? end else - app.parse threadz = false app.commands.each do |_, cmd| next unless cmd.given? if cmd.threaded? threadz = [] unless threadz @@ -170,10 +187,26 @@ end threadz.map(&:join) if threadz end end + # Check if there has been an indexed help command. + def help? + return true if @commands[:help] + false + end + + # Explicitly remove the default help menu from the application. + def remove_default_help_menu + @remove_default_help_menu = true + end + + # Check if the default help menu for the application has been explicitly removed. + def default_help_menu_removed? + @remove_default_help_menu || false + end + # A tiny bit of rainbow magic is included. You can simple include # this option within your application and, if you have the `lolize` gem # installed, then rainbows will automagically be hooked to STDOUT to make your # application much prettier. # @@ -230,12 +263,12 @@ raise "No index or flags were given to use this command." end if cmd.options? cmd.options.each do |_, option| if option.flags? - @flags << option.flags.short if cmd.flags.short? - @flags << option.flags.long if cmd.flags.long? + @flags << option.flags.short if option.flags.short? + @flags << option.flags.long if option.flags.long? else # just use index @flags << option.index.to_s end @commands[option.index] = option #@commands << option @@ -243,10 +276,15 @@ end @commands[cmd.index] = cmd cmd end + def help(&block) + command :help, &block + end + + # Plugin a command that's probably been built outside of the application's run or build block. # This is helpful for sharing or reusing commands in applications. # @param command [Command] def plugin(command) command(command) @@ -266,11 +304,12 @@ # # @TODO Re-visit this. def parse @commands.each do |_, cmd| if cmd.flags? - next unless argv_index = ARGV.index(cmd.flags.short) or ARGV.index(cmd.flags.long) + # or for the || seems to not do what I want it to do... + next unless argv_index = ARGV.index(cmd.flags.short) || ARGV.index(cmd.flags.long) else next unless argv_index = ARGV.index(cmd.index.to_s) end cmd.given = true unless argv_index.nil? if cmd.type.nil? @@ -290,11 +329,11 @@ # Parse a given command with its # @TODO Re-visit this. def parse_cmd(cmd, flags) if cmd.flags? args = Raw.arguments_to(cmd.flags.short, flags) - if args.empty? + if args.nil? || args.empty? args = Raw.arguments_to(cmd.flags.long, flags) end else args = Raw.arguments_to(cmd.index.to_s, flags) end @@ -305,11 +344,17 @@ when :stdin_stream args = STDIN when :stdin_string args = STDIN.gets.strip when :stdin_strings - args = STDIN.gets.strip + args = [] + while arg = STDIN.gets + next if arg.nil? + arg = arg.strip + args << arg + end + args when :stdin_integer args = STDIN.gets.strip.to_i when :stdin_integers args = [] while arg = STDIN.gets @@ -322,11 +367,11 @@ end args when :stdin_bool args = STDIN.gets.strip.downcase == "true" when :single, :string - args.first + args = args.first when :strings, :multi if cmd.delimiter? if args.count > 1 args = args.first.split(cmd.delimiter) #args = args.first.join.split(cmd.delimiter).flatten.select { |arg| arg unless arg.empty? } @@ -336,32 +381,32 @@ args = args.map { |arg| arg.split(cmd.delimiter) }.flatten end end args when :integer - args.first.to_i + args = args.first.to_i when :integers if cmd.delimiter? if args.count > 1 args = args.join args = args.select { |arg| arg if arg.include?(cmd.delimiter) } args = args.map { |arg| arg.split(cmd.delimiter) }.flatten else args = args.map { |arg| arg.split(cmd.delimiter) }.flatten end end - args.map(&:to_i) + args = args.map(&:to_i) when :bool, :bools if cmd.delimiter? if args.count > 1 args = args.join args = args.select { |arg| arg if arg.include?(cmd.delimiter) } args = args.map { |arg| arg.split(cmd.delimiter) }.flatten else args = args.map { |arg| arg.split(cmd.delimiter) }.flatten end end - args.map { |arg| arg == "true" } + args = args.map { |arg| arg == "true" } end rescue => e# this is dangerous puts e nil end