.\" Copyright (c) 2010-2022 Julien Nadeau Carriere .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED .\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, .\" INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES .\" (INCLUDING BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR .\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, .\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING .\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" .Dd December 21, 2022 .Dt AG_GETOPT 3 .Os Agar 1.7 .Sh NAME .Nm AG_Getopt .Nd agar incremental argument list parser .Sh SYNOPSIS .Bd -literal #include .Ed .Sh DESCRIPTION The .Nm interface parses command-line arguments. .Sh INTERFACE .nr nS 1 .Ft "int" .Fn AG_Getopt "int argc" "char * const argv[]" "const char *optstring" "char **pOptArg" "int *pOptInd" .nr nS 0 .Pp The .Fn AG_Getopt function incrementally parses command-line arguments .Fa argv and returns the next .Em known option character. A .Em known character is one that occurs in the dictionary specified by the .Fa optstring argument. .Pp If a character is followed by ":" in .Fa optstring then a string is expected to follow the option character in the command-line arguments. This string is returned into .Fa pOptArg , if not NULL. .Pp The index of the last argument processed by .Nm is returned into .Fa pOptInd , if not NULL. .Pp On platforms where command-line arguments are not supported, .Fn AG_Getopt always returns -1. .Sh EXAMPLES The following program uses .Fn AG_Getopt to parse its command-line options: .Bd -literal -offset indent .\" SYNTAX(c) static int debug = 0; int main(int argc, char *argv[]) { char *optArg, *filename; int c, optInd, i; while ((c = AG_Getopt(argc, argv, "df:?h", &optArg, &optInd)) != -1) { switch (c) { case 'd': debug = 1; break; case 'f': filename = optArg; break; default: printf("Usage: my-program [-d] " "[-f filename] [...]\\n") return (1); } } for (i = optInd; i < argc; i++) printf("Extra argument: %s\\n", argv[i]); return (0); } .Ed .Sh SEE ALSO .Xr AG_Intro 3 .Sh HISTORY The .Nm interface first appeared in Agar 1.4.0.