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.
I'm a Sr. Software Engineer at Flashpoint. I specialize in Python and Go, building functional, practical, and maintainable web systems leveraging Kubernetes and the cloud. Blog opinions are my own.
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! :)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Thank you so much for writing this comprehensive tutorial! What do you think of GNU
getoptwhich is also thrown in as an alternative for arg-parsing?I recently refactored a bash script to use one positional argument and
getoptsfor the rest of the arguments. The short options have made using the script more intuitive and the error messages much friendlier for the team.Hmm. I hadn't heard of
getopt. Neat! From just a quick search/read-up, here's what I am seeing people say:GNU getoptseems 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 asgetopts, 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
argparsefunctionality.I have read that it has issues with whitespace in arguments, but I can't confirm that without trying it out.
Thanks for sharing! :)