When to Use Long (Word) or Short (Letter) Command Line Flags?
I tend to use what's in muscle memory for running adhoc commands in a terminal but long flags when writing scripts or documentation.
Someone recently DM’d me and asked:
Hi Nick, is there a difference or best practice between using word or letter options, example
--all
vs-a
?
I don’t think there’s any hard rules around this.
When running ad hoc commands on the terminal I’ll use whatever is in muscle memory. Sometimes it’s the short version and sometimes it’s the long version.
I prefer using the long version in scripts because it acts as a form of documentation, it’s much more descriptive. Likewise if I’m reading documentation I much prefer seeing the long version because it might save me a trip of having to Google something.
For example I know exactly what yarn add --dev some-package
will do but since
I run all of my web apps in Docker and almost never run yarn add
directly,
depending on when I read your docs I might not know what yarn add -D some-package
does.
Does it add it as a dev dependency or maybe delete old versions before adding the new one? That becomes something I would Google where as the longer word version gives me just enough context to know what it is without Googling.
This video goes into a few examples and adds a lot more detail on what goes through my mind to make a decision on which one to use. There’s even edge cases too.
# Going Over a Few Examples and Use Cases
Timestamps
- 0:21 – This is very opinion based but I think there’s guidelines to follow
- 0:31 – Running ad hoc commands on the terminal? Use whatever is in muscle memory
- 2:29 – Writing a script? Almost always use the long form flags
- 3:46 – Writing docs? Prefer the long form flags but sometimes short is ok
- 5:54 – I broke my own rule out of habit and took curl knowledge for granted
- 7:37 – It might be ok because there’s docs explaining what it does right after
- 8:01 – You have to draw the line somewhere with your docs
- 8:48 – Recap and questions?
Reference Links
- https://github.com/nickjj/lcurl
- https://github.com/nickjj/esbuild-copy-static-files
- https://nickjanetakis.com/blog/creating-an-esbuild-plugin-to-efficiently-copy-static-files
- https://nickjanetakis.com/blog/lcurl-is-a-script-to-visit-a-site-every-x-seconds-using-curl
What’s your strategy for choosing one over the other? Let me know below.