General
cancel
Showing results for 
Search instead for 
Did you mean: 

Best Practices Q: framework/skeleton for CLI executable /bin/sh script

drb_1
Occasional Advisor

Best Practices Q: framework/skeleton for CLI executable /bin/sh script

I am currently using

set -- $(getopt ... $*)
...
while test "--" != "$1"
do
case "$1" in
...
esac
shift
done

as described by getopt(1)



This is good enough for most of my executables,
but it has two significant limitations.

1. It fails to properly handle quoted spaces
in CLI option arguments.
2. It does not read from a CLI named
configuration file.

Does anyone have a sample
(or pointer to sample) that will properly
handle spaces in
/bin/sh CLI option arguments?

foo -a 'this has space'

I have tried using $@,
but so far without success.
I suspect an example would
provide the missing details.



Does anyone have a sample
(or pointer to sample) that
will read arguments from
a file argument to -f option,
before processing the rest of the options?

foo -f configfile -a 'override'
foo -a 'override' -f configfile

I have tried parsing once with getopts,
scanning the arguments for
a configuration file,
reading the configuration file, and
scanning the arguments as indicated by
getopt(1)

set -- $(getopts ... $*)
for f in $*
do
case "$f" in
...
-f) CONF=...
...
esac
done
if test -r "$CONF"
then
read-conf-file
fi
while test "--" != "$1"
do
case "$1" in
...
esac
...
shift
done

The limitations of the above include
1. option argument spaces are still mishandled
2. reading the conf file is currently
inelegant at best
and ad hoc at worst



Does anyone have a simple
(or pointer to sample)
that parses a config file parallel
to CLI options?

Ideally, I would like something
with results are equivalent to
1. read option and possible argument
from each conf file line
2. prepend the options and arguments
to the argument list
3. rerun getopt
so that there would be no difference between
whether an option was supplied
on the command line or in the conf file.

TIA,
1 REPLY
Peter Nikitka
Honored Contributor

Re: Best Practices Q: framework/skeleton for CLI executable /bin/sh script

Hi,

1) Option scanning
I suggest NOT to use getopt but getopts - its a builtin in pdksh, zsh and - I think - bash, as well - look into 'man getopts'.
Usage for an example call:
-v just set a flag
-o file option with a parameter
-c str option with a parameter containing spaces
cmd -o file -v -c 'more than one param' arg1 arg2

verbose=n
while getopts :vo:c:h c
do
case $c in
v) verbose=y ;;
h) echo usage; exit 0;;
o) ofile=$OPTARG ;;
c) comm="$OPTARG" ;;
?) echo usage; exit 1;;
esac
done
shift $((OPTIND-1))
par1=$1
par2=$2
...

2) Configfile parsing:
First you should decide, what is your strategy, when you get an option in a config file AND as an option argument.
I recommend, that such an additional + duplicate option has priority.
Such way, you just use the above getopts-loop BUT - if '-f conf' found as option - store the values in the configfile into different variables and use these as (new) default values for unspecified options.

mfG Peter

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"