I am looking to use long options in some of my code instead of single value 
switches.  I read the getopt shouldn't be used, instead getopts (with an 's') 
which is built in to ksh93.  Looking at the example in appendix B of a the  
book _Learning the Korn Shell_, I made the following test case.

I see that I can't use two long options that start with the same letter (e.g. 
--date and --delay), since both would be reduced to "-d" for getopts.

  * Should I just give up on getopts and roll my own? (.e.g -d would not be 
allowed, but --date and --delay would be)
  * I do like how this makes a nice man page (e.g ./test.ksh --man).  But is it 
possible to have only long options without short options?
  * Is there a way to make some of these options required and some optional, 
without checking after getopts?  i.e.,within the usage definitions.  I see 
where I can make an OPTARG optional (e.g. USAGE+="[r:read?reader 
mode.]:?[reader]"
  

Thanks for any insight and assistance
Eric



============ start of test.ksh ============ 
#! /bin/ksh
#usage: test [options]
#  -d, --date <date>     delivery date (YYYYMMDD or YYYYMMDDHH)
#  -f, --flag <flag>     [ aaa | bbb ]
#  --user                retrieve data
#  --media               retrieve media

USAGE=$'[-?@(#)$Id: '$(basename $0 )$']'
USAGE+="[-author?Eric Peterson <email address>]"
USAGE+="[+NAME?$(basename $0 ) --- test command line arguments]"
USAGE+="[+DESCRIPTION?A test verify long options work "
USAGE+="on the command line.]"
USAGE+="[d:date?Delivery date (YYYYMMDD or YYYYMMDDHH)]:[date value]"
USAGE+="[f:flag?[ aaa | bbb ]]]:[type] "
USAGE+="[u:user?Retrieve user data]"
USAGE+="[m:media?Retrieve media content]"

dt_date=
fl_flag=
integer fl_user=0
integer fl_media=0
while getopts "${USAGE}" optchar
do
  case ${optchar} in
    d)  dt_date=${OPTARG} ;;
    f) fl_flag=${OPTARG} ;;
    u) fl_user=1 ;;
    m) fl_media=1 ;;
  esac
done

if [[ -z ${dt_date} ]]
then
  print -u2 '***> ERR: Missing date value'
  $0 --long
  exit 1
fi
if [[ ${fl_flag} != "aaa" ]] && [[ ${fl_flag} != "bbb" ]]
then
  print -u2 '***> ERR: Invalid flag, expecting [ aaa | bbb ]'
  $0 --long
  exit 1
fi
if (( fl_user == 0 )) && (( fl_media == 0 )) 
then
  print -u2 '***> ERR: one or both retrieval flags must be on command line'
  $0 --long
  exit 1
fi

print "dt_date  : [${dt_date}]"
print "fl_flag  : [${fl_flag}]"
print "fl_user  : [${fl_user}]"
print "fl_media : [${fl_media}]"
============ end of test.ksh ============ 


>./test.ksh --man
NAME
  test.ksh --- test command line arguments

SYNOPSIS
  ./test.ksh [ options ]

DESCRIPTION
  A test verify long options work on the command line.

OPTIONS
  -d, --date=date value
                  Delivery date (YYYYMMDD or YYYYMMDDHH)
  -f, --flag=type [ aaa | bbb ]
  -u, --user      Retrieve user data
  -m, --media     Retrieve media content

IMPLEMENTATION
  version         test.ksh
  author          Eric Peterson <email address>




_______________________________________________
ast-users mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-users

Reply via email to