Live data from Hacker News

Use long flags when scripting (2013)

changelog.com

91–100 of 197 posts

Re: Use long flags when scripting (2013)

#91

Earlier quoted context omitted.

tar is one of the worst. I think I know what those do without looking it up but I think most people look at that with bewilderment. Randall Munroe is always helpful in these matters and has this to say: https://xkcd.com/1168/

I remember 90% of the time which flag I want to use, but the other 10% sends me into a rage! =) I gave up and added this to my .bashrc: function extract() { if [ -f $1 ] ; then case $1 in *.tar.bz2) tar xvjf $1 ;; *.tar.gz) tar xvzf $1 ;; *.bz2) bunzip2 $1 ;; *.rar) unrar x $1 ;; *.gz) gunzip $1 ;; *.tar) tar xvf $1 ;; *.tbz2) tar xvjf $1 ;; *.tgz) tar xvzf $1 ;; *.zip) unzip $1 ;; *.Z) uncompress $1 ;; *.7z) 7z x $1…

Recent (as in, within the last ~3-5 years) versions of GNU tar automatically detect the type of compression and apply the appropriate tool, so you can often get away with `tar xvf`.

Re: Use long flags when scripting (2013)

#92
post #78

Earlier quoted context omitted.

As does Python, which drives me crazy. -V is version on Python.

Very annoying. Java HotSpot uses -version , rather than --version , which is even worse. Don't mess with the standard long-form flag!

To be fair, the JVM predates GNU-style --long flags being "standard". Lots of older programs use "X11-style" -long flags (that many pre-X11 programs used, like `find`).

Re: Use long flags when scripting (2013)

#93

Earlier quoted context omitted.

I agree. To me, "sed -e" is an expression as a whole, and replacing -e with --expression is on the same level as aliasing "sed" to "stream-editor". Though I would make the list of "allowed" short flags very short. I can't think of many more than the ones you listed: mkdir -p sh -c cp -r tar -xaf tar -caf

tar is one of the worst. I think I know what those do without looking it up but I think most people look at that with bewilderment. Randall Munroe is always helpful in these matters and has this to say: https://xkcd.com/1168/

These two commands should fit most of your use cases:

  tar cf dir.tar dir          # mnemonic: 'create file'  
  tar xf dir.tar(.gz|bz2|...) # mnemonic: 'eXtract file' 
On systems with "modern" versions of tar `-x` is capable of recognizing which compression format is used and doesn't require the explicit `-j/z` flags you usually see.

Re: Use long flags when scripting (2013)

#94

Great suggestion, but I'd go for a middle ground: use long flags for the long tail, but short flags are fine for stuff that's very commonly used. I think I'll continue to write these: grep -i rm -rf ln -s gzip -v sed -e instead of: grep --ignore-case rm --recursive --force ln --symbolic gzip --verbose sed --expression If you're working on shell scripts, you probably know certain short flags well enough that long flag…

While i agree these are "common" you now have to define what common flags are. It's easier for an organization to just have a blanket rule to use long form expressions

Also is typing that much of a chore?

Re: Use long flags when scripting (2013)

#95

Great suggestion, but I'd go for a middle ground: use long flags for the long tail, but short flags are fine for stuff that's very commonly used. I think I'll continue to write these: grep -i rm -rf ln -s gzip -v sed -e instead of: grep --ignore-case rm --recursive --force ln --symbolic gzip --verbose sed --expression If you're working on shell scripts, you probably know certain short flags well enough that long flag…

I argue that 'very commonly used' is subjective, and someone new won't know all the ones you or I consider common. One of my goals in writing software is to make my code understandable to complete newcomers. I can't always achieve it. Sometimes other design goals take priority. But from my perspective this one is a no-brainer. I just don't see the down side. If it's too time consuming to type, use them for typing practice and get faster :-)

Re: Use long flags when scripting (2013)

#96

`rm -rf` is much more recognizable than `rm --recursive --force`. `tar -cvf` is much more recognizable than `tar --create --verbose --file`. Moreover, why are you writing a shell script if it's NOT quick and dirty?

Makefiles are one situation where one is essentially writing small shell scripts, but which are not really supposed to be quick and dirty in my opinion.

Re: Use long flags when scripting (2013)

#97
post #21

Earlier quoted context omitted.

Long flags aren’t just for readability, they also add entropy which contributes defensiveness against typos.

Related to this, they're less likely to do something unexpected. Many programs use -v as a short flag for --version , but some (such as curl) use it as short for --verbose Probably something you'd catch pretty quickly, but still.

The worst (well known) offender is grep -v meaning grep --invert-match just for the sheer bafflement of "verbose" now hiding what you were looking for.

Re: Use long flags when scripting (2013)

#98

As the saying goes, code is typically written once and read thousands of times. It takes you less than a second to write the full flag name (add a few seconds if you need to look it up), but it will likely save at least a few hundred readings the time to look up the flag if they do not already know it. In addition, full flag names contributes "self-documentation" in many cases, and also potentially makes searching ea…

You're assuming the long version is more well known than the short version. That's a wildly and provably false assumption. Do you know, without referring to the manpage, exactly what `cp --no-dereference --preserve=links --recursive --preserve=all` does? I don't. I can take some guesses based on the names of the options, but those guesses could very easily miss an important corner case. Do you know what `cp -a` does?…

The long form of `-a` would be `--archive`, for the same reason that you write `-a` and not `-dR --preserve=all`
Post reply on HN