getopts.tex 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. %
  2. % $Id$
  3. % This file is part of the FPC documentation.
  4. % Copyright (C) 1997, by Michael Van Canneyt
  5. %
  6. % The FPC documentation is free text; you can redistribute it and/or
  7. % modify it under the terms of the GNU Library General Public License as
  8. % published by the Free Software Foundation; either version 2 of the
  9. % License, or (at your option) any later version.
  10. %
  11. % The FPC Documentation is distributed in the hope that it will be useful,
  12. % but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. % Library General Public License for more details.
  15. %
  16. % You should have received a copy of the GNU Library General Public
  17. % License along with the FPC documentation; see the file COPYING.LIB. If not,
  18. % write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. % Boston, MA 02111-1307, USA.
  20. %
  21. \chapter{The GETOPTS unit.}
  22. This document describes the GETOPTS unit for Free Pascal. It was written for
  23. \linux\ by Micha\"el Van Canneyt. It now also works for all supported platforms.
  24. The getopts unit provides a mechanism to handle command-line options in
  25. a structured way, much like the GNU getopts mechanism. It allows you to
  26. define the valid options for you program, and the unit will then parse the
  27. command-line options for you, and inform you of any errors.
  28. The chapter is divided in 2 sections:
  29. \begin{itemize}
  30. \item The first section lists types, constants and variables from the
  31. interface part of the unit.
  32. \item The second section describes the functions defined in the unit.
  33. \end{itemize}
  34. \section {Types, Constants and variables : }
  35. \subsection{Constants}
  36. \var{No\_Argument=0} : Specifies that a long option does not take an
  37. argument. \\
  38. \var{Required\_Argument=1} : Specifies that a long option needs an
  39. argument. \\
  40. \var{Optional\_Argument=2} : Specifies that a long option optionally takes an
  41. argument. \\
  42. \var{EndOfOptions=\#255} : Returned by getopt, getlongopts to indicate that
  43. there are no more options.
  44. \subsection{Types}
  45. \begin{verbatim}
  46. TOption = record
  47. Name : String;
  48. Has_arg : Integer;
  49. Flag : PChar;
  50. Value : Char;
  51. end;
  52. POption = ^TOption;
  53. \end{verbatim}
  54. The \var{option} type is used to communicate the long options to \var{GetLongOpts}.
  55. The \var{Name} field is the name of the option. \var{Has\_arg} specifies if the option
  56. wants an argument, \var{Flag} is a pointer to a \var{char}, which is set to
  57. \var{Value}, if it is non-\var{nil}.
  58. \var{POption} is a pointer to a
  59. \var{Option} record. It is used as an argument to the \var{GetLongOpts}
  60. function.
  61. \subsection{Variables}
  62. \var{OptArg:String} \ Is set to the argument of an option, if the option needs
  63. one.\\
  64. \var{Optind:Longint} \ Is the index of the current \var{paramstr()}. When
  65. all options have been processed, \var{optind} is the index of the first
  66. non-option parameter. This is a read-only variable. Note that it can become
  67. equal to \var{paramcount+1}\\
  68. \var{OptErr:Boolean} \ Indicates whether \var{getopt()} prints error
  69. messages.\\
  70. \var{OptOpt:Char} \ In case of an error, contains the character causing the
  71. error.
  72. \section {Procedures and functions}
  73. \begin{function}{GetLongOpts}
  74. \Declaration
  75. Function GetLongOpts (Shortopts : String, LongOpts : POption; var Longint
  76. : Longint ) : Char;
  77. \Description
  78. Returns the next option found on the command-line, taking into account long
  79. options as well. If no more options are
  80. found, returns \var{EndOfOptions}. If the option requires an argument, it is
  81. returned in the \var{OptArg} variable.
  82. \var{ShortOptions} is a string containing all possible one-letter options.
  83. (see \seef{Getopt} for its description and use)
  84. \var{LongOpts} is a pointer to the first element of an array of \var{Option}
  85. records, the last of which needs a name of zero length.
  86. The function tries to match the names even partially (i.e. \var{--app}
  87. will match e.g. the \var{append} option), but will report an error in case of
  88. ambiguity.
  89. If the option needs an argument, set \var{Has\_arg} to
  90. \var{Required\_argument}, if the option optionally has an argument, set
  91. \var{Has\_arg} to \var{Optional\_argument}. If the option needs no argument,
  92. set \var{Has\_arg} to zero.
  93. Required arguments can be specified in two ways :
  94. \begin{enumerate}
  95. \item \ Pasted to the option : \var{--option=value}
  96. \item \ As a separate argument : \var {--option value}
  97. \end{enumerate}
  98. Optional arguments can only be specified through the first method.
  99. \Errors
  100. see \seef{Getopt}, \seem{getopt}{3}
  101. \SeeAlso
  102. Getopt
  103. \end{function}
  104. \begin{function}{Getopt}
  105. \Declaration
  106. Function Getopt (Shortopts : String) : Char;
  107. \Description
  108. Returns the next option found on the command-line. If no more options are
  109. found, returns \var{EndOfOptions}. If the option requires an argument, it is
  110. returned in the \var{OptArg} variable.
  111. \var{ShortOptions} is a string containing all possible one-letter options.
  112. If a letter is followed by a colon (:), then that option needs an argument.
  113. If a letter is followed by 2 colons, the option has an optional argument.
  114. If the first character of \var{shortoptions} is a \var{'+'} then options following a non-option are
  115. regarded as non-options (standard Unix behavior). If it is a \var{'-'},
  116. then all non-options are treated as arguments of a option with character
  117. \var{\#0}. This is useful for applications that require their options in
  118. the exact order as they appear on the command-line.
  119. If the first character of \var{shortoptions} is none of the above, options
  120. and non-options are permuted, so all non-options are behind all options.
  121. This allows options and non-options to be in random order on the command
  122. line.
  123. \Errors
  124. Errors are reported through giving back a \var{'?'} character. \var{OptOpt}
  125. then gives the character which caused the error. If \var{OptErr} is
  126. \var{True} then getopt prints an error-message to \var{stdout}.
  127. \SeeAlso
  128. \seef{GetLongOpts}, \seem{getopt}{3}
  129. \end{function}
  130. \latex{\lstinputlisting{optex/optex.pp}}
  131. \html{\input{optex/optex.tex}}