From c77b66455762f42bb824c1aa8cc60e7f4d44bdab Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Thu, 21 Nov 2019 14:09:23 -0800 Subject: Add getopt(1). Includes new tests. --- tests/getopt.test | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100755 tests/getopt.test (limited to 'tests') diff --git a/tests/getopt.test b/tests/getopt.test new file mode 100755 index 00000000..4c694581 --- /dev/null +++ b/tests/getopt.test @@ -0,0 +1,44 @@ +#!/bin/bash + +[ -f testing.sh ] && . testing.sh + +#testing "name" "command" "result" "infile" "stdin" + +function test_getopt() { + testcmd "$1" "$1" "$2\n" "" "" +} + +# Traditional behavior was to take the first argument as OPTSTR and not quote. +test_getopt "a b c" " -- b c" +test_getopt "a -a b c" " -a -- b c" +test_getopt "a -- -a b c" " -- -a b c" + +# Modern -o mode. +test_getopt "-o a -- " " --" +test_getopt "-o a -- -a b c" " -a -- 'b' 'c'" +test_getopt "-o a: -- -a b c" " -a 'b' -- 'c'" + +# Long options (--like --this). +test_getopt "-o a -l long -- -a --long a" " -a --long -- 'a'" +test_getopt "-o a -l one -l two -- -a --one --two" " -a --one --two --" +test_getopt "-o a -l one,two -- -a --one --two" " -a --one --two --" +# -l arg: (required) +test_getopt "-o a -l one: -- -a --one arg" " -a --one 'arg' --" +# -l arg:: (optional) +test_getopt "-o a -l one:: -- -a --one" " -a --one '' --" +test_getopt "-o a -l one:: -- -a --one arg" " -a --one '' -- 'arg'" +test_getopt "-o a -l one:: -- -a --one=arg" " -a --one 'arg' --" + +# "Alternative" long options (-like -this but also --like --this). +test_getopt "-o a -a -l long -- -long --long a" " --long --long -- 'a'" + +# -u lets you avoid quoting even with modern -o. +test_getopt "-u -o a: -- -a b c" " -a b -- c" + +# Do we quote quotes right? +test_getopt "-o a -- \"it\'s\"" " -- 'it\'\''s'" +test_getopt "-o a -u -- \"it\'s\"" " -- it\'s" + +# Odds and ends. +testcmd "-T" "-T ; echo \$?" "4\n" "" "" +testcmd "-n" "-n unlikely a -x 2>&1 | grep -o unlikely:" "unlikely:\n" "" "" -- cgit v1.2.3