Dave
Dave

Reputation: 1241

Bash getopts differentiate "-?" from invalid options

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?

Upvotes: 0

Views: 856

Answers (1)

Mike Q
Mike Q

Reputation: 7327

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() {

    if [[ $# -lt 1 ]];then
       echo "no input, help infomration"
       #show_help_info_function
       exit
     fi

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

            ;;
            -\?|-h|--\?|--help)
                echo  "help information"
                #show_help_info_function # a function that prints help and exits
                exit;
            ;;
            *)
                echo "not understanding 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 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 within the section of help; if none of these inputs is 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 -o something
 we a seting option=something
$ ./args.sh -j somethingelse
not understading your input at all!
$ ./args.sh -?
help information
$ ./args.sh
no input, help infomration
$ ./args.sh -h
help information

Upvotes: 1

Related Questions