getopt - Running a bash script with mandatory arguments -
i want bash script working when type
bash.sh --subj directoryname --input filename --all
--subj
, --input
, , --all
required make script work, , --all
argument not require input.
if either --subj
or --input
missing, want following message printed:
please provide both directory name , file name.
if --all
argument missing, want print:
please use --all option.
i fix script github using optparse
.
source optparse.bash optparse.define short=s long=subj desc="subject name" variable=subj optparse.define short=i long=input desc="the file process" variable=input optparse.define short=a long=all desc="all option" variable=all value=true default=false source $(optparse.build) if [ "$subj" == "" ]; echo "error: please provide directory name" exit 1 fi if [ "$input" == "" ]; echo "error: please provide input" exit 1 fi if [ "$all" == "" ]; echo "error: please use option" exit 1 fi
https://github.com/nk412/optparse/blob/master/sample_head.sh
using code, please provide directory name when missed --subj or -input. when miss all, not error. know why that?
from doc:
flags defined in same way, parameter value assigned variable.
optparse.define short=v long=verbose desc="set flag verbose mode" variable=verbose_mode value=true default=false
this want, flags options don't require arguments.
Comments
Post a Comment