Others
Utilities
You want to write a script that does argument parsing and don’t like Argbash or you just want to find out how awesome it really is?
Then read through this list of influences and/or alternatives!
Python
argparse: The main inspiration: https://docs.python.org/3/library/argparse.htmlPros:
Works really well.
Has more features.
Cons:
It is Python, we are
bash.It has a strict value restriction turned on (analogous to having
ARG_RESTRICT_VALUES([no-option-all])in every script) that can’t be switched off.
Argbash says:
We handle the boolean options better.
We have the awesome wrapping functionality.
bash—shflags: Thebashframework for argument parsing: https://github.com/kward/shflagsPros:
It works great on Linux.
Cons:
Doesn’t work with Windows
bashDoesn’t support long options on OSX.
Argbash says:
We work the same on all platforms that have
bash.
getopt: Eternal utility for parsing command-line. This is what powersshflags.Pros:
The GNU version can work with long and short optional arguments.
Cons:
Its use is discouraged — it seems to have some issues, you still need to deal with positional arguments by other means.
getopts:bashbuiltin for parsing command-line.Pros:
Being included with
bash, it behaves the same on all platforms.
Cons:
Supports only short optional arguments.
Argbash says:
argbash>=2.7.0can generate code that usesgetoptsin the POSIX mode, so there is no reason any longer to write that code manually.
EasyOptions:Rubyutility with abashinterface as well as its purebashimplementation: https://github.com/renatosilva/easyoptionsPros:
Very simple to use.
Very elegant.
Cons:
You have to distribute
easyoptions.shwith your script.The library itself provides only very basic functionality.
The project does not seem to have any tests (as of 07/2016).
Argbash says:
We have more of nice features.
We offer an option to produce battery-included scripts.
Our declarations as not so elegant, but they are not bad either.
The parsing part of the script generated by
Argbashis only as complex as necessary.
bash-modules- moduleargument:bash-modulesaims to become a standardbashframework and comes with an argument-parsing treat: https://github.com/vlisivka/bash-modulesPros:
Seems to have nice features.
Cons:
bash-moduleshave to be available at run-time to run the script.The documentation is poor (as of 07/2016).
Argbash says:
We have good documentation with examples.
We offer an option to produce battery-included scripts.
docopt: Umbrella project of parsing code modules for various languages. The interface is based on help messages: http://docopt.org/ The shell interface is called docopts and it is implemented as a standalone executable (in fact a Python script) that consumes the help message of the script it is used in, passed arguments, and exposes collected values via environmental variables.Pros:
The help message as an interface is convenient.
Cons:
You need
docoptsinstalled in every environment where you run scripts that use it.
Argbash says:
Argbash is compatible with docopt — when using Argbash, you can go the
docoptway whenever you want .
Learning resources
Do you want to write the argument-parsing part of your script yourself or you want to improve Argbash?
Then read through this list of high-quality learning resources!
Best practices in argument parsing: http://www.shelldorado.com/goodcoding/cmdargs.html Don’t miss the list of short options and their common meaning!
StackOverflow thread about argument parsing: https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash It was mainly this thread which inspired
Argbash. There are plenty of recipes and suggestions available. If you are a happy user ofArgbash, consider upvoting the answer that promotes it since more people knowArgbash, the better for them.Argument parsing for dummies: https://wiki.bash-hackers.org/scripting/posparams A short and nice-looking introduction with all basics covered (wiki).
The POSIX conventions — the reason why
getoptsare still mentioned in bash learning resources.