program list of "shargs" without line-number

コピペしやすいように行番号なしのプログラムを載せておく。

#! /bin/sh

# The shell script for shell script's argument processing:
# shargs Ver 0.003 (2008/07/20)
# Copyright (C) 2008 Adsaria

# This program is free software; you can redistribute it and/or modify it.
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY.

#
###
##### Following variables are environment dependent, modify them to your environment.

if [ -z "$ARGS_DEF_opt_char_flg" ];then	ARGS_DEF_opt_char_flg=""	; fi
if [ -z "$ARGS_DEF_opt_char_val" ];then	ARGS_DEF_opt_char_val=""	; fi
if [ -z "$ARGS_DEF_opt_word_flg" ];then	ARGS_DEF_opt_word_flg=""	; fi
if [ -z "$ARGS_DEF_opt_word_val" ];then	ARGS_DEF_opt_word_val=""	; fi
if [ -z "$ARGS_DEF_arg_namelist" ];then	ARGS_DEF_arg_namelist=""	; fi
if [ -z "$ARGS_DEF_arg_num_must" ];then	ARGS_DEF_arg_num_must=0		; fi

if [ -z "$ARGS_DEF_true" ];	then	ARGS_DEF_true="Y"		; fi
if [ -z "$ARGS_DEF_false" ];	then	ARGS_DEF_false="N"		; fi

#####
###
#

ARGS_count=0
ARGS_0=$0
ARGS_error=$ARGS_DEF_false

##### Checking the characters of option names and argument names

ARGS_tmp_1=$ARGS_DEF_opt_char_flg
ARGS_tmp_1="$ARGS_tmp_1 $ARGS_DEF_opt_char_val"
ARGS_tmp_1="$ARGS_tmp_1 $ARGS_DEF_opt_word_flg"
ARGS_tmp_1="$ARGS_tmp_1 $ARGS_DEF_opt_word_val"
ARGS_tmp_1="$ARGS_tmp_1 `echo $ARGS_DEF_arg_namelist | sed 's/ \.\.\.$//'`"
ARGS_tmp_1=`echo -n $ARGS_tmp_1 | tr -d " a-zA-Z0-9_" | wc -c`
if [ $ARGS_tmp_1 -ne 0 ]; then
	ARGS_fatal_error=$ARGS_DEF_true
	ARGS_message="shargs: the argument's names should be the letter of \"a-z\" \"A-Z\" \"0-9\"and  \"_\"."
fi

if [ $ARGS_DEF_arg_num_must -gt `echo $ARGS_DEF_arg_namelist | wc -w` ]; then
	ARGS_fatal_error=$ARGS_DEF_true
	ARGS_message="shargs: At least the number of mandatory arguments should be in arguments list."
fi

if [ "$ARGS_fatal_error" = $ARGS_DEF_true ]; then
	ARGS_error=$ARGS_DEF_true
else

