Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ################################################################################
- # Function: parse_cmdline
- # Description: Parses the command-line using the enhanced version of getopt
- # Arguments: $@ :- Command-line arguments (required)
- # Requires: Global variables $USER_NAME, $DEST_DIR, $IS_REMOVE, and
- # $IS_VERBOSE
- # Outputs: Sets above-mentioned global variables based on given
- # command-line arguments
- function parse_cmd_line {
- # Ensure that the enhanced version of getopt is available
- ! getopt --test > /dev/null
- if (( "${PIPESTATUS[0]}" != 4 )); then
- echo "I'm sorry, the enhanced version of getopt is required. Exiting." >&2
- exit 1
- fi
- # Initialize the global variables
- USER_NAME=''
- DEST_DIR=''
- IS_VERBOSE=''
- IS_REMOVE=''
- # Parse the command-line options
- if ! readonly PARSED_OPTIONS=$(getopt --options=${OPTIONS} \
- --longoptions=${LONGOPTS} \
- --name "$(basename "$0")" -- "$@"); then
- echo 'Unknown error parsing getopt options. Exiting.' >&2
- exit 2
- fi
- eval set -- "${PARSED_OPTIONS}"
- # Extract and validate command-line options and their arguments, if any
- while (( $# >= 1 )); do
- case "$1" in
- -u|--user)
- USER_NAME=$(trim "$2") ; shift 2
- ;;
- -d|--destdir)
- DEST_DIR=$(chomp_slash "$(trim "$2")") ; shift 2
- ;;
- -r|--remove)
- IS_REMOVE='true' ; shift
- ;;
- -v|--verbose)
- IS_VERBOSE='true' ; shift
- ;;
- -h|--help)
- usage_exit 0
- ;;
- --)
- shift ; break
- ;;
- *)
- usage_exit 3 >&2
- ;;
- esac
- done
- # Ensure that required options have been supplied
- if [[ -z "${USER_NAME}" || -z "${DEST_DIR}" ]]; then
- usage_exit 4 >&2
- fi
- } # parse_cmd_line
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement