From: GGarramuno@... (GGarramuno) Date: 2004-01-09T11:46:40+09:00 Subject: ANN: First release of Perl's Getopt::Declare for ruby Okay, as a way of returning the favor for all my recent silly questions, I've uploaded a first release of the ruby port of Damian Conway's Getopt::Declare (written originally for Perl). It is located at: http://rubyforge.org/projects/getoptdeclare/ Library has been tested on Windows only, albeit it should work on all platforms. Let me know how it goes and what could be improved, in either the distribution, the code or whatever. Getopt::Declare is yet another command-line argument parser, one which is specifically designed to be powerful but exceptionally easy to use. It supports many options not supported by optparser or getoptlong as well as it has a simpler syntax. To parse the command-line in +ARGV+, one simply creates a Getopt::Declare object, by passing Getopt::Declare::new() a specification of the various parameters that may be encountered: args = Getopt::Declare.new(specification) The specification is a single string such as this: specification = %q( [tight] -a Process all data -b Set mean byte length threshold to { bytelen = t } +c Create new file --del Delete old file { delold() } delete [ditto] e x Expand image to height and width { expand(h,w) } -F ... Process named file(s) { defer( proc { file.each {|i| process(i) } } ) } =getrand [] Get a random number (or, optionally, of them) { n = 1 unless !n.empty? } -- Traditionally indicates end of arguments { finish } ) in which the syntax of each parameter is declared, along with a description and (optionally) one or more actions to be performed when the parameter is encountered. The specification string may also include other usage formatting information (such as group headings or separators) as well as standard Ruby comments (which are ignored). Calling Getopt::Delare::new() parses the contents of the array +ARGV+, extracting any arguments which match the parameters defined in the specification string, and storing the parsed values as hash elements within the new Getopt::Declare object being created. Other features of the Getopt::Declare package include: * The use of full Ruby regular expressions to constrain matching of parameter components. * Automatic generation of error, usage and version information. * Optional conditional execution of embedded actions (i.e. only on successful parsing of the entire command-line) * Strict or non-strict parsing (unrecognized command-line elements may either trigger an error or may simply be left in +ARGV+ * Declarative specification of various inter-parameter relationships (for example, two parameters may be declared mutually exclusive and this relationship will then be automatically enforced). * Intelligent clustering of adjacent flags (for example: the command-line sequence "-a -b -c" may be abbreviated to "-abc", unless there is also a -abc flag declared). * Selective or global case-insensitivity of parameters. * The ability to parse files (especially configuration files) instead of the command-line.