Skip to content

Commit 0cde4ca

Browse files
committed
feat: add filterflags function
1 parent faf4817 commit 0cde4ca

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

library.sh

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,15 @@ function nfirstargs() {
5555
}
5656

5757
#
58-
# Filter the specified number of options and flags
59-
# Example:
60-
# filteropts 2 -version --dry-run foo bar baz
58+
# Filter the specified number of options.
59+
# Examples:
60+
# filteropts 2 -repo -user foo bar baz
61+
# -> foo bar baz
62+
#
63+
# filteropts 2 -repo -user -repo=shellib foo bar baz
64+
# -> foo bar baz
65+
#
66+
# filteropts 2 -repo -user -user=iocanel foo bar baz
6167
# -> foo bar baz
6268
function filteropts() {
6369
num=$1
@@ -69,6 +75,7 @@ function filteropts() {
6975
next=false
7076
#Loop through arguments starting from skip
7177
for candidate in "${@:$skip}"; do
78+
#If the previous item is the option, then this is the value. So let's skip it.
7279
if $next; then
7380
next=false
7481
continue;
@@ -87,3 +94,29 @@ function filteropts() {
8794
done
8895
fi
8996
}
97+
#
98+
# Filter the specified number of flags.
99+
# Example:
100+
# filterflags 2 -version -dry-run foo bar baz
101+
# -> foo bar baz
102+
function filteropts() {
103+
num=$1
104+
if echo $num | grep '[^0-9]' > /dev/null; then
105+
echo "$@"
106+
else
107+
skip=$(($num+2))
108+
tofilter=`nfirstargs $@`
109+
#Loop through arguments starting from skip
110+
for candidate in "${@:$skip}"; do
111+
#Loop through items that need to be skiped and see if candidate is included
112+
for i in $tofilter; do
113+
if [ $candidate = "$i" ]; then
114+
next=true
115+
break;
116+
else
117+
echo $candidate
118+
fi
119+
done
120+
done
121+
fi
122+
}

0 commit comments

Comments
 (0)