User Tools

Site Tools


blog:parsing_long_and_short_command_line_options_in_bash

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.txt · Last modified: 2021-01-14 14:51 by brb