3 ms·
Good and useful advice. Just one minor thing I'd change (copied from a comment that I posted there too): Don't use -v for version. It's very uncommon (though I
by lucb1e 10y ago
Good and useful advice. Just one minor thing I'd change (copied from a comment that I posted there too):
Don't use -v for version. It's very uncommon (though I know software that does it, it's confusing) and people will often blindly add -v for verbosity. A better alternative is -V (capitalized) and of course --version. These should both work, just like -h and --help should both work.
- belovedeagle 10y agoAlso, this fits in with advice #3, use common options. Users aren't "blindly" adding -v for verbosity; they're expecting it as a common option. Admittedly, the verbose/version/something else distinction is not as standard as I'd like, but if I were writing this I would have explicitly said in point 3, "-v is verbose. -V is version. -h is help. -?, if implemented at all, is a synonym for --help. -n is dry run, if that makes sense for your command. (By the way, unknown flags should cause the command to fail rather than silently do the wrong thing.) The first instance of -- causes all further arguments to be blindly accepted as positional arguments rather than flags, including further uses of --. If at all possible, all command forwarding (e.g., ssh, shell, autocall) is done using multiple positional params, not nested quoting nightmares."
- lucb1e 10y agoAll agreed. Most importantly, unknown arguments should not go by silently and -- should be implemented.
- barbs 10y agoI find it quite annoying every time I need to check the installed version of java and `java --version` doesn't work (I need to type `java -version`).
- lucb1e 10y agoI've made that mistake at least once, if not more than once.
- OJFord 10y agoI hate `-long` style options too. It's not just aesthetic/confusing, it's objectively worse/more limited: - It means `-thing` != `-t -h -i -n -g`, so the latter has to be that long; - You also can't have `readable-long-things-as-an-option`; - You can't (or not without crazy logic and a confusing UX) have variables passed with no space, like `-ffoobar`
- lucb1e 10y agoIn that third case, I find it crazy logic and confusing UX to have values with no space. If I type mysql -uroot on the command line, that should be equivalent to mysql -u -r -o -o -t but it's not.
- okdana 10y agoWell the argument parser knows that `-u` must take a value, and the only way it can really associate it with its value is positionally (i.e., the value is the stuff that immediately follows it). So it could force you to make the value a separate argument, but that would be a purely arbitrary restriction — it couldn't allow anything but the value to follow, so it'd just have to return an error for the sake of it. This behaviour is defined in POSIX, btw: >If the SYNOPSIS of a standard utility shows an option with a mandatory option-argument (as with [-c option_argument] in the example), a conforming application shall use separate arguments for that option and its option-argument. However, a conforming implementation shall also permit applications to specify the option and option-argument in the same argument string without intervening <blank> characters.
- lucb1e 10y agoOh, I had never thought of it like that. That makes sense! I just treated those commands (like mysql) as one of those commands where -every -option -must -be -separate. I suppose I don't mind that syntax anymore now. Thanks for changing my mind :)
- Annatar 10y agoIt is the (social contract) responsibility of the programmer to document in the usage and in the manual page that -u takes a username as an argument, and where such documentation exists, the convention is that username may be provided as -uuser or -u user. This is trivially achievable with getopts(1): while getopts u: Option do case "$Option" in u) User="$OPTARG" ;; esac done The colon tells getopts(1) that the -u option expects an argument. This behavior is also mandated by the POSIX specification.
- shanemhansen 10y agoFunny story about -v. I was using pkill to get rid of some hung processes on a remote server. For some reason I decided to get verbose output and run pkill -v. Turns out -v negates the match so I killed every process on the box but one. I had to email someone to reboot the server.
- deleted 10y ago[deleted]
- justinsaccount 10y agoI've done that exact thing! Took me a few minutes to realize what happened. Luckily it was just a VM. It makes sense in the context of pgrep, since that emulates grep. But kill does not have a -v flag that means inverse.
- lucb1e 10y agoI use grep -v all the time and still think it's a really weird choice for the negate option. (It's called "invert match" in the man page; there's at least a v in there.) Arguably grep predates any common practices, but I'd have to look that up.
- matt_wulfeck 10y agoOh my gosh. I'm so glad I just read that. For the life of me I can't understand why someone would want to say "let me send a kill to every single process on this host but this one..."
- __derek__ 10y agoNo idea, but there it is: -v Reverse the sense of the matching; display processes that do not match the given criteria. I'm similarly happy that I read that before I went off and did something stupid.
- d0mine 10y agoSometimes it is easier to specify the reverse regex using the option instead of trying to invert it manually http://stackoverflow.com/questions/466053/regex-matching-by-exclusion-without-look-ahead-is-it-possible http://stackoverflow.com/questions/466053/regex-matching-by-...
- chrisfosterelli 10y agoI agree that `--version` should always be present, but I think `-v` as an alias for version is not uncommon. In fact, I usually try it before I bother with `--version`. Trying the first few programs off the top of my head I found node, ruby, docker, clang, npm, and even the man command all respond to the `-v` flag. I think that's common enough for me to support it when I don't need a verbosity toggle.
- lucb1e 10y agoHmm. I suppose if there is no default action (given no stdin input is provided) then -v might still mean verbose and print a version. Like, running grep by itself does nothing, but grep -v by itself might print a version because it's more info (more verbose) than nothing. Commands that do something without arguments, like cat (even though it just blocks, waiting for input) might still print the version to stderr I suppose. That would be a way to solve both cases.
- restalis 10y ago"I agree that `--version` should always be present" I'm not sure why version information has to have an option on its own. For humans it can be provided in help. Considering the limited usefulness it has as a separate thing, it's just command option garbage.
- paulddraper 10y agoI thought -v was exclude, like grep and pkill.
- lucb1e 10y agoThose are the exceptions to the rule in my experience. Depending on how old those tools are they might just predate the practice.
- Annatar 10y ago-v traditionally means verbose, just like -n usually means dry run, but the application's manual page is always the final and definitive authority on which options the application provides, and what those options do. The convention is to consult the manual page first.