lib/gli.rb in gli-0.1.6 vs lib/gli.rb in gli-0.2.1
- old
+ new
@@ -1,18 +1,19 @@
require 'gli/command_line_token.rb'
require 'gli/command.rb'
require 'gli/switch.rb'
require 'gli/flag.rb'
require 'support/help.rb'
+require 'support/rdoc.rb'
# A means to define and parse a command line interface that works as
# Git's does, in that you specify global options, a command name, command
# specific options, and then command arguments.
module GLI
extend self
- VERSION = '0.1.5'
+ VERSION = '0.2.1'
@@program_name = $0.split(/\//)[-1]
@@post_block = nil
@@pre_block = nil
@@error_block = nil
@@ -23,34 +24,40 @@
flags.clear
commands.clear
clear_nexts
end
- # describe the next switch, flag, or command
+ # describe the next switch, flag, or command. This should be a
+ # short, one-line description
def desc(description); @@next_desc = description; end
+
+ # Provide a longer, more detailed description. This
+ # will be reformatted and wrapped to fit in 80 columns
+ def long_desc(long_desc); @@next_long_desc = long_desc; end
+
# describe the argument name of the next flag
def arg_name(name); @@next_arg_name = name; end
# set the default value of the next flag
def default_value(val); @@next_default_value = val; end
# Create a flag, which is a switch that takes an argument
def flag(names)
- flag = Flag.new(names,@@next_desc,@@next_arg_name,@@next_default_value)
+ flag = Flag.new(names,@@next_desc,@@next_arg_name,@@next_default_value,@@next_long_desc)
flags[flag.name] = flag
clear_nexts
end
# Create a switch
def switch(names)
- switch = Switch.new(names,@@next_desc)
+ switch = Switch.new(names,@@next_desc,@@next_long_desc)
switches[switch.name] = switch
clear_nexts
end
# Define a command.
def command(names)
- command = Command.new(names,@@next_desc,@@next_arg_name)
+ command = Command.new(names,@@next_desc,@@next_arg_name,@@next_long_desc)
commands[command.name] = command
yield command
clear_nexts
end
@@ -70,37 +77,37 @@
def post(&a_proc)
@@post_block = a_proc
end
# Define a block to run if an error occurs.
- # The block will receive the exception that was caught.
- # It should return false to avoid the built-in error handling
+ # The block will receive any Exception that was caught.
+ # It should return false to avoid the built-in error handling (which basically just
+ # prints out a message)
def on_error(&a_proc)
@@error_block = a_proc
end
# Runs whatever command is needed based on the arguments.
def run(args)
- commands[:help] = DefaultHelpCommand.new if !commands[:help]
+ rdoc = RDocCommand.new
+ commands[:rdoc] = rdoc if !commands[:rdoc]
+ commands[:help] = DefaultHelpCommand.new(rdoc) if !commands[:help]
begin
global_options,command,options,arguments = parse_options(args)
proceed = true
proceed = @@pre_block.call(global_options,command,options,arguments) if @@pre_block
if proceed
command = commands[:help] if !command
command.execute(global_options,options,arguments)
@@post_block.call(global_options,command,options,arguments) if @@post_block
end
- rescue UnknownCommandException, UnknownArgumentException, MissingArgumentException => ex
+ rescue Exception => ex
regular_error_handling = true
regular_error_handling = @@error_block.call(ex) if @@error_block
if regular_error_handling
- puts "error: #{ex}"
- puts
- help = commands[:help]
- help.execute({},{},[])
+ puts "error: #{ex.message}"
end
end
end
def program_name(override=nil)
@@ -139,10 +146,11 @@
def clear_nexts
@@next_desc = nil
@@next_arg_name = nil
@@next_default_value = nil
+ @@next_long_desc = nil
end
clear_nexts
def flags; @@flags ||= {}; end
@@ -173,11 +181,11 @@
if non_flag_i == 0
# no flags
if !command
command_name = args.shift
command = find_command(command_name)
- raise(UnknownCommandException,"Unknown command '#{command_name}'") if !command
+ raise "Unknown command '#{command_name}'" if !command
return parse_options_helper(args,global_options,command,command_options,arguments)
else
return global_options,command,command_options,arguments | args
end
elsif non_flag_i == -1
@@ -223,20 +231,20 @@
check.each() do |arg|
if arg =~ /^\-\-$/
try_me.delete arg
break
end
- raise(UnknownArgumentException,"Unknown argument #{arg}") if arg =~ /^\-/
+ raise "Unknown argument #{arg}" if arg =~ /^\-/
end
return [global_options,command,command_options,try_me | rest]
else
# Now we have our command name
command_name = try_me.shift
- raise(UnknownArgumentException,"Unknown argument #{command_name}") if command_name =~ /^\-/
+ raise "Unknown argument #{command_name}" if command_name =~ /^\-/
command = find_command(command_name)
- raise(UnknownCommandException,"Unknown command '#{command_name}'") if !command
+ raise "Unknown command '#{command_name}'" if !command
return parse_options_helper(rest,global_options,command,command_options,arguments)
end
end
@@ -250,16 +258,6 @@
return command if (command.aliases && command.aliases.include?(sym))
end
nil
end
- # Raise this if you get an argument you were not expecting
- class UnknownArgumentException < Exception
- end
-
- class UnknownCommandException < Exception
- end
-
- # Raise this if your command doesn't get the number of arguments you were expecting
- class MissingArgumentException < Exception
- end
end