program list of "shargs"
色々と手を加えていたらいつの間にか150行を越えてしまったが、やっていることは非常に単純なので特に説明は要らないと思う。
1 #! /bin/sh 2 3 # The shell script for shell script's argument processing: 4 # shargs Ver 0.003 (2008/07/20) 5 # Copyright (C) 2008 Adsaria 6 7 # This program is free software; you can redistribute it and/or modify it. 8 # This program is distributed in the hope that it will be useful, but 9 # WITHOUT ANY WARRANTY. 10 11 # 12 ### 13 ##### Following variables are environment dependent, modify them to your environment. 14 15 if [ -z "$ARGS_DEF_opt_char_flg" ];then ARGS_DEF_opt_char_flg="" ; fi 16 if [ -z "$ARGS_DEF_opt_char_val" ];then ARGS_DEF_opt_char_val="" ; fi 17 if [ -z "$ARGS_DEF_opt_word_flg" ];then ARGS_DEF_opt_word_flg="" ; fi 18 if [ -z "$ARGS_DEF_opt_word_val" ];then ARGS_DEF_opt_word_val="" ; fi 19 if [ -z "$ARGS_DEF_arg_namelist" ];then ARGS_DEF_arg_namelist="" ; fi 20 if [ -z "$ARGS_DEF_arg_num_must" ];then ARGS_DEF_arg_num_must=0 ; fi 21 22 if [ -z "$ARGS_DEF_true" ]; then ARGS_DEF_true="Y" ; fi 23 if [ -z "$ARGS_DEF_false" ]; then ARGS_DEF_false="N" ; fi 24 25 ##### 26 ### 27 # 28 29 ARGS_count=0 30 ARGS_0=$0 31 ARGS_error=$ARGS_DEF_false 32 33 ##### Checking the characters of option names and argument names 34 35 ARGS_tmp_1=$ARGS_DEF_opt_char_flg 36 ARGS_tmp_1="$ARGS_tmp_1 $ARGS_DEF_opt_char_val" 37 ARGS_tmp_1="$ARGS_tmp_1 $ARGS_DEF_opt_word_flg" 38 ARGS_tmp_1="$ARGS_tmp_1 $ARGS_DEF_opt_word_val" 39 ARGS_tmp_1="$ARGS_tmp_1 `echo $ARGS_DEF_arg_namelist | sed 's/ \.\.\.$//'`" 40 ARGS_tmp_1=`echo -n $ARGS_tmp_1 | tr -d " a-zA-Z0-9_" | wc -c` 41 if [ $ARGS_tmp_1 -ne 0 ]; then 42 ARGS_fatal_error=$ARGS_DEF_true 43 ARGS_message="shargs: the argument's names should be the letter of \"a-z\" \"A-Z\" \"0-9\"and \"_\"." 44 fi 45 46 if [ $ARGS_DEF_arg_num_must -gt `echo $ARGS_DEF_arg_namelist | wc -w` ]; then 47 ARGS_fatal_error=$ARGS_DEF_true 48 ARGS_message="shargs: At least the number of mandatory arguments should be in arguments list." 49 fi 50 51 if [ "$ARGS_fatal_error" = $ARGS_DEF_true ]; then 52 ARGS_error=$ARGS_DEF_true 53 else 54 55 ##### The body of processing the arguments 56 57 while [ $# -ne 0 -a $ARGS_error = $ARGS_DEF_false ]; do 58 case $1 in 59 --*) 60 ARGS_tmp_current=${1:2} 61 ARGS_tmp_1=`echo "$ARGS_DEF_opt_word_val" | wc -w` 62 ARGS_tmp_2=`echo "${ARGS_DEF_opt_word_val/"$ARGS_tmp_current"/}" | wc -w` 63 if [ $ARGS_tmp_1 -ne $ARGS_tmp_2 ]; then 64 if [ $# -eq 1 ]; then ARGS_error=$ARGS_DEF_true; break; fi 65 eval "ARGS_opt_$ARGS_tmp_current=\"$2\"" 66 shift; shift 67 else 68 ARGS_tmp_1=`echo "$ARGS_DEF_opt_word_flg" | wc -w` 69 ARGS_tmp_2=`echo "${ARGS_DEF_opt_word_flg/"$ARGS_tmp_current"/}" | wc -w` 70 if [ $ARGS_tmp_1 -ne $ARGS_tmp_2 ]; then 71 eval "ARGS_opt_$ARGS_tmp_current=$ARGS_DEF_true" 72 shift 73 else 74 ARGS_error=$ARGS_DEF_true 75 shift 76 break 77 fi 78 fi 79 ;; 80 -[^-]*) 81 ARGS_tmp_current=${1:1} 82 ARGS_tmp_index=0 83 while [ $ARGS_tmp_index -lt ${#ARGS_tmp_current} ]; do 84 ARGS_tmp_char=${ARGS_tmp_current:$ARGS_tmp_index:1} 85 ARGS_tmp_1=${#ARGS_DEF_opt_char_val} 86 ARGS_tmp_2=`echo -n ${ARGS_DEF_opt_char_val/$ARGS_tmp_char/} | wc -c` 87 if [ $ARGS_tmp_1 -ne $ARGS_tmp_2 ]; then 88 if [ $# -eq 1 ]; then ARGS_error=$ARGS_DEF_true; break; fi 89 eval "ARGS_opt_$ARGS_tmp_char=\"$2\"" 90 shift 91 else 92 ARGS_tmp_1=${#ARGS_DEF_opt_char_flg} 93 ARGS_tmp_2=`echo -n ${ARGS_DEF_opt_char_flg/$ARGS_tmp_char/} | wc -c` 94 if [ $ARGS_tmp_1 -ne $ARGS_tmp_2 ]; then 95 eval "ARGS_opt_$ARGS_tmp_char=$ARGS_DEF_true" 96 else 97 ARGS_error=$ARGS_DEF_true 98 break 99 fi 100 fi 101 ARGS_tmp_index=`expr $ARGS_tmp_index + 1` 102 done 103 shift 104 if [ "$ARGS_error" = $ARGS_DEF_true ]; then break; fi 105 ;; 106 *) 107 ARGS_count=`expr $ARGS_count + 1` 108 eval "ARGS_$ARGS_count=\"$1\"" 109 ARGS_tmp_word_name=`echo -n "$ARGS_DEF_arg_namelist" | awk "{print \\$$ARGS_count}"` 110 if [ "$ARGS_tmp_word_name" != "" -a "$ARGS_tmp_word_name" != "..." ]; then 111 eval "ARGS__$ARGS_tmp_word_name=\"$1\"" 112 fi 113 shift 114 ;; 115 esac 116 done 117 118 if [ $ARGS_count -lt $ARGS_DEF_arg_num_must ]; then ARGS_error=$ARGS_DEF_true; fi 119 120 121 122 ##### Message generation part 123 124 ARGS_usage="Usage: $ARGS_0" 125 126 if [ -n "$ARGS_DEF_opt_char_flg" ]; then 127 ARGS_usage="$ARGS_usage [-$ARGS_DEF_opt_char_flg]" 128 fi 129 130 for ARGS_tmp_word in $ARGS_DEF_opt_word_flg ; do 131 ARGS_usage="$ARGS_usage [--$ARGS_tmp_word]" 132 done 133 134 ARGS_tmp_index=0 135 while [ $ARGS_tmp_index -lt ${#ARGS_DEF_opt_char_val} ]; do 136 ARGS_tmp_char=${ARGS_DEF_opt_char_val:$ARGS_tmp_index:1} 137 ARGS_usage="$ARGS_usage [-$ARGS_tmp_char argument]" 138 ARGS_tmp_index=`expr $ARGS_tmp_index + 1` 139 done 140 141 for ARGS_tmp_word in $ARGS_DEF_opt_word_val ; do 142 ARGS_usage="$ARGS_usage [--$ARGS_tmp_word argument]" 143 done 144 145 ARGS_tmp_count=0 146 ARGS_tmp_first_opt=$ARGS_DEF_true 147 for ARGS_tmp_word in $ARGS_DEF_arg_namelist ; do 148 if [ $ARGS_tmp_count -lt $ARGS_DEF_arg_num_must ]; then 149 ARGS_usage="$ARGS_usage $ARGS_tmp_word" 150 else 151 if [ $ARGS_tmp_first_opt = $ARGS_DEF_true ]; then 152 ARGS_tmp_first_opt=$ARGS_DEF_false 153 ARGS_usage="$ARGS_usage [$ARGS_tmp_word" 154 else 155 if [ $ARGS_tmp_word = "..." ]; then 156 ARGS_usage="$ARGS_usage $ARGS_tmp_word" 157 else 158 ARGS_usage="$ARGS_usage] [$ARGS_tmp_word" 159 fi 160 fi 161 fi 162 let ARGS_tmp_count++ 163 done 164 if [ $ARGS_tmp_first_opt = $ARGS_DEF_false ]; then ARGS_usage="$ARGS_usage]"; fi 165 166 ##### Display the message if required 167 168 if [ "$ARGS_error" = $ARGS_DEF_true ]; then 169 if [ "$ARGS_DEF_show_message" = $ARGS_DEF_true ];then 170 echo $ARGS_usage 171 fi 172 fi 173 174 175 fi 176 177 ##### Unset the temporary shell variables 178 179 unset ${!ARGS_tmp*} 180 unset ${!ARGS_DEF*}
行番号付だとコピペできないというコメントを頂いたが、長いプログラムは行番号があった方が説明しやすい。もしコピペでファイルに落とすときは、一旦、行番号付きでコピペして、vi コマンドで次のように処理する:
:%s/^ \+[0-9]\+ // ^空白 ^タブ(タブを入力すると"^I"と表示される) :wq但し、コピペする時にタブ文字が空白文字に置き換わってしまう環境では次の様にする:
:%s/^ \+[0-9]\+ // ^空白 ^空白2つ :wq