-
Notifications
You must be signed in to change notification settings - Fork 381
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(set): more featureful completion
Complete option flags, with plus and dash prefixes, setopt values only after `-o`/`+o`, allow filename completions if not preceded by `-` or `--`. Closes #103
- Loading branch information
Showing
4 changed files
with
72 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -398,6 +398,7 @@ bashcomp_DATA = 2to3 \ | |
screen \ | ||
scrub \ | ||
secret-tool \ | ||
set \ | ||
sh \ | ||
sha256sum \ | ||
shellcheck \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# bash completion for set -*- shell-script -*- | ||
|
||
_comp_cmd_set() | ||
{ | ||
local cur prev words cword comp_args | ||
_comp_initialize -- "$@" || return | ||
|
||
local noargopts='!(-*|*[o]*)' | ||
|
||
# shellcheck disable=SC2254 | ||
case "$prev" in | ||
[+-]${noargopts}o) | ||
_comp_compgen -- -A setopt | ||
return | ||
;; | ||
esac | ||
|
||
local i want_options=set | ||
for ((i = 1; i < cword; i++)); do | ||
if [[ ${words[i]} == -?(-) ]]; then | ||
want_options="" | ||
break | ||
fi | ||
done | ||
|
||
if [[ $want_options && $cur == [+-]* ]]; then | ||
local has_plus="" | ||
if [[ $cur == +?([a-zA-Z]) ]]; then | ||
has_plus=set | ||
cur=${cur/#+/-} | ||
fi | ||
_comp_compgen_usage | ||
if [[ $has_plus ]]; then | ||
local i | ||
for i in "${!COMPREPLY[@]}"; do | ||
if [[ ${COMPREPLY[i]} == -- ]]; then | ||
unset -v 'COMPREPLY[i]' | ||
else | ||
COMPREPLY[i]=${COMPREPLY[i]/#-/+} | ||
fi | ||
done | ||
fi | ||
return | ||
fi | ||
|
||
_comp_compgen_filedir | ||
} && | ||
complete -F _comp_cmd_set set | ||
|
||
# ex: filetype=sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,26 @@ | ||
import pytest | ||
|
||
from conftest import assert_complete | ||
|
||
|
||
class TestSet: | ||
@pytest.mark.complete("set no") | ||
def test_1(self, completion): | ||
@pytest.mark.parametrize("dash", ["", "-", "--"]) | ||
def test_basic(self, bash, dash): | ||
completion = assert_complete(bash, f"set {dash} ") | ||
assert completion | ||
|
||
@pytest.mark.parametrize("prefix", ["-", "+"]) | ||
def test_options(self, bash, prefix): | ||
completion = assert_complete(bash, f"set {prefix}") | ||
assert f"{prefix}o" in completion | ||
assert "+-" not in completion | ||
|
||
@pytest.mark.parametrize("prefix", ["-", "+"]) | ||
def test_o_args(self, bash, prefix): | ||
completion = assert_complete(bash, f"set {prefix}o ") | ||
assert any(x.startswith("no") for x in completion) | ||
|
||
@pytest.mark.parametrize("dash,prefix", [["-", "--"], ["-", "+"]]) | ||
def test_options_after_dash_or_dashdash(self, bash, dash, prefix): | ||
completion = assert_complete(bash, f"set {dash} {prefix}") | ||
assert not completion |