TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Use long flags when scripting

423 点作者 adamstac超过 12 年前

25 条评论

npsimons超过 12 年前
While it is generally a good idea to write readable code, it is also true that long flags are not universally supported, and the short flags don't always have the same meanings (or even exist!) across different systems.<p>This is an interesting conundrum. As has been pointed out, POSIX specifies standard options (which are all short):<p><a href="http://news.ycombinator.com/item?id=5165058" rel="nofollow">http://news.ycombinator.com/item?id=5165058</a><p>But these are not universally supported:<p><a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?ps" rel="nofollow">http://unixhelp.ed.ac.uk/CGI/man-cgi?ps</a><p>What to do? My proposal: do what we do in the C/C++ world when presented with portability problems: abstract it away. POSIX sh supports functions, which can be named appropriately for readability, while calling the proper arguments and commands for different platforms. To avoid unnecessary duplication of platform/argument detection, setup could be done in a function called before anything else to create variables with the appropriate command names and arguments.<p>This may all seem too involved, but realize that we are talking about creating robust software that is to be maintained, so applying proper software engineering principles seems appropriate.
评论 #5165810 未加载
评论 #5165312 未加载
评论 #5165677 未加载
评论 #5166799 未加载
评论 #5167273 未加载
ralph超过 12 年前
This is very bad advice. I won't thank you for making me check GNU grep's --ignore-case is precisely the same as the very well-known, POSIX'd, -i. And ditto for all the other verbiage that clutter and obfuscate the script's intent.<p>Short options should be used where they're the more typically known and standardised. Long options are for the unusual.<p><pre><code> grep -iv sed -n tr -dc awk -f ls -tr comm -23 tail -n</code></pre>
评论 #5166022 未加载
评论 #5165483 未加载
评论 #5166422 未加载
评论 #5169127 未加载
taylorfausak超过 12 年前
Agreed! It saves a trip to the man page.<p>Something else I figured out recently is that if you put the pipe at the end of a line, you don't need a backslash:<p><pre><code> echo thing | grep thing</code></pre>
评论 #5164625 未加载
评论 #5170059 未加载
评论 #5170008 未加载
kps超过 12 年前
No. Use standard POSIX flags to invoke standard POSIX functionality, so your scripts don't break when run on a different system.
评论 #5164732 未加载
评论 #5164998 未加载
评论 #5164685 未加载
dclowd9901超过 12 年前
As a part-time scripter (is there any other kind?) I'm ashamed to say that I often use shortcut flags because I looked up the functionality I needed and copy pasted without taking the time to even understand what all of the flags mean.<p>This advice is good not only for others but setting a standard for yourself where you take a moment to learn what exactly is going on.
评论 #5164724 未加载
评论 #5164833 未加载
hnolable超过 12 年前
Disagree. There isn't and shouldn't be any hard fast rule for this. Same as most things in programming. Do what makes sense.<p>Also, [0-9.] not [0-9\.]
评论 #5164719 未加载
评论 #5164720 未加载
shizcakes超过 12 年前
This is a practice I use; in my opinion it is akin to descriptive variable names.
rohit6223超过 12 年前
I write a lot of shell scripts as part of my daily Sys Admin work. In my opinion, the author gave us choice to write less codes &#38; readable code by using short flags for most of the command functionalities.<p>Consider something like this: $ rsync -qaogtHr example.com:/opt/data/ /home/backup/<p>1. Elaborating all the short flags into long ones will increase readability by explaining this statement but at the cost of more lines of code.<p>2. All UNIX commands have options to combine short flags. For eg: ls -l -t -r ./ can be written as: ls -ltr ./ This also helps in reducing code. Writing less code helps in managing it more easily.<p>Long options may help beginners, but once they get comfortable, Short flags may seem more readable!!
opk超过 12 年前
For well known flags like grep's -E, this makes it less obvious what the script is doing. Not everyone speaks English as their first language and most of the long options are GNU specific so won't be portable.
nathanstitt超过 12 年前
A secondary benefit not mentioned in the article is immutability.<p>Long flags on a command line utility <i>should</i> never change. Short flags may be modified between major versions of a utility, which would break scripts.<p>Of course this is just a convention, and I'm sure it's not followed 100%. I'll take whatever protection I can get though.
评论 #5167284 未加载
6ren超过 12 年前
Ironically, tab-completion works on the command-line for long options, but not in a script (I'm sure vim can do it though).
评论 #5164639 未加载
评论 #5164643 未加载
xutopia超过 12 年前
I recently found myself going back and changing a script I had done to use the long form flags because I had forgotten what the short ones meant.
natefinch超过 12 年前
This is very good advice. You may not be maintaining your script later on, maybe someone who is new to unix will be asked to go change it. Having the intention much more obvious will save them some hassle. Hell, even someone who uses unix a lot may not know every flag of every command you use. With no other context, it's pretty hard to know if -r is recursive or reverse sort, or something else entirely.<p>And god forbid you <i>think</i> you know which one it is and end up being wrong... you might not realize until it hits production (scripts not always being the best tested things in the world).<p>Yes, they might not be supported on BSD.. they're also not supported on Windows, what's your point? It's a lot more likely you'll have to read and/or change the script 6 months down the line when you've forgotten everything about what's in it, then you'll all of a sudden need to run your script on BSD when you've never had to in the past (obviously if portability is a requirement from te beginning, then that changes how you write the script from the start).
pacala超过 12 年前
Add auto-complete to the command line and everyone will start using long form without the need to beg. Imperfectly remembering the flag spelling is a rather large annoyance, short flags avoid this problem at the cost of crypticism, autocomplete actually solves it.
评论 #5165067 未加载
评论 #5165487 未加载
shurcooL超过 12 年前
Or, there should be an automated tool that takes scripts with short flags and displays them with automatically replaced equivalent long flags for readability.<p>Not everything has to be static text, one format does not fit all situations.
james-manning超过 12 年前
At least for scripts that are on github, it seems like this could be potentially tackled by creating a robot that submits pull requests for such scripts, changing the options passed from short to long. For people fine with the change, they could just merge the PR, and for those that don't like it, people still have a long-option version of the script they can check out in the PR.<p>I've not written a github robot, though, this is just based on my vague understanding from the robots already out there (whitespace, .gitignore, etc), so please correct me if this isn't actually feasible. :)
webreac超过 12 年前
I think the short flags are more useful when using directly the command (outside of a script). It is faster to type man grep in a window and to type grep -Eo than to type grep --extended-regexp --only-matching. At first, I even didn't realize that --extended-regexp was the same as the usual -E flag. I had to check with man.<p>Theoretically, I would like to agree that long flags are better than short ones in scripts. In practice, I prefer grep -E. And I can not imagine a tar cvfhz with long flags.
pmattos超过 12 年前
I too have been using this practice for years now, unfortunately most coworkers don't seem to appreciate the genius of it.
captn3m0超过 12 年前
Time to update some of my scripts.
darrikmazey超过 12 年前
My age probably shows in this statement, but it seems odd to me to suggest that someone else change their behaviour so the poster does not have to rtfm. We're coders. When we don't understand something, we read the manual, then the source.
jstanley超过 12 年前
I accept --silent, but almost everybody knows what -E to grep means. Normally I would just use egrep rather than grep -E however.
joshbaptiste超过 12 年前
heh.. I have never thought of that. I basically script exactly how I construct commands in the cmd line, but the author is correct looking at my past bash scripts it would be better to use long options, good stuff.
blktiger超过 12 年前
Amen!
evandrix超过 12 年前
if you can't read the short flags, you shouldn't be reading my code.
evandrix超过 12 年前
there's always a "man" page (and Google) to look it up, stupid.