#!/bin/zsh
#
#	test.zsh - Z shell script to test out the parseargs command!
#

NAME="`basename $0`"

ARGUMENTS="
  '?', ARGHIDDEN, argUsage, NULL,    'Help -- print usage and exit',
  'S', ARGVALOPT, argStr,   string,  'STRing -- optional string arg',
  'g', ARGLIST,   argStr,   groups,  'newsGROUPS -- groups to test',
  'r', ARGOPT,    argInt,   count,   'REPcount -- number to repeat each group',
  'd', ARGOPT,    argStr,   dirname, 'DIRectory -- working directory',
  'x', ARGOPT,    argBool,  xflag,   'Xflag -- turn on X-mode',
  'y', ARGOPT,    argUBool, yflag,   'Yflag -- turn off Y-mode',
  's', ARGOPT,    argChar,  sepch,   'SEPchar -- field separator',
  'f', ARGLIST,   argStr,   files,   'files -- files to process',
  'n', ARGREQ|ARGPOS, argStr, name,  'name -- name to use',
  ' ', ARGLIST,   argStr,   argv,    'argv -- any remaining arguments',
  ENDOFARGS
"
export ARGUMENTS

## set defaults ##
typeset count='1'          ## only do once unless otherwise specified
typeset dirname='.'        ## default to current directory
typeset xflag=''           ## default xflag is false
typeset yflag='TRUE'       ## default yflag is true
typeset sepch=','          ## default separator is a comma
typeset groups
typeset files  

## parse command-line ##
parseargs -s zsh -e ARGUMENTS -u -- "$NAME" "$@" >/tmp/tmp$$
if [ $? -ne 0 ] ; then
  rm -f /tmp/tmp$$
  exit 2  ## improper syntax (or just wanted usage)
fi

## evaluate results from parseargs and remove temporary file ##
INTERPRET="."
$INTERPRET /tmp/tmp$$
rm -f /tmp/tmp$$

## print arguments ##
print "ARGUMENTS:"
print "=========="
print "Groups='${groups[@]}'"
print "Count='$count'"
print "Directory='$dirname'"
print "XFlag='$xflag'"
print "YFlag='$yflag'"
print "SepChar='$sepch'"
print "Name='$name'"
print "Files='${files[@]}'"
if [ "$string_flag" ] ; then
  string=${string:-"!string arg ommitted on cmd-line!"}
else
  string="default string"
fi
print "String='$string'"
print "New Positional Parameters='$*'"

## print usage ##
parseargs -a "$ARGUMENTS" -U "$NAME"
