Skip to content

Complete stdin "-" in file locations, add more options that allow it #1237

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 2 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions completions/curl
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ _comp_cmd_curl()
--sasl-authzid | --service-name | --socks5-gssapi-service | \
--speed-limit | --speed-time | --telnet-option | --tftp-blksize | \
--time-cond | --tls13-ciphers | --tlspassword | --tlsuser | \
--url | --user | --user-agent | --version | --write-out | \
-${noargopts}[CFmQreYytzuAVw])
--url | --user | --user-agent | --version | \
-${noargopts}[CFmQreYytzuAV])
return
;;
--cacert | --cert | --proxy-cacert | --proxy-cert | -${noargopts}E)
Expand All @@ -50,10 +50,11 @@ _comp_cmd_curl()
_comp_compgen_filedir crl
return
;;
--data | --data-ascii | --data-binary | --data-urlencode | --header | \
--proxy-header | -${noargopts}[dH])
--data | --data-ascii | --data-binary | --data-urlencode | --json | \
--header | --proxy-header | --write-out | -${noargopts}[dHw])
if [[ $cur == \@* ]]; then
_comp_compgen -c "${cur:1}" filedir
_comp_compgen -a -c "${cur:1}" -- -W '-'
Copy link
Owner

Choose a reason for hiding this comment

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

No strong opinions, but I suppose we could simplify/optimize this to the following unless we see downsides to doing that? WDYT, and @akinomyoga too?

Suggested change
_comp_compgen -a -c "${cur:1}" -- -W '-'
[[ $cur == @?(-) ]] && COMPREPLY+=(-)

Copy link
Collaborator

@akinomyoga akinomyoga Aug 11, 2024

Choose a reason for hiding this comment

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

Hmm, although it is functionally equivalent, I prefer using _comp_compgen which is more idiomatic.

It takes time for me to understand that [[ $cur == @?(-) ]] && COMPREPLY+=(-) is functionally equivalent to _comp_compgen -ac "${cur:1}" -- -W '-' and doesn't have a bug nor an extra intentional effect.

Copy link
Collaborator

Choose a reason for hiding this comment

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

There seem to be both types of existing cases:

./completions/7z:59:            _comp_compgen -c "${cur:4}" -- -P"${cur:0:4}" -W '-'
./completions/curl:144:            _comp_compgen -- -W '-'
./completions/gpgv:35:                _comp_compgen -- -W '-'
./completions/iperf:55:            _comp_compgen -- -W 'C'
./completions/pack200:35:            _comp_compgen -- -W '-'
./completions/pdftotext:32:            _comp_compgen -- -W '-'
./completions/puppet:160:                    _comp_compgen -- -W '-'
./completions/tshark:97:            _comp_compgen -- -W 'n'
./completions/unpack200:19:            _comp_compgen -- -W '-'
./completions/cppcheck:38:            [[ ! $cur || $cur == - ]] && COMPREPLY+=(-)
./completions/mr:46:                    COMPREPLY+=(-)
./completions/nslookup:59:        [[ $REPLY -eq 1 && $cur == @(|-) ]] && COMPREPLY+=(-)
./completions/patch:24:            [[ ! $cur || $cur == - ]] && COMPREPLY=(-)
./completions/tshark:67:            [[ $cur == @(|-) ]] && COMPREPLY+=(-)
./completions/wget:78:            _comp_compgen_filedir && [[ $cur == - || ! $cur ]] && COMPREPLY+=(-)

Copy link
Owner

Choose a reason for hiding this comment

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

For me it's the opposite, I'm far from being "natural" with the stuff we've added over the few last releases and have to look up stuff a lot, almost certainly because I haven't been writing all that much code in this project lately, whereas straight bash is much easier for me. But I'm fine with those who are doing most of the actual work these days getting to decide the flavor. Would not hurt to be consistent with it though through the whole tree.

Pre-approved either way you guys decide.

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 don't really mind either, I think the second option is easier to read without context, but the first one makes more sense here since it follows the same pattern as the line before it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I again thought about it, but I think it is still better to generate completions through _comp_compgen even in simple cases because it would be easier to apply changes later in a consistent way (by modifying the behavior of _comp_compgen or by grep'ping the calls of _comp_compgen).

if [[ ${#COMPREPLY[@]} -eq 1 && -d ${COMPREPLY[0]} ]]; then
COMPREPLY[0]+=/
compopt -o nospace
Expand Down
3 changes: 2 additions & 1 deletion test/t/test_curl.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def test_4(self, completion):

@pytest.mark.complete("curl --data @", cwd="shared/default/foo.d")
def test_data_atfile(self, completion):
assert completion == "foo"
assert "@foo" in completion
assert "@-" in completion

@pytest.mark.complete("curl --data @foo.", cwd="shared/default")
def test_data_atfile_dir(self, completion):
Expand Down