Use getopt
Why getopt?
To parse elaborated command-line arguments to avoid confusion and clarify the options we are parsing so that reader of the commands can understand what's happening.
What is getopt?
getopt is used to break up (parse) options in command lines for easy parsing by shell procedures, and to check for legal options. It uses the GNU getopt(3) routines to do this.
getopt can have following types of options.
- No-value options
- key-value pair options
Note: In this document, during explaining syntax:
- Anything inside
[ ] is optional parameter in the syntax/examples.
<value> is a place holder, which mean it should be substituted with an actual value.
HOW TO USE getopt?
Syntax: First Form
getopt optstring parameters
Examples:
getopt "hv:t::" -v 123 -t123
getopt "hv:t::" -v123 -t123
getopt "hv:t::" -h -v123
getopt "hv:t::" -v 123 -t 123
getopt "h:v:t::g::" -h abc -v 123 -t21
getopt "hvt" -htv
getopt "hvt" -h -t -v
getopt "hvt" -tv -h
Here h,v,t are the options and -h -v -t is how options should be given in command-line.
- 'h' is a no-value option.
- 'v:' implies that option -v has value and
is a mandatory option. ':' means has a value.
- 't::' implies that
option -t has value but is optional. '::' means optional.
In optional param, value cannot have whitespace separation with the option. So, in "-t123" example, -t is option 123 is value.
Syntax: Second Form
getopt [getopt_options] [--] optstring parameters
Here after getopt is split into five parts
- The command itself i.e. getopt
- The getopt_options, it describes how to parse the arguments. single dash long options, double dash options.
- --, separates out the getopt_options from the options you want to parse and the allowed short options
- The short options, is taken immediately after -- is found. Just like the Form first syntax.
- The parameters, these are the options that you have passed into the program. The options you want to parse and get the actual values set on them.
Examples
getopt -l "name:,version::,verbose" -- "n:v::V" --name=Karthik -version=5.2 -verbose
Syntax: Third Form
getopt [getopt_options] -o|--options optstring [getopt_options] [--] [parameters]
Here after getopt is split into five parts
- The command itself i.e. getopt
- The getopt_options, it describes how to parse the arguments. single dash long options, double dash options.
- The short options i.e. -o or --options. Just like the Form first syntax but with option "-o" and before the "--" (double dash).
- --, separates out the getopt_options from the options you want to parse and the allowed short options
- The parameters, these are the options that you have passed into the program. The options you want to parse and get the actual values set on them.
Examples
getopt -l "name:,version::,verbose" -a -o "n:v::V" -- -name=Karthik -version=5.2 -verbose
GETOPT_OPTIONS
getopt_options changes the way command-line params are parsed.
Below are some of the getopt_options
Option: -l or --longoptions
Means getopt command should allow multi-character options to be
recognised. Multiple options are separated by comma.
For example, --name=Karthik is a long option sent in command line. In getopt, usage of long options are like
getopt -l "name:,version" -- "" --name=Karthik
Since name: is specified, the option should contain a value
Option: -a or --alternative
Means getopt command should allow long option to have a single dash
'-' rather than double dash '--'.
Example, instead of --name=Karthik you could use just -name=Karthik
getopt -a -l "name:,version" -- "" -name=Karthik
A complete script example with the code:
#!/bin/bash
showHelp() {
cat << EOF
Usage: ./installer -v <espo-version> [-hrV]
Install Pre-requisites for EspoCRM with docker in Development mode
-h, -help, --help Display help
-v, -espo-version, --espo-version Set and Download specific version of EspoCRM
-r, -rebuild, --rebuild Rebuild php vendor directory using composer and compiled css using grunt
-V, -verbose, --verbose Run script in verbose mode. Will print out each step of execution.
EOF
}
export version=0
export verbose=0
export rebuilt=0
options=$(getopt -l "help,version:,verbose,rebuild,dryrun" -o "hv:Vrd" -a -- "$@")
eval set -- "$options"
while true
do
case "$1" in
-h|--help)
showHelp
exit 0
;;
-v|--version)
shift
export version="$1"
;;
-V|--verbose)
export verbose=1
set -xv
;;
-r|--rebuild)
export rebuild=1
;;
--)
shift
break;;
esac
shift
done
Running this script file:
bash commandLine.sh --version=1.0 -rV
bash commandLine.sh -version=1.0 -rV
bash commandLine.sh -v 1.0 -rV
bash commandLine.sh -v1.0 -rV
bash commandLine.sh -v1.0 -r -V
-s, make it a positional argument:./myscript 45 anystring.$./myscript -s 45 -p any_string-pis actually an option (that is, your program can proceed if it's not present). In this case,./myscript 45 -p any_string. (I think thatgetoptcan handle mixed options and positional arguments, whereas thebashbuilt-in commandgetoptsrequires all positional arguments to be placed after options.)