Mind Dump, Tech And Life Blog
written by Ivan Alenko
published under license Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)copy! share!
posted in category Systems Software / Bash & Shells
posted at 03. Sep '17
last updated at 24. Aug '21

Fuck getopts of Bash (angry rant)

Fuck getopts in Bash, because I feel like I wasted 4 hours of my life now.

Example parser:

#!/bin/bash
while getopts "h:p:" opt; do
  echo 'opt name'
  echo $opt;
  echo 'index'
  echo $OPTIND
  echo 'optarg'
  echo $OPTARG;
done

Expectation: parse command line options

I specify required paramaters and getopts handles it.

./mysqlstuff -h -p
-h requires an argument

Reality: doesn’t even work properly for missing parameters

./parser -h -p

opt name
h
index
3
optarg
-p

Instead it takes -p as a parameter. Wtf? How should I handle that? And why? I specified in options before it requires an argument, no? This is exactly why I wanted to choose an option parser. I want to die…

I realize, that option like -p in mysqldump requires special treating, because it can take an argument or not, but that kind of requirements can be handled with custom code.

I really find it depressing when technology works in a retarded way and is slowing me down. Fuck, I just want to get things done, not care about parsing options of my script. Fuck this world. I get angry, because I’m powerless.

I don’t really have much time to work on open source projects - “How have you contributed this year to open source world?” “I wrote a blog post about how I wasted my time with stupid technology.”.

I maneuver a lot to avoid this situations, but I just expected a bash internal command with lots of examples here http://wiki.bash-hackers.org/howto/getopts_tutorial. It hurts a lot when I realize too late that it won’t work and I invested so much time and effort.

Why to use an option parser anyway? To avoid mistakes in the future. I expect to forget how everything works and strict argument checking is gold. Also it’s for other people who will work with project.

Fuuuuck. I’m going to try to use GNU getopt [sic], not getopts, now.

Add Comment