I don't think that there's a standard method (i.e. besides implementing it all from scratch) that would be available, let alone widely availabe in shells.
Though ksh
supports a quite powerful getopts
built-in. Based on this (and on your quite demanding requirements) I outlined a possible ksh
based solution with the following code fragment:
while getopts ":[-][[n]:][99:verbose][s]" opt
do case $opt in
(n) n_arg=$OPTARG ;;
(99) verbose=1 ;;
(s) s=1 ;;
(*) arg_rest+=( "${@:OPTIND-1:1}" ) ;;
esac
done
shift OPTIND-1
printf "main opt(%s)=%s\n" "-n" "$n_arg"
printf "main opt(%s)=%s\n" "--verbose" "$verbose"
printf "main opt(%s)=%s\n" "-s" "$s"
function delegate
{
while getopts ":[-][i][98:long]" opt
do case $opt in
(i) int=1 ;;
(98) long=1 ;;
esac
done
shift OPTIND-1
printf "func opt(%s)=%s\n" "-i" "$int"
printf "func opt(%s)=%s\n" "--long" "$long"
}
printf "Delegate: '%s'\n" "${arg_rest[@]}"
delegate "${arg_rest[@]}"
The program first parses all options, sets the internal variables as necessary, and stores the unknown options in an array. Then you see a few printf
's to control the settings. Then a function definition where the rest of the options are to be delegated to; the function can as well be replaced by a command. Finally the call of the function (or resp. some other command) with the rest of the arguments.
(For a description of ksh
's getopts
features call from within a ksh
session getopts --man
.)
Running that program produces this output:
$ ksh ./getopts_script -s -n 23 --verbose -i --long
main opt(-n)=23
main opt(--verbose)=1
main opt(-s)=1
Delegate: '-i'
Delegate: '--long'
func opt(-i)=1
func opt(--long)=1
For a shell implementation of a getopts function that supports long options see https://github.com/stephane-chazelas/misc-scripts/blob/master/getopts_long.sh
-h
or--help
to list all the options?makepkg
.) It has many different options with and without arguments, so they would be very difficult to parse themselves.man makepkg
that should give you a list of options.