Commit 8446df21 authored by Sam Stephenson's avatar Sam Stephenson
Browse files

Parse options so they can be combined (e.g. `-kv`) and occur anywhere on the command line

parent aee47820
Loading
Loading
Loading
Loading
+66 −35
Original line number Diff line number Diff line
@@ -5,6 +5,39 @@ RUBY_BUILD_VERSION="20120524"
set -E
exec 3<&2 # preserve original stderr at fd 3


lib() {
  parse_options() {
    OPTIONS=()
    ARGUMENTS=()
    local arg option index

    for arg in "$@"; do
      if [ "${arg:0:1}" = "-" ]; then
        if [ "${arg:1:1}" = "-" ]; then
          OPTIONS[${#OPTIONS[*]}]="${arg:2}"
        else
          index=1
          while option="${arg:$index:1}"; do
            [ -n "$option" ] || break
            OPTIONS[${#OPTIONS[*]}]="$option"
            index=$(($index+1))
          done
        fi
      else
        ARGUMENTS[${#ARGUMENTS[*]}]="$arg"
      fi
    done
  }

  if [ "$1" == "--$FUNCNAME" ]; then
    declare -f "$FUNCNAME"
    exit
  fi
}
lib "$1"


resolve_link() {
  $(type -p greadlink readlink | head -1) "$1"
}
@@ -332,7 +365,7 @@ version() {

usage() {
  { version
    echo "usage: ruby-build [-v|--verbose] definition prefix"
    echo "usage: ruby-build [-k|--keep] [-v|--verbose] definition prefix"
    echo "       ruby-build --definitions"
  } >&2

@@ -351,34 +384,41 @@ list_definitions() {


unset VERBOSE
unset KEEP_BUILD_PATH
RUBY_BUILD_ROOT="$(abs_dirname "$0")/.."

case "$1" in
"-h" | "--help" )
parse_options "$@"

for option in "${OPTIONS[@]}"; do
  case "$option" in
  "h" | "help" )
    usage without_exiting
    { echo
      echo "  -k/--keep        Do not remove source tree after installation"
      echo "  -v/--verbose     Verbose mode: print compilation status to stdout"
      echo "  --definitions    List all built-in definitions"
      echo
    } >&2
    exit 0
    ;;
"--definitions" )
  "definitions" )
    list_definitions
    exit 0
    ;;
"--version" )
  version
  exit 0
  "k" | "keep" )
    KEEP_BUILD_PATH=true
    ;;
"-v" | "--verbose" )
  "v" | "verbose" )
    VERBOSE=true
  shift
    ;;
  "version" )
    version
    exit 0
    ;;
  esac
done


DEFINITION_PATH="$1"
DEFINITION_PATH="${ARGUMENTS[0]}"
if [ -z "$DEFINITION_PATH" ]; then
  usage
elif [ ! -e "$DEFINITION_PATH" ]; then
@@ -391,20 +431,11 @@ elif [ ! -e "$DEFINITION_PATH" ]; then
  fi
fi

PREFIX_PATH="$2"
PREFIX_PATH="${ARGUMENTS[1]}"
if [ -z "$PREFIX_PATH" ]; then
  usage
fi

OPTIONS="$3"
for option in $OPTIONS; do
  case "$option" in
    "-k" | "--keep" )
      KEEP_BUILD_PATH="y"
      ;;
  esac
done

if [ -z "$TMPDIR" ]; then
  TMP="/tmp"
else