Home > Net >  Bash getopts differentiate "-?" from invalid options
Bash getopts differentiate "-?" from invalid options

Time:04-02

I want getopts to recognize when a user passes "-?" and "-h" and send to my help function. I also don't want getopts to send the user to the help function if an invalid option is presented. I want a different message to display telling them it was an invalid option and send to stderr.

Here is my options section:

options(){

    # grab options flags before entering application:
    while getopts ":h?vu:d" opts; do
        case "${opts}"
        in

            h\?)  # Help
                help_section
                exit 0
                ;;

            v)  # Version
                version
                exit 0
                ;;

            u)  # units
                other_units="$OPTARG"
                ;;

            d)  # Debug
                debug=1
                ;;

            \?) # Invalid options
                echo "no here"
                >&2 echo "[!] ERROR: Invalid Option: -$OPTARG"
                exit 1
                ;;


        esac
    done
    shift $((OPTIND -1))

    # Run main function
    main_run "$@"
}

Problem: getopts keeps sending the user to help when they put an invalid option in. I need to make sure the user recognizes they provided an invalid parameter; I do not want to send them to help.

Is there a way to implement this with getopts? Or build logic outside of getopts to capture and perform what I need?

CodePudding user response:

Bash Command Line Input Example

The following example shows how to take various types of inputs and default to an invalid input option.

# -- Get input options (if any)
function get_user_input_options() {

    while [[ $# > 0 ]] ;do
        key="$1"
        case ${key,,} in
            -o|--opt)
                echo " we a seting option=$2"
                option=2
                shift

            ;;
            -?|-h|--?|--help)
                echo  "help information"
                exit;
            ;;
            *)
                echo "not understading your input at all!"
                exit;
            ;;
        esac
        shift
    done
}

get_user_input_options "$@"

What is going on here?

We read in all of the inputs and based on the type we can shift to the next or we shift twice. If you see --opt is used, it is expecting something after which would be set to a variable. More on point of your issue, we accept -? or --? and if that happens it will do what is withing the section of help, if none of these inputs are processed it will do whatever the default is in the section '*' which means any input. Note: this is just an example and in this case the help section causes the script to stop even if other inputs were provided, this may or may not be what you personally are looking to do.

Example output

./args.sh --opt "option"
 we a seting option=option

./args.sh --?
help information

./args.sh --invalid_option
not understading your input at all!
  • Related