-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Define a convention for parsing command-line flags #10
Conversation
I think we should go with the The main reason for short flags is to make it easier on users who have to type the command names a lot, but none of our scripts are things that users would regularly type. They're called by other scripts, where we'd want long flags anyway, or they're called by users who are probably copy/pasting an example we gave them. |
Apologies for the unprompted comment! I stumbled across this and thought it was worth mentioning that we also use The benefit of @mtlynch / @jdeanwallace - Would it be worth considering |
@cghague - Good point! We should consider |
Thanks @cghague, my eyes just glazed over that one. I've updated the doc with an example using For me, this has slightly changed my preferences. If our heart is set on implementing long flag names then I would still stick with I'll leave the final decision to @mtlynch (if it's changed since #10 (comment)) |
Thanks @jdeanwallace. The naming of the two utilities certainly doesn’t help with distinguishing them! In terms of long flag names, a big bonus of them is they’re less scary to users. It’s a lot more reassuring to run |
@jdeanwallace - Yeah, I'd rather re-use existing tools, but reading the write-up, Can we move the discussions of pros/cons to the PR description and update the style to just be the convention we're choosing? Since this is a style PR, it'll go to whole team for review, but I want to have one clear proposal. |
@mtlynch - I've updated the PR description to outline our discussion and reduced the code change to only detail the convention chosen. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Automated comment from CodeApprove ➜
In: Discussion
- Using custom implementation A
- Using custom implementation B
Can we call these, "Using a custom implementation for long flags" and "Using a custom implementation for short and long flags"? It took me a few seconds of comparing the two snippets to spot what the difference was.
In: Discussion
Code is easier to read and write
Can we add "easier to read and write than getopt
/ getopts
?
In: README.md:
> Line 175
echo 'Help is on its way'
Can we put in an example of printing usage information?
In practice, we're probably going to come to this page every time we implement a new bash script that takes flags, so it's handy if we can just copy a full, realistic example and replace the parameter names.
We don't have to do a full example in the PR description, just in the README itself.
👀 @jdeanwallace it's your turn please take a look
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Automated comment from CodeApprove ➜
In: Discussion
Done.
In: Discussion
Done.
In: README.md:
Done.
👀 @mtlynch it's your turn please take a look
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Automated comment from CodeApprove ➜
LGTM!
Can we share it with the team for wider review?
👀 @jdeanwallace it's your turn please take a look
Hey team, when you each get a chance could you review this style proposal for parsing shell command-line flags? |
Automated comment from CodeApprove ➜⏳ @cghague please review this Pull Request |
Automated comment from CodeApprove ➜⏳ @jotaen4tinypilot please review this Pull Request |
Automated comment from CodeApprove ➜⏳ @db39 please review this Pull Request |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Automated comment from CodeApprove ➜
LGTM!
👀 @cghague, @jdeanwallace, @jotaen4tinypilot it's your turn please take a look
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Automated comment from CodeApprove ➜
👍 I also feel this is the best approach for us. (And it’s really a bummer that flag parsing is generally so all over the place in shell/bash…)
👀 @cghague, @jdeanwallace it's your turn please take a look
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Automated comment from CodeApprove ➜
⏳ Approval Pending (1 unresolved comments)
Approval will be granted automatically when all comments are resolved
LGTM
In: README.md:
> Line 198
>&2 print_help
I don’t think I’ve ever seen redirection to STDERR formatted in this way before. I’ve checked a few of our other scripts at random and we always put the redirection at the end. Should this be changed?
That said, I do like this, it looks so much tidier over multiple lines. If we could get this adopted globally I’d be all for it!
👀 @jdeanwallace it's your turn please take a look
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Automated comment from CodeApprove ➜
In: README.md:
> Line 198
>&2 print_help
Yeah I like it too. One other place we use it like this is in bundler/verify-bundle
. So I don't think it's wrong, we just haven't defined a convention for it yet...
Until now 🤠 #13
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Automated comment from CodeApprove ➜
Approved: I have approved this change on CodeApprove and all of my comments have been resolved.
Resolves #3
Looking at the shell scripts in our repos, we seem to parse command-line flags in basically four different ways. This PR compares these methods and picks a convention based on our preference.
1. Using
getopts
Pros:
getopts
commandscript.sh -abcd
)Cons:
getopts 'ht:f'
)Example:
2. Using
getopt
Pros:
=
(e.g.script.sh --target-file=/tmp/file
)Cons:
getopt
command (i.e., not available on macOS)For example,
script.sh -target /tmp/file
results inTARGET_FILE=arget
.This isn't an issue in the other parsing methods.
Example:
3. Using a custom implementation for long flags
Pros:
getopt
/getopts
Cons:
=
Example:
4. Using a custom implementation for short and long flags
Pros:
getopt
/getopts
Cons:
=
Example:
Source material