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

Re: [PATCH v2 0/8] git-submodule.sh: improve parsing of options

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 9, 2024, 23:26 UTC
Message-ID
<xmqq1pygo2ti.fsf@gitster.g>
In-Reply-To
<20241209165009.40653-1-royeldar0@gmail.com>
Roy Eldar <royeldar0@gmail.com> writes:
Show 14 quoted lines
> When we run "git submodule", the script parses the various options and
> then invokes "git-submodule--helper". Unlike most builtin git commands
> which parse short/long options using parse-options.c, the parsing of
> arguments is completely done within git-submodule.sh; therefore, there
> are some inconsistencies with the rest of the commands, in particular
> the parsing of option arguments given to various options.
>
> Improve the handling of option arguments for both long & short options;
> for example, passing flags such as "--branch=master" or "-j8" now works.
>
> Changes since v1:
>
> - Make variable values always contain the option name.
> - Rename a couple of variables in order to improve consistency.

After reading this, it was confusing to see [1/8] still doing "1 or empty" boolean, only to be further modified in [7/8]. We prefer to see a single series stumbling in the middle and changing the course.

Just a tangent.

While a simple wrapper script is generally easier to debug and read if $verbose variable's value is "--verbose" or "", there is a case where following this pattern is not a good idea. If an option we are eventually going to give to the underlying command takes a value, the value can contain whitespace, and the option and its value need to be passed as two separate arguments, it is less error prone to use the "variable only contains the value" approach.

Imagine that submodule--helper takes a "--foo" option with a greeting message like "hello world" in such a way. We'd want to trigger it this way:

	git submodule--helper --foo "hello world"

as we are assuming that for some reason we need to pass them as two words, and

	git submodule--helper --foo="hello world"

is not an option. In such a case, a wrapper script that takes such an optional parameter in $foo is easier to write like so

	# parse
	foo=
	while ...
	do
                case "$opt" in
                --foo=*)	foo="${1#--foo=}" ;;
                --foo)		foo=${2?"--foo without value"};	shift ;;
		...
		esac
		shift
	done
	# interpolate
	git submodule--helper ${foo:+--foo "$foo"}

in order to avoid the value given to the option split at $IFS whitespace. With foo='--foo="hello world"', passing it to the underlying command would involve use of eval and becomes error prone.

I am assuming (but I don't use "git submodule" very often, so my assumption may be way off) that there is no such variable we need to pass, but if not, we may need to reconsider and use the "variable has only value of the option" for at least some of them.

Show 16 quoted lines
> Link to v1:
>
> https://lore.kernel.org/git/20241207135201.2536-1-royeldar0@gmail.com
>
> Roy Eldar (8):
>   git-submodule.sh: make some variables boolean
>   git-submodule.sh: improve parsing of some long options
>   git-submodule.sh: improve parsing of short options
>   git-submodule.sh: get rid of isnumber
>   git-submodule.sh: get rid of unused variable
>   git-submodule.sh: add some comments
>   git-submodule.sh: improve variables readability
>   git-submodule.sh: rename some variables
>
>  git-submodule.sh | 214 +++++++++++++++++++++++------------------------
>  1 file changed, 104 insertions(+), 110 deletions(-)
Previous: Roy EldarNext: Junio C Hamano
Message 18 of 43 in “git-submodule.sh: improve parsing of options”
  1. 0/3 git-submodule.sh: improve parsing of optionsRoy Eldar, Dec 7, 2024
  2. 1/3 git-submodule.sh: make some variables booleanRoy Eldar, Dec 7, 2024
  3. Junio C HamanoDec 7, 2024
  4. Eric SunshineDec 8, 2024
  5. 2/3 git-submodule.sh: improve parsing of some long optionsRoy Eldar, Dec 7, 2024
  6. 3/3 git-submodule.sh: improve parsing of short optionsRoy Eldar, Dec 7, 2024
  7. Junio C HamanoDec 8, 2024
  8. Roy EDec 9, 2024
  9. 0/8 git-submodule.sh: improve parsing of optionsRoy Eldar, Dec 9, 2024
  10. 1/8 git-submodule.sh: make some variables booleanRoy Eldar, Dec 9, 2024
  11. 2/8 git-submodule.sh: improve parsing of some long optionsRoy Eldar, Dec 9, 2024
  12. 3/8 git-submodule.sh: improve parsing of short optionsRoy Eldar, Dec 9, 2024
  13. 4/8 git-submodule.sh: get rid of isnumberRoy Eldar, Dec 9, 2024
  14. 5/8 git-submodule.sh: get rid of unused variableRoy Eldar, Dec 9, 2024
  15. 6/8 git-submodule.sh: add some commentsRoy Eldar, Dec 9, 2024
  16. 7/8 git-submodule.sh: improve variables readabilityRoy Eldar, Dec 9, 2024
  17. 8/8 git-submodule.sh: rename some variablesRoy Eldar, Dec 9, 2024
  18. Junio C HamanoDec 9, 2024
  19. Junio C HamanoDec 10, 2024
  20. Roy EDec 10, 2024
  21. Junio C HamanoDec 11, 2024
  22. Roy EDec 11, 2024
  23. Junio C HamanoDec 11, 2024
  24. 0/7 git-submodule.sh: improve parsing of optionsRoy Eldar, Dec 10, 2024
  25. 1/7 git-submodule.sh: improve parsing of some long optionsRoy Eldar, Dec 10, 2024
  26. 2/7 git-submodule.sh: improve parsing of short optionsRoy Eldar, Dec 10, 2024
  27. 3/7 git-submodule.sh: get rid of isnumberRoy Eldar, Dec 10, 2024
  28. 4/7 git-submodule.sh: get rid of unused variableRoy Eldar, Dec 10, 2024
  29. 5/7 git-submodule.sh: add some commentsRoy Eldar, Dec 10, 2024
  30. 6/7 git-submodule.sh: improve variables readabilityRoy Eldar, Dec 10, 2024
  31. Junio C HamanoDec 11, 2024
  32. Roy EDec 11, 2024
  33. Đoàn Trần Công DanhDec 11, 2024
  34. Junio C HamanoDec 11, 2024
  35. 7/7 git-submodule.sh: rename some variablesRoy Eldar, Dec 10, 2024
  36. 0/7 git-submodule.sh: improve parsing of optionsRoy Eldar, Dec 11, 2024
  37. 1/7 git-submodule.sh: improve parsing of some long optionsRoy Eldar, Dec 11, 2024
  38. 2/7 git-submodule.sh: improve parsing of short optionsRoy Eldar, Dec 11, 2024
  39. 3/7 git-submodule.sh: get rid of isnumberRoy Eldar, Dec 11, 2024
  40. 4/7 git-submodule.sh: get rid of unused variableRoy Eldar, Dec 11, 2024
  41. 5/7 git-submodule.sh: add some commentsRoy Eldar, Dec 11, 2024
  42. 6/7 git-submodule.sh: improve variables readabilityRoy Eldar, Dec 11, 2024
  43. 7/7 git-submodule.sh: rename some variablesRoy Eldar, Dec 11, 2024

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.