Skip to content

refactor: _parse_{help,usage} => _comp_compgen_help #954

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 8, 2023

Conversation

akinomyoga
Copy link
Collaborator

This is based on #952 so would be kept to be Draft until PR #952 settles.

The own changes of this PR are 2ce600f...39b15f3. This PR includes the API change of _parse_{help,usage} and trivial rewriting for switching from _parse_{help,usage}. Other non-trivial rewriting related to _parse_{help,usage} will be included in later PRs.

@akinomyoga akinomyoga force-pushed the _parse_help-1 branch 3 times, most recently from d6f8402 to c133b1c Compare May 5, 2023 23:47
@akinomyoga
Copy link
Collaborator Author

8a203c1 is automatically generated by running sed and checked afterward by eyes.

8a203c1e
sed -E "s/$path/$rep/g"

with

pat='COMPREPLY=\(\$\(compgen -W '\''\$\(_parse_help ([^()|]+)\)'\'' -- "\$cur"\)\)'
rep='_comp_compgen_help \1'
pat='COMPREPLY=\(\$\(compgen -W '\''\$\(_parse_usage ([^()|]+)\)'\'' -- "\$cur"\)\)'
rep='_comp_compgen_help -u \1'
pat='COMPREPLY\+=\(\$\(compgen -W '\''\$\(_parse_help ([^()|]+)\)'\'' -- "\$cur"\)\)'
rep='_comp_compgen -a help \1'
pat='COMPREPLY\+=\(\$\(compgen -W '\''\$\(_parse_usage ([^()|]+)\)'\'' -- "\$cur"\)\)'
rep='_comp_compgen -a help -u \1'
pat='([[:alnum:]_]{1,})=\(\$\(compgen -W '\''\$\(_parse_help ([^()|]+)\)'\'' -- "\$cur"\)\)'
rep='_comp_compgen_help -v \1 \2'
pat='([[:alnum:]_]{1,})=\(\$\(compgen -W '\''\$\(_parse_usage ([^()|]+)\)'\'' -- "\$cur"\)\)'
rep='_comp_compgen_help -uv \1 \2'
pat='([[:alnum:]_]{1,})\+=\(\$\(compgen -W '\''\$\(_parse_help ([^()|]+)\)'\'' -- "\$cur"\)\)'
rep='_comp_compgen_help -av \1 \2'
pat='([[:alnum:]_]{1,})\+=\(\$\(compgen -W '\''\$\(_parse_usage ([^()|]+)\)'\'' -- "\$cur"\)\)'
rep='_comp_compgen_help -uav \1 \2'
pat='COMPREPLY=\(\$\(_parse_help ([^()|]+)\)\)'
rep='_comp_compgen -R help \1'
pat='COMPREPLY=\(\$\(_parse_usage ([^()|]+)\)\)'
rep='_comp_compgen -R help -u \1'
pat='COMPREPLY\+=\(\$\(_parse_help ([^()|]+)\)\)'
rep='_comp_compgen -aR help \1'
pat='COMPREPLY\+=\(\$\(_parse_usage ([^()|]+)\)\)'
rep='_comp_compgen -aR help -u \1'
pat='^([[:space:]]*)local ([[:alnum:]_]{1,})=\(\$\(_parse_help ([^()|]+)\)\)'
rep='\1local -a \2\n\1_comp_compgen -Rv \2 help \3'
pat='^([[:space:]]*)local ([[:alnum:]_]{1,})=\(\$\(_parse_usage ([^()|]+)\)\)'
rep='\1local -a \2\1_comp_compgen -Rv \2 help -u \3'
pat='^([[:space:]]*)local ([[:alnum:]_]{1,})\+=\(\$\(_parse_help ([^()|]+)\)\)'
rep='\1local -a \2\n\1_comp_compgen -Rv \2 help -a \3'
pat='^([[:space:]]*)local ([[:alnum:]_]{1,})\+=\(\$\(_parse_usage ([^()|]+)\)\)'
rep='\1local -a \2\n\1_comp_compgen -aRv \2 help -u \3'
pat='([[:alnum:]_]{1,})=\(\$\(_parse_help ([^()|]+)\)\)'
rep='_comp_compgen -Rv \1 help \2'
pat='([[:alnum:]_]{1,})=\(\$\(_parse_usage ([^()|]+)\)\)'
rep='_comp_compgen -Rv \1 help -u \2'
pat='([[:alnum:]_]{1,})\+=\(\$\(_parse_help ([^()|]+)\)\)'
rep='_comp_compgen -aRv \1 help \2'
pat='([[:alnum:]_]{1,})\+=\(\$\(_parse_usage ([^()|]+)\)\)'
rep='_comp_compgen -aRv \1 help -u \2'

56d55ac is searched by grep and then manually replaced.

