GiselleSerate / myaliases

Useful shell aliases and functions.
6 stars 1 forks source link

options #64

Closed aryarm closed 4 years ago

aryarm commented 6 years ago

I'm very, very confused about the best way to use options in a bash script. There are a lot of competing answers on the internet and none of them seem ideal. I'm going to try explaining them here. @GiselleSerate, let me know what you think. I found a great explanation of three ways to parse options in this Stack Overflow answer. They recommend just using regular bash in the first two methods, both of which seem quite robust, if not a little verbose. The last answer, however, explains that the builtin getopts (which I presume was made specifically for this sort of thing) might be better because it's portable and can handle combining options automatically (like using -vf instead of -v -f). I looked into getopts using this tutorial. The only downside, it seems, is that it can't handle optional option arguments, like the kind we're planning to use for -h. This Stack Overflow post details some work-arounds. However, the second answer in that post describes a much better alternative called enhanced getopt, which honestly sounds like the best thing ever. It does all of the stuffs and is supposedly standard on any system. However, a comment to that answer specified that it would be bad for wrapper scipts, which is kinda what we're doing, right? They suggested this code, instead.

Could we also just use c?

GiselleSerate commented 6 years ago

C has workable options code, I can go dig it out of my homework repo. It can definitely handle optional arguments.

The only problem is linking the C to script, which is maybe a problem . . . maybe we should figure out how to use execve first.

aryarm commented 6 years ago

Ah ok sounds good

GiselleSerate commented 6 years ago

Here's the writeup for 105's cache lab, which talks about getopt, the C library we used. Also, since this is a public thread, I won't post my code that I wrote for the class, but if we want to use it for reference, we can definitely do that.

GiselleSerate commented 6 years ago

Wait wait, I maybe misunderstood your original post. Are we talking about optional flags or optional arguments? Cause optional flags are for sure a thing (actually, it would be pretty dumb if they weren't) but optional arguments are "not required by POSIX".

I don't remember, what were we using optional arguments for?

aryarm commented 6 years ago

oh hmm yeah I was talking about optional arguments. It seems like POSIX requires that if we have an argument to an option, it be required? That's upsetting. Actually, it seems like the page you linked to describes the c equivalents of the bash functions that I was talking about when I started this issue. There's this enhanced getopt which handles optional arguments in the correct way, but I bet it isn't POSIX. But apparently, "no GNU system is missing this (e.g. any Linux has it)" and it's "available on most 'bash-systems'"

aryarm commented 4 years ago

Alright, I'm going to close this issue because I feel like I now have a better handle on this.

The answer is that if you want to be super portable, you have to write your own code to handle the options. But for most cases (ie on non-Solaris machines), you can just use getopts, which is POSIX. The main disadvantage to this is that it doesn't handle long options. If you want those, you should use the enhanced getopt which does all of the things. The only downside is that it is only available on Linux machines (and not installed by default on Macs).