OSDN Git Service

[update] : Use getopt
authorhayao <shun819.mail@gmail.com>
Mon, 28 Dec 2020 10:49:40 +0000 (19:49 +0900)
committerhayao <shun819.mail@gmail.com>
Mon, 28 Dec 2020 10:49:40 +0000 (19:49 +0900)
tools/msg.sh

index 65a8825..090bec7 100755 (executable)
@@ -53,19 +53,42 @@ msg_error() {
 }
 
 
-while getopts "a:c:l:no:r:s:xh-:" arg; do
-  case ${arg} in
-        a) appname="${OPTARG}" ;;
-        c) adjust_chr="${OPTARG}" ;;
-        l) 
+# Parse options
+ARGUMENT="${@}"
+_opt_short="a:c:l:no:r:s:xh"
+_opt_long="nocolor,bash-debug,help,nolabel,noappname,noadjust"
+OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- ${ARGUMENT})
+[[ ${?} != 0 ]] && exit 1
+
+eval set -- "${OPT}"
+unset OPT _opt_short _opt_long
+
+while true; do
+    case "${1}" in
+        -a)
+            appname="${2}"
+            shift 2
+            ;;
+        -c)
+            adjust_chr="${2}"
+            shift 2
+            ;;
+        -l) 
             customized_label=true
-            msg_label="${OPTARG}"
+            msg_label="${2}"
+            shift 2
+            ;;
+        -n)
+            nocolor=true
+            shift 1
             ;;
-        n) nocolor=true ;;
-        o) echo_opts="${OPTARG}" ;;
-        r)
+        -o)
+            echo_opts="${2}"
+            shift 2
+            ;;
+        -r)
             customized_label_color=true
-            case ${OPTARG} in
+            case "${2}" in
                 "black")
                     labelcolor="30"
                     ;;
@@ -95,41 +118,34 @@ while getopts "a:c:l:no:r:s:xh-:" arg; do
                     exit 1
                     ;;
             esac
+            shift 2
+            ;;
+        -s) 
+            label_space="${2}"
+            shift 2
             ;;
-        s) label_space="${OPTARG}" ;;
-        x)
+        -x | --bash-debug)
             bash_debug=true
             set -xv
+            shift 1
             ;;
-        h)
+        -h | --help)
             _help
             shift 1
             exit 0
             ;;
-        -)
-            case "${OPTARG}" in
-                "nocolor") nocolor=true ;;
-                "bash-debug")
-                    bash_debug=true
-                    set -xv
-                    ;;
-                "help") 
-                    _help
-                    exit 0
-                    ;;
-                "nolabel") nolabel=true ;;
-                "noappname") noappname=true ;;
-                "noadjust") noadjust=true ;;
-                *)
-                    _help
-                    exit 1
-                    ;;
-            esac
-  esac
+        --)
+            shift 1
+            break
+            ;;
+        *)
+            shift 1
+            _help
+            exit 1 
+            ;;
+    esac
 done
 
-shift $((OPTIND - 1))
-
 # Color echo
 #
 # Text Color