Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents
typeflat
printablefalse


About

Excerpt

Manipulating the input command line arguments

Code Snippets


Code Block
languagebash
title# Arguments
collapsetrue
if [ $# -lt 2 ]; then
  echo "Usage: rosrun PACKAGE EXECUTABLE [ARGS]"
  echo "  rosrun will locate PACKAGE, cd to it, and try to find"
  echo "  an executable named EXECUTABLE in the PACKAGE tree."
  echo "  If it finds it, it will run it with ARGS."
  exit 1
fi


Code Block
languagebash
titleListing
collapsetrue
echo $*


Code Block
languagebash
titleIterating Over
collapsetrue
echo $*
shift
shift
echo $*


Code Block
languagebash
titleComplex Argument Parsing
linenumberstrue
collapsetrue
###########################
# Macros
###########################

show_help ()
{
  echo "    Usage:"
  echo "        foo <required-options> <options>"
  echo ""
  echo "    Required Option (one of):"
  echo "        --stable                                        : work with the stable sources/binaries"
  echo "        --devel                                         : work with the devel sources/binaries"
  echo ""
  echo "    Options:"
  echo "        --help                                          : show this help message"
  echo "        --jobs=<n>                                      : parallelise downloads (default is 5)"
  echo "        -d, --source-directory=<directory>              : specify custom source directory"
  echo ""
  exit 0
}


###########################
# PreConfigured Defaults
###########################

JOBS=5

###########################
# Command Line Parsing
###########################


for i in "$@"
do
case $i in
    --help)
    SHOW_HELP=1
    shift
    ;;
    --stable)
    STABLE=1
    shift
    ;;
    --devel)
    DEVEL=1
    shift
    ;;
    -j=*|--jobs=*)
    JOBS="${i#*=}"
    shift
    ;;
    -d=*|--source-directory=*)
    USE_CUSTOM_SOURCE_DIRECTORY=1
    CUSTOM_SOURCE_DIRECTORY="${i#*=}"
    shift
    ;;
    *)
       # unknown option
    ;;
esac
done


if [ ! -z "$SHOW_HELP" ]; then
  show_help
  exit 0
fi


if [ ! -z "$USE_CUSTOM_SOURCE_DIRECTORY" ]; then
  GROOT=$CUSTOM_SOURCE_DIRECTORY
else
  GROOT=/opt/groot
fi