1#!/bin/bash 2 3[ -f testing.sh ] && . testing.sh 4 5#testing "name" "command" "result" "infile" "stdin" 6 7function test_getopt() { 8 testcmd "$1" "$1" "$2\n" "" "" 9} 10 11# Traditional behavior was to take the first argument as OPTSTR and not quote. 12test_getopt "a b c" " -- b c" 13test_getopt "a -a b c" " -a -- b c" 14test_getopt "a -- -a b c" " -- -a b c" 15 16# Modern -o mode. 17test_getopt "-o a -- " " --" 18test_getopt "-o a -- -a b c" " -a -- 'b' 'c'" 19test_getopt "-o a: -- -a b c" " -a 'b' -- 'c'" 20 21# Long options (--like --this). 22test_getopt "-o a -l long -- -a --long a" " -a --long -- 'a'" 23test_getopt "-o a -l one -l two -- -a --one --two" " -a --one --two --" 24test_getopt "-o a -l one,two -- -a --one --two" " -a --one --two --" 25# -l arg: (required) 26test_getopt "-o a -l one: -- -a --one arg" " -a --one 'arg' --" 27# -l arg:: (optional) 28test_getopt "-o a -l one:: -- -a --one" " -a --one '' --" 29test_getopt "-o a -l one:: -- -a --one arg" " -a --one '' -- 'arg'" 30test_getopt "-o a -l one:: -- -a --one=arg" " -a --one 'arg' --" 31 32# "Alternative" long options (-like -this but also --like --this). 33test_getopt "-o a -a -l long -- -long --long a" " --long --long -- 'a'" 34 35# -u lets you avoid quoting even with modern -o. 36test_getopt "-u -o a: -- -a b c" " -a b -- c" 37 38# Do we quote quotes right? 39test_getopt "-o a -- \"it\'s\"" " -- 'it\'\''s'" 40test_getopt "-o a -u -- \"it\'s\"" " -- it\'s" 41 42# Odds and ends. 43testcmd "-T" "-T ; echo \$?" "4\n" "" "" 44testcmd "-n" "-n unlikely a -x 2>&1 | grep -o unlikely:" "unlikely:\n" "" "" 45