getopt.test 1.5 KB

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