git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH/RFC] parse-options: add facility to make options configurable

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Mar 25, 2017, 16:47 UTC
Message-ID
<CACBZZX6iz5QpfpOO6s9c-GY7+ZZ2uXBxqgKfSRhU+__P0VLC5g@mail.gmail.com>
In-Reply-To
<20170324231013.23346-1-avarab@gmail.com>
 On Sat, Mar 25, 2017 at 12:10 AM, Ævar Arnfjörð Bjarmason
<avarab@gmail.com> wrote:
Show 51 quoted lines
> [...]
> This is all very proof-of-concept, and uses the ugly hack of s/const
> // for the options struct because I'm now keeping state in it, as
> noted in one of the TODO comments that should be moved.
> [...]
>  static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
> -                          const struct option *options)
> +                          struct option *options)
>  {
>         const struct option *all_opts = options;
> [...]
> @@ -313,7 +314,17 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
>                                 continue;
>                         p->opt = rest + 1;
>                 }
> -               return get_value(p, options, all_opts, flags ^ opt_flags);
> +               if (!(ret = get_value(p, options, all_opts, flags ^ opt_flags))) {
> +                       /* TODO: Keep some different state on the side
> +                        * with info about what options we've
> +                        * retrieved via the CLI for use in the loop
> +                        * in parse_options_step, instead of making
> +                        * the 'options' non-const
> +                        */
> +                       if (options->flags & PARSE_OPT_CONFIGURABLE)
> +                               options->flags |= PARSE_OPT_VIA_CLI;
> +               }
> +               return ret;
>         }
> [...]
> +
> +       /* The loop above is driven by the argument vector, so we need
> +        * to make a second pass and find those options that are
> +        * configurable, and haven't been set via the command-line */
> +       for (; options->type != OPTION_END; options++) {
> +               if (!(options->flags & PARSE_OPT_CONFIGURABLE))
> +                       continue;
> +
> +               if (options->flags & PARSE_OPT_VIA_CLI)
> +                       continue;
> +
> +               /* TODO: Maybe factor the handling of OPTION_CALLBACK
> +                * in get_value() into a function.
> +                *
> +                * Do we also need to save away the state from the
> +                * loop above to handle unset? I think not, I think
> +                * we're always unset here by definition, right?
> +                */
> +               return (*options->conf_callback)(options, NULL, 1) ? (-1) : 0;
> +       }
> +
> [...]

After looking at some of the internal APIs I'm thinking of replacing this pattern with a hashmap.c hashmap where the keys are a sprintf("%d:%s", short_name, long_name) to uniquely identify the option. There's no other obvious way to uniquely address an option. I guess I could just equivalently and more cheaply use the memory address of each option to identify them, but that seems a bit hacky.

Show 8 quoted lines
> @@ -110,6 +112,9 @@ struct option {
>         int flags;
>         parse_opt_cb *callback;
>         intptr_t defval;
> +
> +       const char *conf_key;
> +       parse_opt_cb *conf_callback;
>  };

I've already found that this needs to be a char **conf_key, since several command-line options have multiple ways to spell the option name, e.g. add.ignoreerrors & add.ignore-errors, pack.writebitmaps & repack.writebitmaps etc.

Previous: Ævar Arnfjörð BjarmasonNext: Jeff King
Message 13 of 19 in “Add configuration options for some commonly used command-line options (Was: [RFH] GSoC 2015 application)”
  1. Duy NguyenMar 19, 2017
  2. Matthieu MoyMar 19, 2017
  3. brian m. carlsonMar 19, 2017
  4. Ævar Arnfjörð BjarmasonMar 19, 2017
  5. Duy NguyenMar 20, 2017
  6. Brandon WilliamsMar 20, 2017
  7. Jeff KingMar 20, 2017
  8. Brandon McCaigMar 31, 2017
  9. Junio C HamanoMar 20, 2017
  10. Jeff KingMar 20, 2017
  11. Ævar Arnfjörð BjarmasonMar 20, 2017
  12. parse-options: add facility to make options configurableÆvar Arnfjörð Bjarmason, Mar 24, 2017
  13. Ævar Arnfjörð BjarmasonMar 25, 2017
  14. Jeff KingMar 25, 2017
  15. Ævar Arnfjörð BjarmasonMar 25, 2017
  16. Jeff KingMar 28, 2017
  17. WIP configurable options facilityÆvar Arnfjörð Bjarmason, Mar 28, 2017
  18. brian m. carlsonMar 25, 2017
  19. Duy NguyenMar 20, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.