Skip to content

Commit

Permalink
fix(tar): Complete added files with long opts
Browse files Browse the repository at this point in the history
Currently when using long options, we don't complete files to add to the
archive, but show options. Handle long options in tar_mode to fix this.

Fixes #98
  • Loading branch information
yedayak committed Jun 24, 2024
1 parent e209afb commit c94bebb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
16 changes: 8 additions & 8 deletions completions/tar
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,11 @@ _comp_cmd_tar__preparse_cmdline()

for i in "$@"; do
case "$i" in
--delete | --test-label)
--delete | --test-label | --catenate | --concatenate | --extract | --get | --update | --list | --append | --create)
tar_mode=${i:2:100}
tar_mode_arg=$i
# FIXME: We don't set $tar_mode_arg since it's used for combined
# single letter options, but that means we don't handle
# compression formats.
break
;;
--*)
Expand Down Expand Up @@ -236,7 +238,7 @@ _comp_cmd_tar__file_option()
local ext="$1"

case "$tar_mode" in
c)
c | create)
# no need to advise user to re-write existing tarball
_comp_compgen_filedir -d
;;
Expand Down Expand Up @@ -391,7 +393,7 @@ _comp_cmd_tar__adjust_PREV_from_old_option()
_comp_cmd_tar__extract_like_mode()
{
local i
for i in x d t delete; do
for i in x d t delete extract get list; do
[[ $tar_mode == "$i" ]] && return 0
done
return 1
Expand Down Expand Up @@ -455,7 +457,7 @@ _comp_cmd_tar__detect_ext()

case "$tar_mode_arg" in
--*)
# Should never happen?
# FIXME: get correct extensions for long options (gnu style)
;;
?(-)*[cr]*f)
ext='@(tar|gem|spkg|cbt|xpbs)'
Expand Down Expand Up @@ -490,7 +492,7 @@ _comp_cmd_tar__gnu()
tar_mode tar_mode_arg old_opt_progress="" \
old_opt_used="" old_opt_parsed=()

# Main mode, e.g. -x or -c (extract/creation)
# Main mode, e.g. "x" or "c" or the long forms "extract" or "create"
local tar_mode=none

# The mode argument, e.g. -cpf or -c
Expand Down Expand Up @@ -685,8 +687,6 @@ _comp_cmd_tar__posix()

_comp_initialize -s -- "$@" || return

tar_mode=none

# relatively compatible modes are {c,t,x}
# relatively compatible options {b,f,m,v,w}
short_arg_req="fb"
Expand Down
5 changes: 5 additions & 0 deletions test/t/test_tar.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,9 @@ def test_22(self, bash, completion):
"""Test listing escaped chars in old option."""
assert completion == "c/"

# Test long options (gnu style)
@pytest.mark.complete("tar --create --file created.tar ", cwd="tar")
def test_23(self, completion):
assert completion == "archive.tar.xz dir/ dir2/ escape.tar".split()

# TODO: "tar tf escape.tar a/b"

0 comments on commit c94bebb

Please sign in to comment.