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.html- Pros:
- 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.
- It is Python, we are
- Argbash says:
- We handle the boolean options better.
- We have the awesome wrapping functionality.
- Pros:
bash
—shflags
: Thebash
framework for argument parsing: https://github.com/kward/shflags- Pros:
- It works great on Linux.
- Cons:
- Doesn’t work with Windows
bash
- Doesn’t support long options on OSX.
- Doesn’t work with Windows
- Argbash says:
- We work the same on all platforms that have
bash
.
- We work the same on all platforms that have
- Pros:
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.
- Pros:
getopts
:bash
builtin for parsing command-line.- Pros:
- Being included with
bash
, it behaves the same on all platforms.
- Being included with
- Cons:
- Supports only short optional arguments.
- Argbash says:
argbash>=2.7.0
can generate code that usesgetopts
in the POSIX mode, so there is no reason any longer to write that code manually.
- Pros:
EasyOptions
:Ruby
utility with abash
interface as well as its purebash
implementation: https://github.com/renatosilva/easyoptions- Pros:
- Very simple to use.
- Very elegant.
- Cons:
- You have to distribute
easyoptions.sh
with your script. - The library itself provides only very basic functionality.
- The project does not seem to have any tests (as of 07/2016).
- You have to distribute
- 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
Argbash
is only as complex as necessary.
- Pros:
bash-modules
- moduleargument
:bash-modules
aims to become a standardbash
framework and comes with an argument-parsing treat: https://github.com/vlisivka/bash-modules- Pros:
- Seems to have nice features.
- Cons:
bash-modules
have 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.
- Pros:
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
docopts
installed in every environment where you run scripts that use it.
- You need
- Argbash says:
- Argbash is compatible with docopt — when using Argbash, you can go the
docopt
way whenever you want .
- Argbash is compatible with docopt — when using Argbash, you can go the
- Pros:
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
getopts
are still mentioned in bash learning resources.