@akinomyoga akinomyoga marked this pull request as ready for review May 6, 2023 00:15
bash_completion Outdated
# -u Parse the BSD style usage output. The default is in the GNU
# style help output.
# @param $1 command command; if "-", read from stdin and ignore rest of args
# @param $2 opt command options (default: --help)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if passing everything after a -- to the command would be a cleaner and more consistent API for this? For example _comp_compgen uses that approach.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also thought about it, but as far as the command name $1 does not start with -, we can actually omit --. The difference in _comp_compgen is that the arguments to compgen always starts with -. But I think it is good to prefix -- before every "$1".

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, maybe it is better to split the function into two separate functions _comp_compgen_help and _comp_compgen_usage.

Before introducing the idea _comp_compgen NAME, I originally combined _parse_help and _parse_usage in a single function because it shared long codes of the command-line option parsing. But now the option parsing is moved to _comp_compgen, so two processings help and usage now share only a small part of the code. I'll think about it again.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I split the function into _comp_compgen_help and _comp_compgen_usage.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the split better. Perhaps still provide command options as $2... instead of cramming them into single $2 string, for a bit cleaner call site code and avoiding a _comp_split?

Then again, I suppose we could make the first parameter optional too, and default it to ${comp_args[0]}. If so, usage like [-c command] [--] [command_opts...] could be in order.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then again, I suppose we could make the first parameter optional too, and default it to ${comp_args[0]}. If so, usage like [-c command] [--] [command_opts...] could be in order.

I like this idea. Let me implement it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After thinking about it again, I have slightly changed the suggested command line usage:

usage: _comp_compgen_help -
usage: _comp_compgen_help -c cmd args...
usage: _comp_compgen_help [[--] args...]
  • When we want to read stdin, requiring to write _comp_compgen_help -c - seems strange, so _comp_compgen_help - is supported separately.
  • When the caller specifies the command and arguments, splitting between $1 and $2... with -- as _comp_compgen_help -c cmd -- args also seems not so natural. So when the first argument is -c, the later words are all treated as the command and arguments as is.
  • _comp_dequote is only applied to comp_args[0]. The command name specified through -c cmd args... is not de-quoted by _comp_dequote. When it is needed, the caller should take care of it.
  • --help (or --usage in the case of _comp_compgen_usage) is only supplemented when no arguments are specified. E.g., when the user uses _comp_compgen_help --, it doesn't specify --help but directly calls comp_args[0] without arguments.

I rebased and modified existing commits. There are additional commits:

  • There are additional fixes for localvar_inherit: 0a87c15
  • The backslash in -\? in ssh-addwas removed: 01cfc44
  • These are non-trivial changes (where originally, a command name different from "$1" or multiple words in $2 are supplied): fbe62ba

@akinomyoga akinomyoga force-pushed the _parse_help-1 branch 3 times, most recently from 6f22632 to 9b1b5d9 Compare May 7, 2023 02:13
Copy link
Owner

@scop scop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could clean up some redundant passing of --help while at it.

bash_completion Outdated
# -u Parse the BSD style usage output. The default is in the GNU
# style help output.
# @param $1 command command; if "-", read from stdin and ignore rest of args
# @param $2 opt command options (default: --help)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the split better. Perhaps still provide command options as $2... instead of cramming them into single $2 string, for a bit cleaner call site code and avoiding a _comp_split?

Then again, I suppose we could make the first parameter optional too, and default it to ${comp_args[0]}. If so, usage like [-c command] [--] [command_opts...] could be in order.

akinomyoga and others added 4 commits May 8, 2023 02:19
We also remove the backslash of '-\?' within this patch.  The
backslash was introduced in commit 60b8fab, which seems to have tried
to work around the failglob failure.  Because we now directly specify
the arguments to _comp_compgen_help so that we do not have to evaluate
unquoted $2, we do not need to add the backslash anymore.  Note that
the backslash added to ssh-keygen in 60b8fab was removed in commit
4dc0016 where _parse_help was switched to _parse_usage.

References:

scop@60b8fab
scop@4dc0016
Copy link
Owner

@scop scop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, pre-approved with some comments addressed.

@scop scop merged commit b87010c into scop:master May 8, 2023
@akinomyoga akinomyoga deleted the _parse_help-1 branch May 8, 2023 05:53
akinomyoga added a commit to akinomyoga/bash-completion that referenced this pull request May 8, 2023
akinomyoga added a commit to akinomyoga/bash-completion that referenced this pull request May 10, 2023
akinomyoga added a commit to akinomyoga/bash-completion that referenced this pull request May 21, 2023
akinomyoga added a commit to akinomyoga/bash-completion that referenced this pull request May 21, 2023
akinomyoga added a commit to akinomyoga/bash-completion that referenced this pull request May 26, 2023
akinomyoga added a commit to akinomyoga/bash-completion that referenced this pull request May 28, 2023
akinomyoga added a commit to akinomyoga/bash-completion that referenced this pull request May 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants