David Golden > Getopt-Lucid-0.12 > Getopt::Lucid

Download:
Getopt-Lucid-0.12.tar.gz

Dependencies

Annotate this POD

CPAN RT

Open  0
Report a bug
Module Version: 0.12   Source   Latest Release: Getopt-Lucid-0.16

NAME ^

Getopt::Lucid - Clear, readable syntax for command line processing

SYNOPSIS ^

  use Getopt::Lucid qw( :all );

  # basic option specifications with aliases
  
  @specs = (
    Switch("version|V"),    
    Counter("verbose|v"), 
    Param("config|C"), 
    List("lib|l|I"),
    Keypair("define"),
    Switch("help|h")
  );
  
  $opt = Getopt::Lucid->getopt( \@specs );
  
  $verbosity = $opt->get_verbose;
  @libs = $opt->get_lib;
  %defs = $opt->get_define;
  
  %all_options = $opt->options;
  
  # advanced option specifications

  @adv_spec = (
    Param("input")->required,       # required
    Param("mode")->default("tcp"),  # defaults
    Param("host")->needs("port"),   # dependencies
    Param("port", qr/\d+/ ),        # regex validation
    Param("config", sub { -r } ),   # custom validation
    Param("help")->anycase,         # case insensitivity
  );
  
  # example with a config file
  
  use Config::Std;
  if ( -r $opt->get_config ) {
    read_config( $opt->get_config() => my %config_hash );
    $opt->merge_defaults( $config_hash{''} ); 
  }

DESCRIPTION ^

The goal of this module is providing good code readability and clarity of intent for command-line option processing. While readability is a subjective standard, Getopt::Lucid relies on a more verbose, plain-English option specification as compared against the more symbolic approach of Getopt::Long. Key features include:

USAGE ^

Option Styles, Naming and "Strictness"

Getopt::Lucid support three kinds of option styles: long-style ("--foo"), short-style ("-f") and bareword style ("foo"). Short-style options are automatically unbundled during command line processing if a single dash is followed by more than one letter (e.g. "-xzf" becomes "-x -z -f" ).

Each option is identified in the specification with a string consisting of the option "name" followed by zero or more "aliases", with any alias (and each subsequent alias) separated by a vertical bar character. E.g.:

  "lib|l|I" means name "lib", alias "l" and alias "I"

Names and aliases must begin with an alphanumeric charactes, but subsequently may also include both underscore and dash. (E.g. both "input-file" and "input_file" are valid.) While names and aliases are interchangeable when provided on the command line, the "name" portion is used with the accessors for each option (see "Accessors and Mutators").

Any of the names and aliases in the specification may be given in any of the three styles. By default, Getopt::Lucid works in "magic" mode, in which option names or aliases may be specified with or without leading dashes, and will be parsed from the command line whether or not they have corresponding dashes. Single-character names or aliases may be read with no dash, one dash or two dashes. Multi-character names or aliases must have either no dashes or two dashes. E.g.:

In practice, this means that the specification need not use dashes, but if used on the command line, they will be treated appropriately.

Alternatively, Getopt::Lucid can operate in "strict" mode by setting $Getopt::Lucid::STRICT to a true value. In strict mode, option names and aliases may still be specified in any of the three styles, but they will only be parsed from the command line if they are used in exactly the same style. E.g., given the name and alias "--help|-h", only "--help" and "-h" are valid for use on the command line.

Option Specification Constructors

Options specifications are provided to Getopt::Lucid in an array. Entries in the array must be created with one of five special constructor functions that return a specification object. These constructor functions may be imported either individually or as a group using the import tag ":all" (e.g. use Getopt::Lucid qw(:all);).

The form of the constructor is:

 Param( NAME_ARGUMENT, VALIDATION_ARGUMENT(S) );

The constructor name indicates the type of option. The name argument is a string with the names and aliases separated by vertical bar characters. Validation arguments may or may not be relevant, depending on the type of option. (See "Validation" below.)

The five option specification constructors are:

An option specification can be further modified with the following methods, each of which return the object modified so that modifier chaining is possible:

