source: trunk/essentials/app-shells/bash/examples/functions/getoptx.bash@ 3951

Last change on this file since 3951 was 3228, checked in by bird, 19 years ago

bash 3.1

File size: 8.9 KB
Line 
1#From: "Grigoriy Strokin" <[email protected]>
2#Newsgroups: comp.unix.shell
3#Subject: BASH: getopt function that parses long-named options
4#Date: Mon, 22 Dec 1997 20:35:18 +0300
5
6#Hi, I have written a BASH function named getoptex, that is like bash builtin
7#"getopts", but does parse long-named options and optional arguments. It only
8#uses builtin bash commands, so it is very fast. In order to use it in your
9#bash scripts, include a command ". getopt.sh" (<dot> getopt.sh) to the file
10#containing your script, and that will define functions getopt, getoptex, and
11#optlistex (the file getopt.sh with its detailed description is listed
12#below).
13
14#*** file getopt.sh ***
15
16#! /bin/bash
17#
18# getopt.sh:
19# functions like getopts but do long-named options parsing
20# and support optional arguments
21#
22# Version 1.0 1997 by Grigoriy Strokin ([email protected]), Public Domain
23# Date created: December 21, 1997
24# Date modified: December 21, 1997
25#
26# IMPORTANT FEATURES
27#
28# 1) Parses both short and long-named options
29# 2) Supports optional arguments
30# 3) Only uses bash builtins, thus no calls to external
31# utilities such as expr or sed is done. Therefore,
32# parsing speed is high enough
33#
34#
35# DESCRIPTION
36#
37# FUNCTION getopt
38# Usage: getopt OPTLIST {"$@"|ALTERNATIVE_PARAMETERS}
39#
40# like getopts, but parse options with both required and optional arguments,
41# Options with optional arguments must have "." instead of ":" after them.
42# Furthemore, a variable name to place option name cannot be specified
43# and is always placed in OPTOPT variable
44#
45# This function is provided for compatibility with getopts()
46# OPTLIST style, and it actually calls getoptex (see bellow)
47#
48# NOTE that a list of parameters is required and must be either "$@",
49# if processing command line arguments, or some alternative parameters.
50#
51# FUNCTION getoptex
52# Usage: getoptex OPTION_LIST {"$@"|ALTERNATIVE_PARAMETERS}
53#
54# like getopts, but parse long-named options.
55#
56# Both getopt and getoptex return 0 if an option has been parsed,
57# and 1 if all options are already parsed or an error occured
58#
59# Both getopt and getoptex set or test the following variables:
60#
61# OPTERR -- tested for whether error messages must be given for invalid
62options
63#
64# OPTOPT -- set to the name of an option parsed,
65# or to "?" if no more options or error
66# OPTARG -- set to the option argument, if any;
67# unset if ther is no argument;
68# on error, set to the erroneous option name
69#
70# OPTIND -- Initialized to 1.
71# Then set to the number of the next parameter to be parsed
72# when getopt or getoptex will be called next time.
73# When all options are parsed, contains a number of
74# the first non-option argument.
75#
76#
77# OPTOFS -- If a parameter number $OPTIND containg an option parsed
78# does not contain any more options, OPTOFS is unset;
79# otherwise, OPTOFS is set to such a number of "?" signs
80# which is equal to the number of options parsed
81#
82# You might not set variables OPTIND and OPTOFS yourself
83# unless you want to parse a list of parameters more than once.
84# Otherwise, you whould unset OPTIND (or set it to 1)
85# and unset OPTOFS each time you want to parse a new parameters
86list
87#
88# Option list format is DIFFERENT from one for getopts or getopt.
89getopts-style
90# option list can be converted to getoptex-style using a function optlistex
91# (see bellow)
92#
93# DESCRIPTION of option list used with getoptex:
94# Option names are separated by whitespace. Options consiting of
95# more than one character are treated as long-named (--option)
96#
97# Special characters can appear at the and of option names specifying
98# whether an argument is required (default is ";"):
99# ";" (default) -- no argument
100# ":" -- required argument
101# "," -- optional argument
102#
103# For example, an option list "a b c help version f: file: separator."
104# defines the following options:
105# -a, -b, -c, --help, --version -- no argument
106# -f, --file -- argument required
107# --separator -- optional argument
108#
109# FUNCTION optlistex
110# Usage new_style_optlist=`optlistex OLD_STYLE_OPTLIST`
111#
112# Converts getopts-style option list in a format suitable for use with getoptex
113# Namely, it inserts spaces after each option name.
114#
115#
116# HOW TO USE
117#
118# In order o use in your bash scripts the functions described,
119# include a command ". getopt.sh" to the file containing the script,
120# which will define functions getopt, getoptex, and optlistex
121#
122# EXAMPLES
123#
124# See files 'getopt1' and 'getopt2' that contain sample scripts that use
125# getopt and getoptex functions respectively
126#
127#
128# Please send your comments to [email protected]
129
130function getoptex()
131{
132 let $# || return 1
133 local optlist="${1#;}"
134 let OPTIND || OPTIND=1
135 [ $OPTIND -lt $# ] || return 1