Getopt::Function - provide mixed options with help information


    use Getopt::Function qw(maketrue makevalue);
    $::opthandler = new Getopt::Function 
      [ <option-list> ], { <option> => [ <function>, <short descript>, 
                                       <argument type>] }
   $result = GetOptions (...option-descriptions...);


The aim of this module is to make it easy to provide sophisticated and complex interfaces to commands in a simple to use and clear manner with proper help facilities.

It is designed to do this by making it possible to write the options, with documentation and subroutines to implement them, directly into a hash and then call the appropriate ones as options detected on the command line.

$gto = new Getopt::Function

This constructor takes two arguments. the first is a reference to a list of options names in Getopt::Mixed format (see the documentation for Getopt::Mixed), but with options grouped in strings with their different forms.

The second argument is a reference to a hash of option functions and descriptions. Example:-

  new Getopt::Function 
    [ "red r>red", "green g>green", 
      "bright=s b>bright"] ,
    { "red" => [ &maketrue,
                   "print using red" ],
      "green" => [ sub { print STERR "warning: the green isn't very "
                                 . "good\n"; &maketrue},
                           "print using green" ],
      "bright" => [ &makevalue, "set brightness", "INTENSITY" ],


This is a basic code example using most features

    use Getopt::Function qw(maketrue makevalue);

    use vars qw($not_perfect $redirect $since); 


    $::opthandler = new Getopt::Function
      [ "version V>version",
        "usage h>usage help>usage",
        "verbose:i v>verbose",
        "exclude=s e>exclude",
        "include=s i>include",
          "exclude" => [ sub { push @::exclude, $::value; },
            "Add a list of regular expressions for URLs to ignore.",
            "EXCLUDE RE" ],
          "include" => [ sub { push @::include, $::value; },
            "Give regular expression for URLs to check (if this "
            . "option is given others aren't checked).",
            "INCLUDE RE" ],
          "maxno" => [ \&makevalue, 
            "stop after a certain number",
            "ITERATIONS" ],


    sub usage() {
      print <<EOF;
    example [options]

      print <<EOF;

    Show off how we could use uptions

    sub version() {
      print <<'EOF';
    example version
    $NOTId:,v 1.3 1010/10/22 09:10:46 joebloggs Exp $

    my @list=biglist(); #get a list of things
    foreach $item ( @biglist ) {
      $maxno==0 && last;
      is_member ($item, @include) or is_member ($item, @exclude) && next;
      do_big_things @item;


This adds the standard options provided by the options module its self to the hash. It says nothing about the option list so some of these options may be made inaccessible.

To use these you have to provide the usage() and version() functions in the main package. Something like this.

        sub usage() {
          print <<EOF; [options] url-base file-base 

          print <<EOF;

        Extract the link and index information from a directory containing
        HTML files.

        sub version() {
          print <<'EOF';
        lists-from-files version 
        $Id:,v 1.10 2001/08/30 21:31:11 mikedlr Exp $

The standard functions will not override ones you have already provided.


This just gives the usage information for the program then exits. Normally this should be mapped also to the --help option (and possibly a short option like -h if that's available).


This prints the version of the program then exits.


This gives the help information for the options which are its parameters.


This sets the variable $::verbose to the value given as a parameter or $Getopt::Function::verbose_default (default value 4) if no value is given.


This sets the variable silent to be true for hushing normal program output. Standard aliases to create for this would be --quiet and -q.


This provides a convenience function which simply sets a variable in the main package true according to the option it is called for. If the option is negative (no-... e.g. no-delete as opposed to delete), it sets the variable false.

If the option name contains - then this is substituted for with a _.


This provides a convenience function which simply sets a variable in the main package corresponding to the option to a the given value.

If the option name contains - then this is substituted for with a _.


Checks all of the options calling the appropriate functions.

The following local variables are available to the function in the main package.


The option object. This can be used to call include


This is the option that was called. The same as the hash key that is used. It is determined through the option list. See above.


If the option can take a parameter this will contain it. It will be undefined if the parameter wasn't given.


This will be the option string as entered.


This is really an internal function and is used to implement list_opts and help_opt. Given a string from the options list, this prints out the the options listed in the string and their docuentation in a neat format.


This function prints out the usage information for all of the options.


This function searches through the array of options until it gets one which matches then prints out its documentation.

If the help option is a single character then we only print out a single character option which matches exactly. Otherwise we print the first long option who's start matches. This doesn't guarantee that we unambiguously have chosen that option, but could be useful where someone has forgotten part of the option name...


There is no scheme for automatic way to do negation. The workaround is to define the negative and positive options. This should be fixed.