Release Name: 0.3
Notes:
======== Version 0.3 ==============================================================
The biggest change in version 0.3 is that the argument type architecture is
now pluggable. Users can add their own typed argument parsers. Along with
doing that, I have added file argument types:
< - A file that will be read
> - A file that will be written to
<< - A file that will be read in by readlines
>> - A file that will be appended to
>? - A file that will be written to but the user is prompted if it already
exists
>>? - A file that will be appended to but the user is prompted if the file
doesn't exist
Lastly I added the ability to use a block argument to the Usage call so that
clean-up of objects created during parsing can be safely cleaned-up at the end
of the usage block.
The simplest example to illustrate all of the above attributes is a very
short copy program that has much more error checking (nice pretty error
checking) than your average short ruby copy script.
require "usage"
Usage.new "<infile >outfile" do |u|
u.outfile.write(u.infile.read)
end
A more complex example (taken from the actual built-in writable file code) is
shows how you can add a type of your own:
#
# This example is taken from the built-in class for arguments that are writable files
# that don't want to be overwritten
#
# first define the file exists exception class
class FileOutputExistsError < UsageMod::Error
attr_reader :filename
def initialize(filename)
@filename = filename
super("output file exists: '#{filename}'")
end
end
# next define the argument parser plugin
class FileOutputQueryPlugin < UsageMod::ArgumentParserPlugin
def initialize(usage_ui, str)
if FileTest.exist?(str) then
raise FileOutputExistsError.new(str) if usage_ui.ask_yes_no(OVERWRITE_QUERY % str, NO_RESPONSE) == NO_RESPONSE
end
@value = File.open(str, "w")
end
def close
@value.close
end
end
# lastly attach that parser to the character sequence '>?'
UsageMod::Base.add_type_handler(">?", FileOutputQueryPlugin)
After putting the above code into a library that you require, you do the following:
require "usage"
require "mycustomtype"
Usage.new "<infile >?outfile" do |u|
u.outfile.write(u.infile.read)
end
Check out the README for a complete description of the how usage works.
Changes:
* Added pluggable argument type parsers
* Added new argument types to handle reading/writing files
* Added ability to run Usage.new with a block
|