parse_options.sh 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/bin/bash
  2. # Copyright 2012 Johns Hopkins University (Author: Daniel Povey);
  3. # Arnab Ghoshal, Karel Vesely
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  11. # KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  12. # WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  13. # MERCHANTABLITY OR NON-INFRINGEMENT.
  14. # See the Apache 2 License for the specific language governing permissions and
  15. # limitations under the License.
  16. # Parse command-line options.
  17. # To be sourced by another script (as in ". parse_options.sh").
  18. # Option format is: --option-name arg
  19. # and shell variable "option_name" gets set to value "arg."
  20. # The exception is --help, which takes no arguments, but prints the
  21. # $help_message variable (if defined).
  22. ###
  23. ### The --config file options have lower priority to command line
  24. ### options, so we need to import them first...
  25. ###
  26. # Now import all the configs specified by command-line, in left-to-right order
  27. for ((argpos=1; argpos<$#; argpos++)); do
  28. if [ "${!argpos}" == "--config" ]; then
  29. argpos_plus1=$((argpos+1))
  30. config=${!argpos_plus1}
  31. [ ! -r $config ] && echo "$0: missing config '$config'" && exit 1
  32. . $config # source the config file.
  33. fi
  34. done
  35. ###
  36. ### No we process the command line options
  37. ###
  38. while true; do
  39. [ -z "${1:-}" ] && break; # break if there are no arguments
  40. case "$1" in
  41. # If the enclosing script is called with --help option, print the help
  42. # message and exit. Scripts should put help messages in $help_message
  43. --help|-h) if [ -z "$help_message" ]; then echo "No help found." 1>&2;
  44. else printf "$help_message\n" 1>&2 ; fi;
  45. exit 0 ;;
  46. --*=*) echo "$0: options to scripts must be of the form --name value, got '$1'"
  47. exit 1 ;;
  48. # If the first command-line argument begins with "--" (e.g. --foo-bar),
  49. # then work out the variable name as $name, which will equal "foo_bar".
  50. --*) name=`echo "$1" | sed s/^--// | sed s/-/_/g`;
  51. # Next we test whether the variable in question is undefned-- if so it's
  52. # an invalid option and we die. Note: $0 evaluates to the name of the
  53. # enclosing script.
  54. # The test [ -z ${foo_bar+xxx} ] will return true if the variable foo_bar
  55. # is undefined. We then have to wrap this test inside "eval" because
  56. # foo_bar is itself inside a variable ($name).
  57. eval '[ -z "${'$name'+xxx}" ]' && echo "$0: invalid option $1" 1>&2 && exit 1;
  58. oldval="`eval echo \\$$name`";
  59. # Work out whether we seem to be expecting a Boolean argument.
  60. if [ "$oldval" == "true" ] || [ "$oldval" == "false" ]; then
  61. was_bool=true;
  62. else
  63. was_bool=false;
  64. fi
  65. # Set the variable to the right value-- the escaped quotes make it work if
  66. # the option had spaces, like --cmd "queue.pl -sync y"
  67. eval $name=\"$2\";
  68. # Check that Boolean-valued arguments are really Boolean.
  69. if $was_bool && [[ "$2" != "true" && "$2" != "false" ]]; then
  70. echo "$0: expected \"true\" or \"false\": $1 $2" 1>&2
  71. exit 1;
  72. fi
  73. shift 2;
  74. ;;
  75. *) break;
  76. esac
  77. done
  78. # Check for an empty argument to the --cmd option, which can easily occur as a
  79. # result of scripting errors.
  80. [ ! -z "${cmd+xxx}" ] && [ -z "$cmd" ] && echo "$0: empty argument to --cmd option" 1>&2 && exit 1;
  81. true; # so this script returns exit code 0.