User Tools

Site Tools


blog:parsing_long_and_short_command_line_options_in_bash

This is an old revision of the document!


Parsing long and short command line options in bash

parse_user_options() {
    local -r args=("${@}")
    local opts
# The following code works perfectly for 
    opts=$(getopt --options a:,f,h --long abc:,help,flag -- "${args[@]}" 2> /dev/null) || {
        usage
        die "error: parsing options" "${error_parsing_options}"
    }
eval set -- "${opts}"
while true; do
    case "${1}" in
--abc)
            abc_option_flag=1
            readonly abc_arg="${2}"
            shift
            shift
            ;;
-a)
            a_option_flag=1
            readonly a_arg="${2}"
            shift
            shift
            ;;
--help|-h)
            usage
exit 0
            shift
            ;;
--flag|-f)
            flag_option_flag=1
shift
            ;;
--)
            shift
            break
            ;;
        *)
            break
            ;;
    esac
    done
}

Discussion

Enter your comment. Wiki syntax is allowed:
   ____ ______   __  ___   ___   _____
  / __//_  __/  /  |/  /  / _ | / ___/
 / _/   / /    / /|_/ /  / __ |/ /__  
/___/  /_/    /_/  /_/  /_/ |_|\___/
 
blog/parsing_long_and_short_command_line_options_in_bash.1610631946.txt.gz · Last modified: 2021-01-14 14:45 by brb