lib/commandeer.rb in commandeer-0.0.1 vs lib/commandeer.rb in commandeer-0.1.0

- old
+ new

@@ -1,7 +1,7 @@ module Commandeer - VERSION = "0.0.1" + VERSION = "0.1.0" @commands = {} def self.commands @commands @@ -38,22 +38,23 @@ exit(1) end # No args. Let's show what we have registered if (args.size == 0) || (args[0] =~ /^-/) - puts "Usage: #{@script_name} [command options] or [command subcommand options]\n\n" - puts "Registered commands\n" + output = '' + output << "Usage: #{@script_name} [command options] or [command subcommand options]\n\n" @commands.each do |command, options| next if (command==:klass || command==:parser) - puts " #{command}" + output << "\t#{command}\t" unless options["subcommands"].nil? - puts " subcommands:" + output << "Subcommands:" options["subcommands"].each do |sub, opts| - puts " #{sub}" + output << " #{sub}" end end end + puts output exit end # Workflow # Check if current scope is a valid command @@ -62,32 +63,31 @@ # set the current command or return help @commands.has_key?(scope) ? command=@commands[scope] : (puts "Unknown command: #{scope}\n"; self.parse!("-h")) subcommands = command["subcommands"] - if subcommands + if subcommands && args.size == 0 output = '' output << "`#{scope}` has the following registered subcommands:\n" subcommands.keys.each {|x| output << "\t#{x}" } puts output - end if args.size == 0 + end - if command.has_key?(:parser) - output = '' - output << "\n`#{scope}` also takes options" - output << "\ntry running '#{@script_name} #{scope} --help'" - puts output - end if args.size == 0 + if command.has_key?(:parser) && args.size == 0 + # We should just pass this straight to the parser and be done + parser = command[:parser] + klass = command[:klass] + end if args.size > 0 - if subcommands.has_key?(args[0]) + if subcommands && subcommands.has_key?(args[0]) # Okay so the next arg is a registered subcommand. Let's shift args new_scope = args.shift warning =<<-EOF Warning! `#{new_scope}` is a registered subcommand for `#{scope}` but `#{scope}` also takes options. This can cause unexpected results if `#{scope}` has an option named `#{new_scope}` EOF - puts warning + puts warning if command.has_key?(:parser) parser = subcommands[new_scope][:parser] klass = subcommands[new_scope][:klass] else parser = command[:parser]