DEV Community

Discussion on: Advanced Argument Handling in Bash

Collapse
 
rrampage profile image
Raunak Ramakrishnan • Edited

Thank you so much for writing this comprehensive tutorial! What do you think of GNU getopt which is also thrown in as an alternative for arg-parsing?

I recently refactored a bash script to use one positional argument and getopts for the rest of the arguments. The short options have made using the script more intuitive and the error messages much friendlier for the team.

Collapse
 
rpalo profile image
Ryan Palo

Hmm. I hadn't heard of getopt. Neat! From just a quick search/read-up, here's what I am seeing people say:

GNU getopt seems like a reasonable alternative that makes long options a little easier. It looks like the syntax may be a little more complicated, but not problematically so. The main complaint I'm seeing is that it is external to Bash, so it's not quite as likely to be as portable as getopts, which is a Bash built-in.

That being said, if it's present on the system you're working on or if you're fine installing it, then it seems like a pretty OK alternative: tidying up the things you miss when you roll your own argument parsing, while still being relatively readable.

It reminds me a little of the Python argparse functionality.

I have read that it has issues with whitespace in arguments, but I can't confirm that without trying it out.

Thanks for sharing! :)