Home | History | Annotate | Line # | Download | only in getopt
getopt.1 revision 1.1
      1  1.1  brezak .Dd June 21, 1993
      2  1.1  brezak .Dt GETOPT 1
      3  1.1  brezak .Os
      4  1.1  brezak .Sh NAME
      5  1.1  brezak .Nm getopt
      6  1.1  brezak .Nd parse command options
      7  1.1  brezak .Sh SYNOPSIS
      8  1.1  brezak .Nm set \-\- \`getopt optstring $*\`
      9  1.1  brezak .Sh DESCRIPTION
     10  1.1  brezak .Nm Getopt
     11  1.1  brezak is used to break up options in command lines for easy parsing by
     12  1.1  brezak shell procedures, and to check for legal options.
     13  1.1  brezak .Op Optstring
     14  1.1  brezak is a string of recognized option letters (see
     15  1.1  brezak .Xr getopt 3
     16  1.1  brezak );
     17  1.1  brezak if a letter is followed by a colon, the option
     18  1.1  brezak is expected to have an argument which may or may not be
     19  1.1  brezak separated from it by white space.
     20  1.1  brezak The special option
     21  1.1  brezak .B \-\-
     22  1.1  brezak is used to delimit the end of the options.
     23  1.1  brezak .Nm Getopt
     24  1.1  brezak will place
     25  1.1  brezak .B \-\-
     26  1.1  brezak in the arguments at the end of the options,
     27  1.1  brezak or recognize it if used explicitly.
     28  1.1  brezak The shell arguments
     29  1.1  brezak (\fB$1 $2\fR ...) are reset so that each option is
     30  1.1  brezak preceded by a
     31  1.1  brezak .B \-
     32  1.1  brezak and in its own shell argument;
     33  1.1  brezak each option argument is also in its own shell argument.
     34  1.1  brezak .Sh EXAMPLE
     35  1.1  brezak The following code fragment shows how one might process the arguments
     36  1.1  brezak for a command that can take the options
     37  1.1  brezak .Op a
     38  1.1  brezak and
     39  1.1  brezak .Op b ,
     40  1.1  brezak and the option
     41  1.1  brezak .Op o ,
     42  1.1  brezak which requires an argument.
     43  1.1  brezak .Pp
     44  1.1  brezak .Bd -literal -offset indent
     45  1.1  brezak set \-\- \`getopt abo: $*\`
     46  1.1  brezak if test $? != 0
     47  1.1  brezak then
     48  1.1  brezak 	echo 'Usage: ...'
     49  1.1  brezak 	exit 2
     50  1.1  brezak fi
     51  1.1  brezak for i
     52  1.1  brezak do
     53  1.1  brezak 	case "$i"
     54  1.1  brezak 	in
     55  1.1  brezak 		\-a|\-b)
     56  1.1  brezak 			flag=$i; shift;;
     57  1.1  brezak 		\-o)
     58  1.1  brezak 			oarg=$2; shift; shift;;
     59  1.1  brezak 		\-\-)
     60  1.1  brezak 			shift; break;;
     61  1.1  brezak 	esac
     62  1.1  brezak done
     63  1.1  brezak .Ed
     64  1.1  brezak .Pp
     65  1.1  brezak This code will accept any of the following as equivalent:
     66  1.1  brezak .Pp
     67  1.1  brezak .Bd -literal -offset indent
     68  1.1  brezak cmd \-aoarg file file
     69  1.1  brezak cmd \-a \-o arg file file
     70  1.1  brezak cmd \-oarg -a file file
     71  1.1  brezak cmd \-a \-oarg \-\- file file
     72  1.1  brezak .Ed
     73  1.1  brezak .Sh SEE ALSO
     74  1.1  brezak .Xr sh 1 ,
     75  1.1  brezak .Xr getopt 3
     76  1.1  brezak .Sh DIAGNOSTICS
     77  1.1  brezak .Nm Getopt
     78  1.1  brezak prints an error message on the standard error output when it
     79  1.1  brezak encounters an option letter not included in
     80  1.1  brezak .Op optstring .
     81  1.1  brezak .Sh HISTORY
     82  1.1  brezak Written by Henry Spencer, working from a Bell Labs manual page.
     83  1.1  brezak Behavior believed identical to the Bell version.
     84  1.1  brezak .Sh BUGS
     85  1.1  brezak Whatever
     86  1.1  brezak .Xr getopt 3
     87  1.1  brezak has.
     88  1.1  brezak .Pp
     89  1.1  brezak Arguments containing white space or imbedded shell metacharacters
     90  1.1  brezak generally will not survive intact;  this looks easy to fix but isn't.
     91  1.1  brezak .Pp
     92  1.1  brezak The error message for an invalid option is identified as coming
     93  1.1  brezak from
     94  1.1  brezak .Nm getopt
     95  1.1  brezak rather than from the shell procedure containing the invocation
     96  1.1  brezak of
     97  1.1  brezak .Nm getopt ;
     98  1.1  brezak this again is hard to fix.
     99  1.1  brezak .Pp
    100  1.1  brezak The precise best way to use the
    101  1.1  brezak .Nm set
    102  1.1  brezak command to set the arguments without disrupting the value(s) of
    103  1.1  brezak shell options varies from one shell version to another.
    104  1.1  brezak varies from one shell version to another.
    105