//TODO
You can use Go
to install this package for you:
go get -u github.com/mwat56/getopts
Since you're interested in this module you probably know the getopts
functionality as used in, for example, shell scripts etc. This module tries to mimic that. However, there are some small differences in the pattern used to describe the expected commandline arguments.
As an example here a simple use case:
func MySetup(aPattern string) {
var (
b bool
f float64
i int
o string
s string
)
// Loop through all available options:
for {
opt, arg, more := Get(aPattern)
switch opt {
case "b":
b = arg.Bool()
case "f":
f = arg.Float()
case "i":
i = arg.Int()
case "s":
s = arg.String()
default:
o = opt
}
if !more {
// No more options available
break
}
}
fmt.Printf("Bool: %t, Float: %f, Int: %d, String: %q, other: %v",
b, f, i, s, o)
} // MySetup()
Of course, you would need to give a valid pattern
. And you would assign the respective options to your own configuration variables (instead of the locally declared dummies in the example above).
Here comes a brief comparison between the use of getopts
in a shell script and in Go.
In a bash
shell script, the use would look something like this:
while getopts ":a:c:hjl:p:qs:v:" opt; do
case ${opt} in
a) # use the `a` argument's option
echo " -a option: ${OPTARG}"
;;
c) # use the `c` argument's option
echo " -c option: ${OPTARG}"
;;
h) # handle the `h` argument's existence
echo " -h: ${opt}"
;;
j) # handle the `j` argument's existence
echo " -j: ${opt}"
;;
l) # use the `p` argument's option
echo " -p option: ${OPTARG}"
;;
p) # use the `p` argument's option
echo " -p option: ${OPTARG}"
;;
q) # handle the `q` argument's existence
echo " -q: ${opt}"
;;
s) # use the `s` argument's option
echo " -s option: ${OPTARG}"
;;
v) # use the `v` argument's option
echo " -v option: ${OPTARG}"
;;
\?) # unknown option
echo -e "\n\tignoring unknown option: -${opt}=${OPTARG}\n" >&2
;;
:) # invalid argument
echo "Option -${opt} requires an argument." >&2
;;
esac
done
As can be seen above, the pattern used is this: :a:c:hjl:p:qs:v:
. The expected options are given by their respective name. A following :
(colon) signals that the option expects an argument following it. Let's break it down:
- The leading colon (
:
) at the beginning of the string enables silent error reporting mode. In this mode,getopts
handles errors internally without printing error messages. - The remaining characters define the valid options:
a
: - Option-a
requires an argumentc
: - Option-c
requires an argumenth
: - Option-h
does not require an argumentj
: - Option-j
does not require an argumentl
: - Option-l
requires an argumentp
: - Option-p
requires an argumentq
: - Option-q
does not require an arguments
: - Option-s
requires an argumentv
: - Option-v
requires an argument
So, this getopts
argument specifies that the script accepts the following options:
-a
,-c
,-l
,-p
,-s
, and-v
: These options require an argument. When using these options, you must provide a value immediately after the option letter.-h
,-j
, and-q
: These are flag options that do not require an argument. They can be used to toggle certain behaviours or settings in the script.
Now, Go does not support a while
loop directly but as can be seen above it can be simply build by an for{ ... }
loop that runs as long as it isn't broken.
The pattern to use look similar but not identical:
- Shell:
:a:c:hjl:p:qs:v:
- Go:
a:|c:|h|j|l:|p:|q|s:|v:
A leading colon in the pattern is not needed here because any problems are handled internally anyway. One common problem, for example, is giving an option on the commandline that requires an argument (e.g. a filename or a certain value) without providing that argument. This Go implementation of getopts()
simply ignores such option, and it's up to the developer to decide what to do if the option wasn't provided by the app user (which, BTW, a developer has to do anyway).
While the *nix getopts allows only for single letter options, we want to be able to work with long options like --help
as well. Hence we need a separator between the options which is here the pipe symbol |
. So a pattern for this Go implementation could look like this:
a|i:|-input:|h|-help|o:|-output:|q|v
This would handle a commandline with options like
$> myprog -a -i inFile1 --input inFile1 -o outFile1 --output outFile2 -q -v --help
It's up to the developer to decide how to handle such settings: Here are both, the short and the long form, options used for the input and output file names. And there are both, the -q
(quiet) and -v
(verbose), flags used. And shouldn't myprog
be terminated if help was requested?
The following external libraries were used building getopts
:
- No external modules were used apart from Go's standard library.
Copyright © 2024 M.Watermann, 10247 Berlin, Germany
All rights reserved
EMail : <support@mwat.de>
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
You should have received a copy of the GNU General Public License along with this program. If not, see the GNU General Public License for details.