This repository details the somewhat automated way in which I have created helper scripts to help myself templatise the parsing of command line arguments (both short and long) in a BASH script. The files it contains are the following:
-
init.sh: This script actually is used to create the scriptparse-command-line.sh. It is present just to make life a bit easier. In reality it is theparse-command-line.shwhich does all the heavy lifting. -
parse-command-line.sh: This script is the script which parses your command line arguments. When using it, it exposes 4 main variables to be used:map_long_to_short: This is an associative array which maps long options to short options (e.g--aye=>-a).map_short_type: This is an associative array which maps short options to a "type". In the current implementation the current types I support are "k" and "v"."v" type: This means that the short option requires a value. This is similar in concept to getopts when you are specifying the option string and you must put a ":" after the character (e.g"a:b:c:")."k" type: This means that the short option is only a key. In other words, it functions as a true/false switch. This is similar in concept to getopts when you are specifying the option string and you don't put a ":' after the character (e.g "d")
map_runtime: This is an associative array which picks up the options we inputted on the command line and the corresponding values for those options. It does this as we run the script (so I thought a fitting name was "runtime").ARGC: This is the number of command line arguments which was entered. for example:- Entering:
-a a_val -b b_val -c "cee val" -dwould mean thatARGCequals 7 - Entering:
--aye "a long" --bee "b long" --dee --would mean thatARGCequals 6 (because in this case we have the extra--which wasn't entered in the previous example).
- Entering:
Moreover, the script also exports a useful function called
print_arraywhich you can use to print out (in JSON format) what an associative array looks like. -
main.sh: This is just an example script to show the usage of how to referenceparse-command-line.shand the variables and functions which are exposed. the script is the following:#! /usr/bin/bash . ./parse-command-line.sh "$@" shift $ARGC # printing out variables which are defined in parse-command-line.sh echo "ARGC: $ARGC" print_array "map_long_to_short" print_array "map_short_type" print_array "map_runtime" echo $1
You can try running this with the example
-a "short_a" --bee "long bee" --cee "long c" -d -- "hello world"
To use the repository by entering quoted positional paramters to the init.sh in the following form: <optional long parameter>:<required short parameter>:<k or v>. For example:
./init.sh "aye:a:v" "bee:b:v" "cee:c:v" "dee:d:k" ":e:v"
This create the parse-command-line.sh file with the following meanings:
--aye or -arequires a value--bee or -brequires a value--cee or -crequires a value--dee or -dis a switch-edoes not have a long form but requires a value
Generally It might be easier to directly call the maps which are pre-configured for you like in the example main.sh above. However, I'm not going to stop you from directly copying the content of the parse-command-line.sh into your script 😀! Up to you how you want to use this code.