1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * getopt.h - a simple getopt(3) implementation.
5 * Copyright (C) 2020 Sean Anderson <seanga2@gmail.com>
6 * Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
13 * struct getopt_state - Saved state across getopt() calls
17 * @index: Index of the next unparsed argument of @argv. If getopt() has
18 * parsed all of @argv, then @index will equal @argc.
22 /** @arg_index: Index within the current argument */
27 * @opt: Option being parsed when an error occurs. @opt is only
28 * valid when getopt() returns ``?`` or ``:``.
32 * @arg: The argument to an option, NULL if there is none. @arg
33 * is only valid when getopt() returns an option character.
41 * getopt_init_state() - Initialize a &struct getopt_state
42 * @gs: The state to initialize
44 * This must be called before using @gs with getopt().
46 void getopt_init_state(struct getopt_state *gs);
48 int __getopt(struct getopt_state *gs, int argc, char *const argv[],
49 const char *optstring, bool silent);
52 * getopt() - Parse short command-line options
53 * @gs: Internal state and out-of-band return arguments. This must be
54 * initialized with getopt_init_context() beforehand.
55 * @argc: Number of arguments, not including the %NULL terminator
56 * @argv: Argument list, terminated by %NULL
57 * @optstring: Option specification, as described below
59 * getopt() parses short options. Short options are single characters. They may
60 * be followed by a required argument or an optional argument. Arguments to
61 * options may occur in the same argument as an option (like ``-larg``), or
62 * in the following argument (like ``-l arg``). An argument containing
63 * options begins with a ``-``. If an option expects no arguments, then it may
64 * be immediately followed by another option (like ``ls -alR``).
66 * @optstring is a list of accepted options. If an option is followed by ``:``
67 * in @optstring, then it expects a mandatory argument. If an option is followed
68 * by ``::`` in @optstring, it expects an optional argument. @gs.arg points
69 * to the argument, if one is parsed.
71 * getopt() stops parsing options when it encounters the first non-option
72 * argument, when it encounters the argument ``--``, or when it runs out of
73 * arguments. For example, in ``ls -l foo -R``, option parsing will stop when
74 * getopt() encounters ``foo``, if ``l`` does not expect an argument. However,
75 * the whole list of arguments would be parsed if ``l`` expects an argument.
77 * An example invocation of getopt() might look like::
79 * char *argv[] = { "program", "-cbx", "-a", "foo", "bar", 0 };
80 * int opt, argc = ARRAY_SIZE(argv) - 1;
81 * struct getopt_state gs;
83 * getopt_init_state(&gs);
84 * while ((opt = getopt(&gs, argc, argv, "a::b:c")) != -1)
85 * printf("opt = %c, index = %d, arg = \"%s\"\n", opt, gs.index, gs.arg);
86 * printf("%d argument(s) left\n", argc - gs.index);
88 * and would produce an output of::
90 * opt = c, index = 1, arg = "<NULL>"
91 * opt = b, index = 2, arg = "x"
92 * opt = a, index = 4, arg = "foo"
95 * For further information, refer to the getopt(3) man page.
98 * * An option character if an option is found. @gs.arg is set to the
99 * argument if there is one, otherwise it is set to ``NULL``.
100 * * ``-1`` if there are no more options, if a non-option argument is
101 * encountered, or if an ``--`` argument is encountered.
102 * * ``'?'`` if we encounter an option not in @optstring. @gs.opt is set to
103 * the unknown option.
104 * * ``':'`` if an argument is required, but no argument follows the
105 * option. @gs.opt is set to the option missing its argument.
107 * @gs.index is always set to the index of the next unparsed argument in @argv.
109 static inline int getopt(struct getopt_state *gs, int argc,
110 char *const argv[], const char *optstring)
112 return __getopt(gs, argc, argv, optstring, false);
116 * getopt_silent() - Parse short command-line options silently
118 * @argc: Argument count
119 * @argv: Argument list
120 * @optstring: Option specification
122 * Same as getopt(), except no error messages are printed.
124 static inline int getopt_silent(struct getopt_state *gs, int argc,
125 char *const argv[], const char *optstring)
127 return __getopt(gs, argc, argv, optstring, true);
130 #endif /* __GETOPT_H */