[PATCH v3 18/23] lib: Add getopt

Simon Glass sjg at chromium.org
Tue Nov 3 16:11:48 CET 2020


On Sat, 17 Oct 2020 at 12:08, Sean Anderson <seanga2 at gmail.com> wrote:
>
> Some commands can get very unweildy if they have too many positional
> arguments. Adding options makes them easier to read, remember, and
> understand.
>
> This implementation of getopt has been taken from barebox, which has had
> option support for quite a while. I have made a few modifications to their
> version, such as the removal of opterr in favor of a separate getopt_silent
> function. In addition, I have moved all global variables into struct
> getopt_context.
>
> The getopt from barebox also re-orders the arguments passed to it so that
> non-options are placed last. This allows users to specify options anywhere.
> For example, `ls -l foo/ -R` would be re-ordered to `ls -l -R foo/` as
> getopt parsed the options. However, this feature conflicts with the const
> argv in cmd_tbl->cmd. This was originally added in 54841ab50c ("Make sure
> that argv[] argument pointers are not modified."). The reason stated in
> that commit is that hush requires argv to stay unmodified. Has this
> situation changed? Barebox also uses hush, and does not have this problem.
> Perhaps we could use their fix?
>
> I have assigned maintenance of getopt to Simon Glass, as it is currently
> only used by the log command. I would also be fine maintaining it.
>
> Signed-off-by: Sean Anderson <seanga2 at gmail.com>
> ---
>
> (no changes since v2)
>
> Changes in v2:
> - Expand documentation of getopt() to include examples
> - Remove opt prefix from getopt_state members
>
>  MAINTAINERS        |   1 +
>  doc/api/getopt.rst |   8 +++
>  doc/api/index.rst  |   1 +
>  include/getopt.h   | 130 +++++++++++++++++++++++++++++++++++++++++++++
>  lib/Kconfig        |   5 ++
>  lib/Makefile       |   1 +
>  lib/getopt.c       | 125 +++++++++++++++++++++++++++++++++++++++++++
>  7 files changed, 271 insertions(+)
>  create mode 100644 doc/api/getopt.rst
>  create mode 100644 include/getopt.h
>  create mode 100644 lib/getopt.c

Reviewed-by: Simon Glass <sjg at chromium.org>


More information about the U-Boot mailing list