The portable equivalent of getopt_long for use in shell scripts

I would like to parse long parameters in a shell script. POSIX provides only getopts for parsing single letter parameters. Does anyone know of a portable (POSIX) way to implement a long parsing of parameters in a shell? I looked at what autoconf does when generating configure scripts, but the result is far from elegant. I can live with the adoption of only full options for long options. Parameters for a single letter should be allowed, possibly in groups.

I am thinking that the shell function accepts a space, separated by spaces, of the arguments of the form [= flags], where the flags indicate that the option takes an arg argument or can be specified several times. Unlike its C counterpart, there is no need to distinguish between strings, integers and floats.

+4
source share
1 answer

Design Considerations for the getopt_long Portable Command

I have a getoptx program that works with single-letter options (therefore, this is not the answer to your problem), but it correctly processes arguments with spaces, and the original getopt command (unlike the shell, built-in getopts ). The description in the source code says:

 /* ** Usage: eval set -- $(getoptx abc: " $@ ") ** eval set -- $(getoptx abc: -a -c 'abc' -b abc 'de f') ** The positional parameters are: ** $1 = "-a" ** $2 = "-c" ** $3 = "abc" ** $4 = "-b" ** $5 = "--" ** $6 = "abc" ** $7 = "def" ** ** The advantage of this over the standard getopt program is that it handles ** spaces and other metacharacters (including single quotes) in the option ** values and other arguments. The standard code does not! The downside is ** the non-standard invocation syntax compared with: ** ** set -- $(getopt abc: " $@ ") */ 

I recommend the notation eval set -- $(getopt_long "$optspec" " $@ ") for your getopt_long .

One of the main problems with getopt_long is the complexity of the argument specification - $optspec in this example.

You can look at the notations used in the Solaris CLIP (Command Line Interface Paradigm) to indicate; it uses a single line (for example, the original getopt() function) to describe the parameters. (Google: "The Solaris Clip Command Line Interface Paradigm", using only the "tanning clip" will take you to video clips.)

This material is a partial example derived from Sun getopt_clip() :

 /* Example 2: Check Options and Arguments. The following example parses a set of command line options and prints messages to standard output for each option and argument that it encounters. This example can be expanded to be CLIP-compliant by substituting the long string for the optstring argument: While not encouraged by the CLIP specification, multiple long-option aliases can also be assigned as shown in the following example: :a(ascii)b(binary):(in-file)(input)o:(outfile)(output)V(version)?(help) */ static const char *arg0 = 0; static void print_help(void) { printf("Usage: %s [-a][-b][-V][-?][-f file][-o file][path ...]\n", arg0); printf("Usage: %s [-ascii][-binary][-version][-in-file file][-out-file file][path ...]\n", arg0); exit(0); } static const char optstr[] = ":a(ascii)b(binary)f:(in-file)o:(out-file)V(version)?(help)"; int main(int argc, char **argv) { int c; char *filename; arg0 = argv[0]; while ((c = getopt_clip(argc, argv, optstr)) != -1) { ... } ... } 
+2
source

All Articles