Moved to skeeto/scratch/misc
Last active
August 29, 2021 22:22
-
-
Save skeeto/d8b18fb4663f4a282e8c0db252a70faf to your computer and use it in GitHub Desktop.
quniq
Thanks again for another great reply - and what a cool reverse()
function! I also noticed that your passphrase code imports your own optparse
library. The configuration struct
idea seems pretty straight forward.
The functional
version it easier for me to follow. The chaining concept looks really useful. I am going to experiment with it. OTOH, the interface
version seems syntactically cumbersome and is harder for me to follow. I think ease of use is going to win out for me vs potential flexibility.
Yeah, I really don't like Go's flag package, neither the API nor UI.
GNU-style options are just too good to give up, and all the other option
parsing packages I could find with better UIs still had poor APIs. So I
filled that gap with nullprogram.com/x/optparse. It's essentially a port
of my C library of the same name: https://github.com/skeeto/optparse
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/skeeto/passphrase2pgp/blob/870b0a4/passphrase2pgp.go#L83
That struct is filled out by the option parser and gets passed around the program so that the different components can access the parts of the configuration they need. If I add or remove an option I don't have to change all the call sites to add/remove parameters/arguments.
Here are two approaches: functional and interface. I'll start with the one I prefer:
Note how I start with the identity filter, then chain on new filters dynamically. Your option parser could append new filters as it parses arguments, so that the order of filters depends on the order the of arguments. Also notice how I could use
strings.ToLower
directly because it was already afilter
.Here's the interface version:
Quite a bit more verbose, but potentially more flexible if you filters become more complex. In the function version the substring filter captured its configuration in a closure. In the interface version it's stored on a struct.