getopts.tex 5.4 KB

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