bash and command-line argument parsing - research
This is something that I toy with at times and then usually drop to the floor, as I really need to get something done, rather than try a bunch of different options.
It is something that I'd like to figure out for me. I have it worked out for Python, but it would be nice to have something for bash as well.
different ways of parsing command-line options
Right now, I'm just listing a bunch of interesting articles for this. I haven't had a chance to use them yet (as they seem a little involved and I needed to do something more quickly - but, I want to come back to it).
After quickly reading some of these, I'm still not sure. I like the idea of getopts (note, not getopt - notice the lack of "s" -- getopts is apparently a built-in command to bash (if it's new enough, so ... something to check on MacOs) and is pretty good). Though, a simple switch case that I've seen in several articles is pretty nice as well.
Most of what I'm trying to find something like this for, is the use of the jrnl, the journaling script that I use all the time, scripts I have written to make life easier for me. Most of the time, I really don't need to parse anything (or at most, just a single argument). But, it would be nice to have more.
In the case that got me looking, the script export_jrnls_md really needs 2 parameters and it's not really set up for that. So ...
Here are some links I found. I wrote some notes for the ones I thought good or had something interesting. The others, are good support material and might provide some extra light on some things, but aren't outstanding overall.
Currently, I think these are the two options to try:
Both have really good qualities.
Resources
- How do I parse command line arguments in Bash? - this one had several different good answers and scripts to boot, so ...
- Parsing bash script options with getopts - this looks interesting, but again, not enough time
- An example of how to use getopts in bash - many examples
- Bash getopts builtin command -- good writeup and examples - I like this one - nice tutorial. Probably one of the best so far!
- Easy getopt for a BASH script - another decent one
- Small getopts tutorial
- Positional Parameters - this one has a nice description of things. (It's also lesson 12, so ... Might be a good set of articles to look at over time.) It doesn't seem to use getopt or getopts and shows how to do things, so ... Also a very good one. This might be the one to use for me, in the use-case I have/had for (export_jrnls_md) script! Actually, in general, this is a really good example of a nicely written bash script! My only nit is that some of it could be pulled out into functions better, but it's really pretty good.
- Bash Scripts - Part 3 - Command-Line Options and Switches - it has some interesting stuff, pretty basic through
- parsing command line arguments in Bash - it's sort of an intro to the github project bargs (only glanced at it)
- Parsing command-line arguments and flags - decent