Command Line Argument Checker Library for Bash

I am looking for a reusable code snippet that checks for a command line argument for bash.

Ideally, something similar to the functionality offered by the Apache Commons CLI:

Commons CLI supports various types of parameters:

  • POSIX-like options (e.g. tar -zxvf foo.tar.gz)
  • GNU as long parameters (i.e. du --human-readable --max-depth = 1)
  • Short options with a value (gcc -O2 foo.c)
  • long options with one def (i.e. ant -projecthelp)
  • ...

and automatically generates a usage message for the program, for example:

usage: ls -A,--almost-all do not list implied . and .. -a,--all do not hide entries starting with . -B,--ignore-backups do not list implied entried ending with ~ -b,--escape print octal escapes for nongraphic characters --block-size <SIZE> use SIZE-byte blocks -c with -lt: sort by, and show, ctime (time of last modification of file status information) with -l:show ctime and sort by name otherwise: sort by ctime -C list entries by columns 

I would include this piece of code at the beginning of my Bash scripts and reuse it in the scripts.

There must be something like this. I donโ€™t think we are all writing code with this effect or similar:

 #!/bin/bash NUMBER_OF_REQUIRED_COMMAND_LINE_ARGUMENTS=3 number_of_supplied_command_line_arguments=$# function show_command_usage() { echo usage: (...) } if (( number_of_supplied_command_line_arguments < NUMBER_OF_REQUIRED_COMMAND_LINE_ARGUMENTS )); then show_command_usage exit fi ... 
+4
source share
1 answer

This is the solution I am using (I found it on the network somewhere, perhaps here myself, I donโ€™t remember exactly). Note that GNU getopt ( /usr/bin/getopt ) supports single long parameters ( ant -projecthelp style) with the -a option, however I did not use it so it would not be shown in the example.

This code parses three parameters: --host value or -h value , --port value or -p value and --table value or -t value . If the required parameter is not set, it will be

 # Get and parse options using /usr/bin/getopt OPTIONS=$(getopt -oh:p:t: --long host:,port:,table: -n "$0" -- " $@ ") # Note the quotes around `$OPTIONS': they are essential for handling spaces in # option values! eval set -- "$OPTIONS" while true ; do case "$1" in -h|--host) HOST=$2 ; shift 2 ;; -t|--table)TABLE=$2 ; shift 2 ;; -p|--port) case "$2" in "") PORT=1313; shift 2 ;; *) PORT=$2; shift 2 ;; esac;; --) shift ; break ;; *) echo "Internal error!" ; exit 1 ;; esac done if [[ -z "$HOST" ]] || [[-z "$TABLE" ]] || [[ -z "$PORT" ]] ; then usage() exit if 

An alternative implementation using the built-in getopts shell (this only supports small options):

 while getopts ":h:p:t:" option; do case "$option" in h) HOST=$OPTARG ;; p) PORT=$OPTARG ;; t) TABLE=$OPTARG ;; *) usage(); exit 1 ;; esac done if [[ -z "$HOST" ]] || [[-z "$TABLE" ]] || [[ -z "$PORT" ]] ; then usage() exit if shift $((OPTIND - 1)) 

Further reading for GNU getopt and getopts bash builtin

+5
source

All Articles