Skip to content
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

Add completion for BSD chflags(1) command #1065

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions completions/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ bashcomp_DATA = 2to3 \
check_db \
check_perms \
checksec \
chflags \
_chfn \
chgrp \
chkconfig \
Expand Down
52 changes: 52 additions & 0 deletions completions/chflags
akinomyoga marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# chflags(1) completion -*- shell-script -*-

[[ $OSTYPE == *@(bsd|darwin)* ]] || return 1

# References
#
# [1] FreeBSD - https://man.freebsd.org/cgi/man.cgi?chflags(1)
# [2] NetBSD - https://man.netbsd.org/NetBSD-9.0/chflags.1
# [3] OpenBSD - https://man.openbsd.org/chflags.1

_comp_cmd_chflags()
{
local cur prev words cword comp_args
_comp_initialize -- "$@" || return

if [[ $cur == -* ]]; then
# Complete -options
local w opts=""
for w in "${words[@]}"; do
[[ $w == -R ]] && opts="-H -L -P" && break
done
[[ $OSTYPE == *freebsd* ]] && opts="$opts -x"
_comp_compgen -- -W '-f -h -v -R $opts'
Copy link
Owner

Choose a reason for hiding this comment

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

FreeBSD (15.0) has -x.

Suggested change
_comp_compgen -- -W '-f -h -v -R $opts'
_comp_compgen -- -W '-f -h -v -x -R $opts'

Copy link
Collaborator

Choose a reason for hiding this comment

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

If -x is not available in other BSDs, I think we should reference $OSTYPE for -x the same as for the first word.

Copy link
Collaborator

Choose a reason for hiding this comment

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

else
local REPLY
# The first argument is a list of flags; the rest are filedir.
_comp_count_args
if ((REPLY == 1)); then
case "$OSTYPE" in
*netbsd*)
_comp_delimited , -W '
arch opaque nodump sappnd schg uappnd uchg'
;;
*openbsd*)
_comp_delimited , -W 'arch nodump sappnd schg uappnd uchg'
;;
*)
_comp_delimited , -W '
simmutable nosimmutable sappend nosappend archived
noarchived sunlink nosunlink opaque noopaque nodump
dump uimmutable nouimmutable uappend nouappend hidden
nohidden uunlink nouunlink'
;;
esac
else
_comp_compgen_filedir
fi
fi
} &&
complete -F _comp_cmd_chflags chflags

# ex: filetype=sh
1 change: 1 addition & 0 deletions test/t/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ EXTRA_DIST = \
test_check_db.py \
test_check_perms.py \
test_checksec.py \
test_chflags.py \
test_chfn.py \
test_chgrp.py \
test_chkconfig.py \
Expand Down
19 changes: 19 additions & 0 deletions test/t/test_chflags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pytest


class TestChflags:
@pytest.mark.complete("chflags no")
def test_basic(self, completion):
assert completion

@pytest.mark.complete("chflags -")
def test_options(self, completion):
assert completion and "-P" not in completion

@pytest.mark.complete("chflags -R -")
def test_options_after_R(self, completion):
assert "-P" in completion

@pytest.mark.complete("chflags -v sappend ")
def test_first_word(self, completion):
assert completion