Docopt.sh – Command-Line Argument Parser for Bash 3.2, 4, and 5
github.com
github.com
arg=default
while [ -n "${1}" ]
do
case "${1}" in
--arg)
shift
arg="${1}"
shift
;;
*)
print_help_page
shift
;;
esac
done
The above is terse, readable, and doesn't rely on any dependencies other than POSIX compatibility.Often the whole point of shell scripts is that they're dependency free; nothing is needed other than the shell. After all, why code rustup in sh if not for this reason?
People who make libraries like this for shell are, I think, missing the point of shell.
arg=
while [[ "$#" -gt 0 ]]; do
case "$1" in
--arg) shift; arg="$1" ;;
*) help; exit 1 ;;
esac
shift
doneFunny, it looks like perfectly readable, idiomatic and dependency-free shell to me.
The biggest issue I see with this is the generated code is a minified chunk of noise that can't be easily edited by hand by downstream users
Hate it's argument handling.
I forgive languages with similier capabilities because they're passing a low level array, or aren't designed with command line conventions in mind; but even then I am wishing for better! Bash already provides the args array as a native array, and has no excuse for not incorporating existing command-line help, version, and keyword-argument standards into accessible builtins: one allowing values to be a read (returning `0`, `1`, or `"string"`), and one that's just sugar for `$read_arg "foo" && { printf '%s\n' "$bar"; exit 0; }`.
Might sound trivial, but parsing arguments is a core component of Shell that simply doesn't need written out explicitly as often as it is. GNU Getopts is the best we get, while if we go down the strict POSIX path, users would apparently rather use an idiomatic loop over `shift` than a similer loop over the portable `getopt`. Wonder what ZSH has for this.
Anyways, Docopt's approach sounds pretty neat, love that 'declarative' usage text, that's slick. I want that all over the place. Don't like how hard the generated code is to read (Argbash seems to be a step ahead in that regard), but this is still great, this'd be an improvement almost anywhere, certainly anywhere where that generated code could be hidden in the implimentation, but definitely in Shell.
getopts is POSIX, and GNU getopts is an implementation of it. [0] The getopt utility is not POSIX (not the command line tool anyway, not in current POSIX) and should not be used. [1]
[0] https://pubs.opengroup.org/onlinepubs/9699919799/utilities/g...
https://github.com/matejak/argbash/blob/master/resources/exa...
The docopt.sh output looks as if it went through a JS minifier. Or is there a way to change that? If so I think it should default to that.
https://github.com/andsens/docopt.sh/blob/master/docs/naval_...
> the wider docopt project is mostly unmaintained at this point. Consider using clap or possibly structopt instead.
It's a lovely way to internalize the CLI argument cultural norms, decrease confusing and verbose argument parsing, make argument parsing work-free for the developer, and make argument parsing a copy-paste across languages. There's no greater pleasure than iteratively adding options to your program by just adding a line of text
-n, --new-option Do something new
I honestly think making a docopt parser is just very hard, which may limit its future prospects.
[the docopt rust repo.]: https://github.com/docopt/docopt.rs
https://manpages.debian.org/testing/util-linux/getopt.1.en.h...