lib/gli.rb in gli-1.5.1 vs lib/gli.rb in gli-1.6.0

- old
+ new

@@ -27,10 +27,11 @@ @@version = nil @@stderr = $stderr @@program_desc = nil @@skips_pre = false @@skips_post = false + @@default_command = :help # Override the device of stderr; exposed only for testing def error_device=(e) #:nodoc: @@stderr = e end @@ -42,27 +43,39 @@ commands.clear @@version = nil @@config_file = nil @@use_openstruct = false @@prog_desc = nil + @@default_command = :help clear_nexts desc 'Show this message' switch :help end + # Sets a default command to run when none is specified on the command line. Note that + # if you use this, you won't be able to pass arguments, flags, or switches + # to the command when run in default mode. All flags and switches are treated + # as global, and any argument will be interpretted as the command name and likely + # fail. + # + # +command+:: Command to run as default + def default_command(command) + @@default_command = command.to_sym + end + # Describe the next switch, flag, or command. This should be a # short, one-line description # # +description+:: A String of the short descripiton of the switch, flag, or command following def desc(description); @@next_desc = description; end # Describe the overall application/programm. This should be a one-sentence summary # of what your program does that will appear in the help output. # # +description+:: A String of the short description of your program's purpose - def program_desc(description=nil) + def program_desc(description=nil) if description @@program_desc = description end @@program_desc end @@ -138,11 +151,11 @@ switch = Switch.new(names,@@next_desc,@@next_long_desc) switches[switch.name] = switch clear_nexts end - # Sets that this app uses a config file as well as the name of the config file. + # Sets that this app uses a config file as well as the name of the config file. # # +filename+:: A String representing the path to the file to use for the config file. If it's an absolute # path, this is treated as the path to the file. If it's *not*, it's treated as relative to the user's home # directory as produced by <code>File.expand_path('~')</code>. def config_file(filename) @@ -185,11 +198,11 @@ end # Define a block to run if an error occurs. # The block will receive any Exception that was caught. # It should evaluate to false to avoid the built-in error handling (which basically just - # prints out a message). GLI uses a variety of exceptions that you can use to find out what + # prints out a message). GLI uses a variety of exceptions that you can use to find out what # errors might've occurred during command-line parsing: # * GLI::CustomExit # * GLI::UnknownCommandArgument # * GLI::UnknownGlobalArgument # * GLI::UnknownCommand @@ -198,11 +211,11 @@ @@error_block = a_proc end # Indicate the version of your application # - # +version+:: String containing the version of your application. + # +version+:: String containing the version of your application. def version(version) @@version = version end # Call this with +true+ will cause the +global_options+ and @@ -213,11 +226,11 @@ # +use_openstruct+:: a Boolean indicating if we should use OpenStruct instead of Hashes def use_openstruct(use_openstruct) @@use_openstruct = use_openstruct end - # Runs whatever command is needed based on the arguments. + # Runs whatever command is needed based on the arguments. # # +args+:: the command line ARGV array # # Returns a number that would be a reasonable exit code def run(args) #:nodoc: @@ -231,11 +244,11 @@ global_options,command,options,arguments = parse_options(args) copy_options_to_aliased_versions(global_options,command,options) global_options = convert_to_openstruct?(global_options) options = convert_to_openstruct?(options) if proceed?(global_options,command,options,arguments) - command = commands[:help] if !command + command = commands[@@default_command] if !command command.execute(global_options,options,arguments) if !command.skips_post && @@post_block @@post_block.call(global_options,command,options,arguments) end end @@ -256,12 +269,12 @@ # True if we should proceed with executing the command; this calls # the pre block if it's defined def proceed?(global_options,command,options,arguments) #:nodoc: if command && command.skips_pre true - elsif @@pre_block - @@pre_block.call(global_options,command,options,arguments) + elsif @@pre_block + @@pre_block.call(global_options,command,options,arguments) else true end end @@ -288,11 +301,11 @@ msg += ". Use '#{program_name} help' for a list of global options" end msg end - # Simpler means of exiting with a custom exit code. This will + # Simpler means of exiting with a custom exit code. This will # raise a CustomExit with the given message and exit code, which will ultimatley # cause your application to exit with the given exit_code as its exit status def exit_now!(message,exit_code) raise CustomExit.new(message,exit_code) end @@ -341,11 +354,11 @@ end end # Returns an array of four values: # * global options (as a Hash) - # * Command + # * Command # * command options (as a Hash) # * arguments (as an Array) def parse_options(args) # :nodoc: global_options,command,options,arguments = parse_options_helper(args.clone,Hash.new,nil,Hash.new,Array.new) flags.each { |name,flag| global_options[name] = flag.default_value if !global_options[name] } @@ -440,11 +453,11 @@ end try_me = args[0..non_flag_i] rest = args[(non_flag_i+1)..args.length] if all_flags - try_me = args + try_me = args rest = [] end # Suck up whatever options we can switch_hash = switches @@ -466,11 +479,11 @@ # So, there's a case where the first time we request the value for a flag, # we get the default and not the user-provided value. The next time we request # it, we want to override it with the real value. # HOWEVER, sometimes this happens in reverse, so we want to err on taking the # user-provided, non-default value where possible. - if value + if value if options[name] options[name] = value if options[name] == flag.default_value else options[name] = value end @@ -483,16 +496,16 @@ # and rest may have more return parse_options_helper(rest,global_options,command,command_options,arguments) else if command check = rest - check = rest + try_me if all_flags - check.each() do |arg| + check = rest + try_me if all_flags + check.each() do |arg| if arg =~ /^\-\-$/ try_me.delete arg - break + break end - raise UnknownCommandArgument.new("Unknown option #{arg}",command) if arg =~ /^\-/ + raise UnknownCommandArgument.new("Unknown option #{arg}",command) if arg =~ /^\-/ end return [global_options,command,command_options,try_me + rest] else # Now we have our command name command_name = try_me.shift