##### The body of processing the arguments

	while [ $# -ne 0 -a $ARGS_error = $ARGS_DEF_false ]; do
		case $1 in
		--*)
			ARGS_tmp_current=${1:2}
			ARGS_tmp_1=`echo "$ARGS_DEF_opt_word_val" | wc -w`
			ARGS_tmp_2=`echo "${ARGS_DEF_opt_word_val/"$ARGS_tmp_current"/}" | wc -w`
			if [ $ARGS_tmp_1 -ne $ARGS_tmp_2 ]; then
				if [ $# -eq 1 ]; then ARGS_error=$ARGS_DEF_true; break; fi
				eval "ARGS_opt_$ARGS_tmp_current=\"$2\""
				shift; shift
			else
				ARGS_tmp_1=`echo "$ARGS_DEF_opt_word_flg" | wc -w`
				ARGS_tmp_2=`echo "${ARGS_DEF_opt_word_flg/"$ARGS_tmp_current"/}" | wc -w`
				if [ $ARGS_tmp_1 -ne $ARGS_tmp_2 ]; then
					eval "ARGS_opt_$ARGS_tmp_current=$ARGS_DEF_true"
					shift
				else
					ARGS_error=$ARGS_DEF_true
					shift
					break
				fi
			fi
			;;
		-[^-]*)
			ARGS_tmp_current=${1:1}
			ARGS_tmp_index=0
			while [ $ARGS_tmp_index -lt ${#ARGS_tmp_current} ]; do
				ARGS_tmp_char=${ARGS_tmp_current:$ARGS_tmp_index:1}
				ARGS_tmp_1=${#ARGS_DEF_opt_char_val}
				ARGS_tmp_2=`echo -n ${ARGS_DEF_opt_char_val/$ARGS_tmp_char/} | wc -c`
				if [ $ARGS_tmp_1 -ne $ARGS_tmp_2 ]; then
					if [ $# -eq 1 ]; then ARGS_error=$ARGS_DEF_true; break; fi
					eval "ARGS_opt_$ARGS_tmp_char=\"$2\""
					shift
				else
					ARGS_tmp_1=${#ARGS_DEF_opt_char_flg}
					ARGS_tmp_2=`echo -n ${ARGS_DEF_opt_char_flg/$ARGS_tmp_char/} | wc -c`
					if [ $ARGS_tmp_1 -ne $ARGS_tmp_2 ]; then
						eval "ARGS_opt_$ARGS_tmp_char=$ARGS_DEF_true"
					else
						ARGS_error=$ARGS_DEF_true
						break
					fi
				fi
				ARGS_tmp_index=`expr $ARGS_tmp_index + 1`
			done
			shift
			if [ "$ARGS_error" = $ARGS_DEF_true ]; then break; fi
			;;
		*)
			ARGS_count=`expr $ARGS_count + 1`
			eval "ARGS_$ARGS_count=\"$1\""
			ARGS_tmp_word_name=`echo -n "$ARGS_DEF_arg_namelist" | awk "{print \\$$ARGS_count}"`
			if [ "$ARGS_tmp_word_name" != "" -a "$ARGS_tmp_word_name" != "..." ]; then
				eval "ARGS__$ARGS_tmp_word_name=\"$1\""
			fi
			shift
			;;
		esac
	done

	if [ $ARGS_count -lt $ARGS_DEF_arg_num_must ]; then ARGS_error=$ARGS_DEF_true; fi



##### Message generation part

	ARGS_usage="Usage: $ARGS_0"

	if [ -n "$ARGS_DEF_opt_char_flg" ]; then
		ARGS_usage="$ARGS_usage [-$ARGS_DEF_opt_char_flg]"
	fi

	for ARGS_tmp_word in $ARGS_DEF_opt_word_flg ; do
		ARGS_usage="$ARGS_usage [--$ARGS_tmp_word]"
	done

	ARGS_tmp_index=0
	while [ $ARGS_tmp_index -lt ${#ARGS_DEF_opt_char_val} ]; do
		ARGS_tmp_char=${ARGS_DEF_opt_char_val:$ARGS_tmp_index:1}
		ARGS_usage="$ARGS_usage [-$ARGS_tmp_char argument]"
		ARGS_tmp_index=`expr $ARGS_tmp_index + 1`
	done

	for ARGS_tmp_word in $ARGS_DEF_opt_word_val ; do
		ARGS_usage="$ARGS_usage [--$ARGS_tmp_word argument]"
	done

	ARGS_tmp_count=0
	ARGS_tmp_first_opt=$ARGS_DEF_true
	for ARGS_tmp_word in $ARGS_DEF_arg_namelist ; do
		if [ $ARGS_tmp_count -lt $ARGS_DEF_arg_num_must ]; then
			ARGS_usage="$ARGS_usage $ARGS_tmp_word"
		else
			if [ $ARGS_tmp_first_opt = $ARGS_DEF_true ]; then
				ARGS_tmp_first_opt=$ARGS_DEF_false
				ARGS_usage="$ARGS_usage [$ARGS_tmp_word"
			else
				if [ $ARGS_tmp_word = "..." ]; then
					ARGS_usage="$ARGS_usage $ARGS_tmp_word"
				else
					ARGS_usage="$ARGS_usage] [$ARGS_tmp_word"
				fi
			fi
		fi
		let ARGS_tmp_count++
	done
	if [ $ARGS_tmp_first_opt = $ARGS_DEF_false ]; then ARGS_usage="$ARGS_usage]"; fi

##### Display the message if required

	if [ "$ARGS_error" = $ARGS_DEF_true ]; then
		if [ "$ARGS_DEF_show_message" = $ARGS_DEF_true ];then
			echo $ARGS_usage
		fi
	fi


fi

##### Unset the temporary shell variables

unset ${!ARGS_tmp*}
unset ${!ARGS_DEF*}