-
Notifications
You must be signed in to change notification settings - Fork 53
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
Generate getops string from docopt #19
Comments
(My2cents, I am not the maintainer of this projects) |
Yeah, I could see it as a separate tool. My main rationale for wanting it built in is that 99% of the work for such a feature is going to be parsing the docopt. Generating a getopts string from the "known/parsed" options structure is near trivial at that point (considering how limited the getopts features are) And I'd worry that a completely separate tool would deviate from proper docopt parsing too much and cause problems. It might also be worthwhile as a tiny feature for the website itself. That would scratch my itch, hopefully keep the docopt parsing where it belongs, and not expand the scope of any one docopt tool. 🤷♂️ |
@jasonkarns, I agree entirely, but then understanding which are the right boundaries for a tool is an art. |
@jasonkarns, yes interesting. Could you provide some fake bash scrips that would contain your expected code usage? My oldstyle getopts scripts looks like (the example may not work, it's just for a sample): usage() {
cat << EOT
Usage:
banip [-w|-r|-s|-u] IP
banip -l
Options:
-h display this help
-s display IP status
-u unban given IP
-w add IP to whitelist
-r remove IP from whitelist
-l list all banned IPs
EOT
}
while getopts "hs:u:w:r:l" OPTION
do
case $OPTION in
h)
usage
exit 0
;;
s)
status=true
the_ip=$OPTARG
;;
u)
unbanip=true
the_ip=$OPTARG
;;
w)
whitelist_ip=$OPTARG
;;
r)
unwhitelist_ip=$OPTARG
;;
l)
list_ips=true
;;
*)
exit 1
;;
esac
done
shift $(($OPTIND - 1))
# do actions after How would you think to use docopts as |
@Sylvain303 I mean completely outside the code. If it were part of a CLI, my ideal usage might be something like:
So it would read in the bash script, parse out the docopt, and then print the corresponding getopts string to STDOUT. That way I could just copy/paste the getopts string into the script. Or alternatively, I could just paste my docopt into the http://try.docopt.org/ website, and expect to see the corresponding getopts string printed on the site somewhere. (Where I'd just copy/paste the getopts string into my script.) My rationale for this is in scenarios where including docopt itself directly is difficult (or limited for other reasons). So it wouldn't be generating the getopts string "live" as part of the code. If I could use docopt in my script directly, then I wouldn't have to use getopts at all. I'd be able to use docopt for the arg parsing itself. |
@jasonkarns OK. So we are in the case where The first reason shouldn't be one, due to the golang version which should be able to run anywhere, and cross-compile from every serious compile environment, or VM. For the second, I guess it's a concern about disagree to use
If we (I) code the idea to provide an helper for outputting
given the actual supported options. But, may be we should encourage to use Could you provide example of the limited case, please? |
@jasonkarns any news about your request? Could you explain more?
What scenarios make « including docopt itself directly is difficult »? When you mention Or are you talking about the For me, See also #9 for more exchange on the standalone discussion. If you think that your request is no more necessary, could you close this issue, please. |
My use cases are the following: development-related scripts (ie, those that live in rbenv-* and nodenv-* (the core tools as well as a handful of plugins to each). These are installed by users and run on their workstations. No dependency manager can be assumed and these tools need to be self contained. It's not acceptable to require the users to first install docopts binary. But this is the case where the option parsing is typically done with getopts last case is understandably rare... I maintain a few bats related libraries that contain helpers (mocking, assertion helpers, etc) for the BATS bash testing tool. Most of these libraries are bash functions. I leverage docopt to describe the function signature. (It's a fantastic way to conform to conventional docs.) But these functions are sourced into BATS and I'm not happy with also requiring docopts binary being available to the user's test suite. (Again, for dep management concerns. Users of bats might be testing bash utilities; but they may also use bats for testing ruby or js clis. In those cases, they're probably using npm or rubygems for their deps. Getting docopts binary available to bats outside of the user's existing dep management tool is not trivial.) But being able to document the helper functions' signature with docopt, and then automatically generate the getopts string, to paste into the function would be 🔥 |
@jasonkarns thanks for the detailed use case. I'm not convinced by the argument "No dependency manager can be assumed and these tools", because I come from sysadmin world, and I think the every tool should be packaged. So I feel installing a software with I must admit that That said, I also understand the need to go "without" dependency. But, it tends to produce strange code. I will work on the I also created #22 for packaging this tool, so it would become easy to install it as part of other tools. ;-) Thanks for your contribution. |
Agree. Mac users typically install the rbenv/nodenv stuff with homebrew. Most linux installs are by a git clone + install.sh script. Rbenv and others are available via other means. But as the maintainers of the tool, we can't assume any particular method of installation.
Indeed. But for my purposes, most of my usecases aren't "big enough" to warrant the dep. But at the same time, I like the clarity and consistency of docopt style. So I continue to use docopt for documentation/usage/help. But hand-code the parsing. (As you can imagine since the parsing is typically done with getopts, the options are fairly limited so docopts wouldn't really be providing that much value for these scenarios. A bigger options surface area would increase the value of docopts and therefore make it worth the cost of adding a dep. |
I love docopt but there are certain scenarios where I can't leverage docopt for the parsing, but still use docopt for documenting a script.
In these scenarios, I frequently need to use getopts for actual option parsing. It would be awesome if one could generate the getopts string from docopt. (Or vice versa.)
This way I could generate the string and either paste it into my getopts usage, or at least compare and verify that my docopt is documenting correctly per my getopts string.
Of course, getopts doesn't support long flags. So attempting to generate getopts string from docopt which uses long flags would be an error.
The text was updated successfully, but these errors were encountered: