Section 26.5. Option Aliasing

   


26.5. Option Aliasing

One of the primary benefits of using popt over getopt() is the ability to use option aliasing. This lets the user specify options that popt expands into other options when they are specified. If the standard grep program made use of popt, users could add a --text option that expanded to -i -n -E -2 to let them more easily find information in text files.

26.5.1. Specifying Aliases

Aliases are normally specified in two places: /etc/popt and the .popt file in the user's home directory (found through the HOME environment variable). Both files have the same format, an arbitrary number of lines formatted like this:

 appname alias newoption expansion 


The appname is the name of the application, which must be the same as the name parameter passed to poptGetContext(). This allows each file to specify aliases for multiple programs. The alias keyword specifies that an alias is being defined; currently, popt configuration files support only aliases, but other abilities may be added in the future. The next option is the option that should be aliased, and it may be either a short or a long option. The rest of the line specifies the expansion for the alias. It is parsed similarly to a shell command, which allows \,", and ' to be used for quoting. If a backslash is the final character on a line, the next line in the file is assumed to be a logical continuation of the line containing the backslash, just as in shell.

The following entry would add a --text option to the grep command, as suggested at the beginning of this section:

 grep alias --text -i -n -E -2 


26.5.2. Enabling Aliases

An application must enable alias expansion for a poptContext before calling poptGetNextOpt() for the first time. There are three functions that define aliases for a context.

 int poptReadDefaultConfig(poptContext con, int flags); 


This function reads aliases from /etc/popt and the .popt file in the user's home directory. Currently, flags should be zero, as it is provided only for future expansion.

 int poptReadConfigFile(poptContext con, char * fn); 


The file specified by fn is opened and parsed as a popt configuration file. This allows programs to use program-specific configuration files.

 int poptAddAlias(poptContext con, struct poptAlias alias, int flags); 


Occasionally, programs want to specify aliases without having to read them from a configuration file. This function adds a new alias to a context. The flags argument should be zero, as it is currently reserved for future expansion. The new alias is specified as a struct poptAlias, which is defined as

 struct poptAlias {     char * longName;              /* may be NULL */     char shortName;               /* may be '\0' */     int argc;     char ** argv;                 /* must be free()able */ }; 


The first two elements, longName and shortName, specify the option that is aliased. The final two, argc and argv, define the expansion to use when the aliases option is encountered.


       
    top
     


    Linux Application Development
    Linux Application Development (paperback) (2nd Edition)
    ISBN: 0321563220
    EAN: 2147483647
    Year: 2003
    Pages: 168

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net