fedoramagazine.org
2 votes c/freepost Posted by tummy — 2 votes, 8 commentsSource

The first option looks incredibly silly as implementing the interface properly is not trivial

I have become so used to using getopts, I don’t really find any problems setting it up.

https://argbash.readthedocs.io/en/latest/others.html?highlight=getopts#learning-resources

It looks like argbash has put quite a bit of thought into this.

This looks interesting, but I don’t see the benefit right away. Is there a side-by-side comparison between getopts and argbash or should I just wait until my next task to try it out?

I try to use only things that default installed with Ubuntu to keep dependencies low. But it seems to fix a problem I do not have in that urgency.

They seem to have a few practical reasons explained in their README.

I am still not convinced. There is no real problem to fix. and a Docker image?!? WTF?!

I use the following bash only approach to parse arguments for a while now and it works for me fine

#!/usr/bin/env bash
# Based on: https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash
#
set -euo pipefail
IFS="$(printf '\n\t')"

# set default values
extension=""
searchpath=""
libpath=""
default=false

declare -a positional=()

while [[ $# -gt 0 ]]
do
key="$1"

case $key in
    -e|--extension)
    extension="$2"
    shift # past argument
    shift # past value
    ;;
    -s|--searchpath)
    searchpath="$2"
    shift # past argument
    shift # past value
    ;;
    -l|--lib)
    libpath="$2"
    shift # past argument
    shift # past value
    ;;
    --default)
    default=true
    shift # past argument
    ;;
    *)    # unknown option
    positional+=("$1") # save it in an array for later
    shift # past argument
    ;;
esac
done
set -- "${#positional[@]}" # restore positional parameters

echo "FILE EXTENSION  = ${extension}"
echo "SEARCH PATH     = ${searchpath}"
echo "LIBRARY PATH    = ${libpath}"
echo "DEFAULT         = ${default}"

This is more or less the standard way but it’s also a lot of code. Plus, bash scripts are cryptic anyway…

Na, bash script can be written clear and concise but most people don’t case. The code I posted is WAY shorter than Argsbash code

This just reminded me of docopt but I’m not sure they have a bash port.