Modifiers may be chained to allow multiple modifiers. E.g.:

  @spec = (
    Param("input")->default("/dev/random")->needs("output"),
    Param("output)->default("/dev/null"),
  );

Validation

For Param, List, and Keypair option types, the constructor can be passed an optional validation specification. Values provided on the command line will be validated according to the specification or an exception will be thrown. A validation specification can be either a regular expression, or a reference to a subroutine. Keypairs take up to two validation specifiers. The first is applied to keys and the second is applied to values; either can be left undef to ignore validation. (More complex validation of specific values for specific keys must be done manually.)

  @spec = (
    Param("copies", "\d+"),
    Param("scaling", qr/\d+/),
    Param("input", sub { -r } ),
    Keypair("define, "os|arch", "\w+"),
  );

For validation subroutines, the value found on the command line is passed as the first element of @_, and $_ is also set equal to the first element. (N.B. Changing $_ will not change the value that is captured.) The value validates if the subroutine returns a true value.

For validation with regular expressions, consider using Regexp::Common for a ready library of validation options.

Parsing the Command Line

Technically, Getopt::Lucid scans an array for command line options, not a command-line string. By default, this array is @ARGV (though other arrays can be used -- see new()), which is typically provided by the operating system according to system-specific rules.

When Getopt::Lucid processes the array, it scans the array in order, removing any specified command line options and any associated arguments, and leaving behind any unrecognized elements in the array. If an element consisting solely of two-dashes ("--") is found, array scanning is terminated at that point. Any options found during scanning are applied in order. E.g.:

  @ARGV = qw( --lib /tmp --lib /var );
  my $opt = Getopt::Lucid->getopt( [ List("lib") ] );
  print join ", " $opt->lib;
  # prints "/tmp, /var"

If an element encountered in processing begins with a dash, but is not recognized as a short-form or long-form option name or alias, an exception will be thrown.

Negation

Getopt::Lucid also supports negating options. Options are negated if the option is specified with "no-" or "--no-" prefixed to a name or alias. By default, negation clears the option: Switch and Counter options are set to zero; Param options are set to ""; List and Keypair options are set to an empty list and empty hash, respectively. For List and Keypair options, it is also possible to negate a specific list element or hash key by placing an equals sign and the list element or key immediately after the option name:

  --no-lib=/tmp --no-define=arch
  # removes "/tmp" from lib and the "arch" key from define

As with all options, negation is processed in order, allowing a "reset" in the middle of command line processing. This may be useful for those using command aliases who wish to "switch off" options in the alias. E.g, in unix:

  $ alias wibble = wibble.pl --verbose
  $ wibble --no-verbose
  
  # @ARGV would contain ( "--verbose", "--no-verbose" )

This also may have applications in post-processing configuration files (see "Managing Defaults and Config Files").

Accessors and Mutators

After processing the command-line array, the values of the options may be read or modified using accessors/mutators of the form "get_NAME" and "set_NAME", where NAME represents the option name in the specification without any leading dashes. E.g.

  @spec = (
    Switch("--test|-t"),
  );
  
  $opt = Getopt::Long->getopt( \@spec );
  print $opt->get_test ? "True" : "False";
  $opt->set_test(1);

For option names with dashes, underscores should be substitued in the accessor calls. E.g.

  @spec = (
    Param("--input-file|-i")->required(),
  );
  
  $opt = Getopt::Long->getopt( \@spec );
  print $opt->get_input_file;

This can create an ambiguous case if a similar option exists with underscores in place of dashes. (E.g. "input_file" and "input-file".) Users can safely avoid these problems by choosing to use either dashes or underscores exclusively and not mixing the two styles.

Using the "set_NAME" mutator is not recommended and should be used with caution. No validation is performed and changes will be lost if the results of processing the command line array are recomputed (e.g, such as occurs if new defaults are applied). List and Keypair options mutators take a list, not references.

Managing Defaults and Config Files

A typical problem for command-line option processing is the precedence relationship between default option values specified within the program, default option values stored in a configuration file or in environment variables, and option values specified on the command-line, particularly when the command-line specifies an alternate configuration file.

Getopt::Lucid takes the following approach to this problem:

With this approach, the resulting option set is always the result of applying options (or negations) from the command-line array to a set of default-values. Users have complete freedom to apply whatever precedence rules they wish to the default values and may even change default values after the command-line array is processed without losing the options given on the command line.

Getopt::Lucid provides several functions to assist in manipulating default values:

Exceptions and Error Handling

Getopt::Lucid uses Exception::Class for exceptions. When a major error occurs, Getopt::Lucid will die and throw one of three Exception::Class subclasses:

These exception may be caught using an eval block and allow the calling program to respond differently to each class of exception.

  my $opt;
  eval { $opt = Getopt::Lucid->getopt( \@spec ) };
  if ($@) {
    print "$@\n" && print_usage() && exit 1
      if ref $@ eq 'Getopt::Lucid::Exception::ARGV';
    ref $@ ? $@->rethrow : die $@;
  }

Ambiguous Cases and Gotchas

One-character aliases and anycase
  @spec = (
    Counter("verbose|v")->anycase,
    Switch("version|V")->anycase,
  );

Consider the spec above. By specifying anycase on these, "verbose", "Verbose", "VERBOSE" are all acceptable, as are "version", "Version" and so on. (Including long-form versions of these, too, if "magic" mode is used.) However, what if the command line has "-v" or even "-v -V"? In this case, the rule is that exact case matches are used before case-insensitive matches are searched. Thus, "-v" can only match "verbose", despite the anycase modification, and likewise "-V" can only match "version".

Identical names except for dashes and underscores
  @spec = (
    Param("input-file"),
    Switch("input_file"),
  );

Consider the spec above. These are two, separate, valid options, but a call to the accessor get_input_file is ambiguous and may return either option, depending on which first satisfies a "fuzzy-matching" algorithm inside the accessor code. Avoid identical names with mixed dash and underscore styles.

METHODS ^

new()

 $opt = Getopt::Lucid->new( \@option_spec );
 $opt = Getopt::Lucid->new( \@option_spec, \@option_array );

Creates a new Getopt::Lucid object. An array reference to an option spec is required as an argument. (See "USAGE" for a description of the object spec). By default, objects will be set to read @ARGV for command line options. An optional second argument with a reference to an array will use that array for option processing instead. For typical cases, users will likely prefer to call getopt instead, which creates a new object and parses the command line with a single function call.

append_defaults()

 %options = append_defaults( %config_hash );
 %options = append_defaults( \%config_hash );

Takes a hash or hash reference of new default values, modifies the stored defaults, recalculates the result of processing the command line with the revised defaults, and returns a hash with the resulting options. Each key/value pair in the passed hash is added to the stored defaults. For Switch and Param options, the value in the passed hash will overwrite any pre-existing value. For Counter options, the value is added to any pre-existing value. For List options, the value (or values, if the value is an array reference) will be pushed onto the end of the list of existing values. For Keypair options, the keypairs will be added to the existing hash, overwriting existing key/value pairs (just like merging two hashes). Keys which are not valid names from the options specification will be ignored.

defaults()

 %defaults = $opt->defaults();

Returns a hash containing current default values. Keys are names from the option specification (without any leading dashes). These defaults represent the baseline values that are modified by the parsed command line options.

getopt()

 $opt = Getopt::Lucid->getopt( \@option_spec );
 $opt = Getopt::Lucid->getopt( \@option_spec, \@option_array );
 $opt->getopt();

Parses the command line array (@ARGV by default). When called as a class function, getopt takes the same arguments as new, calls new to create an object before parsing the command line, and returns the new object. When called as an object method, it takes no arguments and returns itself.

merge_defaults()

 %options = merge_defaults( %config_hash );
 %options = merge_defaults( \%config_hash );

Takes a hash or hash reference of new default values, modifies the stored defaults, recalculates the result of processing the command line with the revised defaults, and returns a hash with the resulting options. Each key/value pair in the passed hash is added to the stored defaults, overwriting any pre-existing value. Keys which are not valid names from the options specification will be ignored.

names()

 @names = $opt->names();

Returns the list of names in the options specification. Each name represents a key in the hash of options provided by options.

options()

 %options = $opt->options();

Returns a deep copy of the options hash. Before getopt is called, its behavior is undefined. After getopt is called, this will return the result of modifying the defaults with the results of command line processing.

replace_defaults()

 %options = replace_defaults( %config_hash );
 %options = replace_defaults( \%config_hash );

Takes a hash or hash reference of new default values, replaces the stored defaults, recalculates the result of processing the command line with the revised defaults, and returns a hash with the resulting options. Each key/value pair in the passed hash replaces existing defaults, including those given in the option specifications. Keys which are not valid names from the option specification will be ignored.

reset_defaults()

 %options = reset_defaults();

Resets the stored defaults to the original values from the options specification, recalculates the result of processing the command line with the restored defaults, and returns a hash with the resulting options. This undoes the effect of a merge_defaults or add_defaults call.

SEE ALSO ^

INSTALLATION ^

The following commands will build, test, and install this module:

 perl Build.PL
 perl Build
 perl Build test
 perl Build install

BUGS ^

Please report bugs using the CPAN Request Tracker at http://rt.cpan.org/NoAuth/Bugs.html?Dist=Getopt-Lucid

AUTHOR ^

David A Golden (DAGOLDEN)

dagolden@cpan.org

http://dagolden.com/

COPYRIGHT ^

Copyright (c) 2005 by David A Golden

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the LICENSE file included with this module.