getopts.tex 5.8 KB

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