aboutsummaryrefslogtreecommitdiff
path: root/tests/getopt.test
blob: 4c694581a3a15bb455a738a2e46aad7d2144eb18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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" "" ""