3 ms·
> Just Enough Batteries Included This is just an anecdote, but recently I decided to give golang a spin, and wrote a small command line utility in it. I won’t
by oefrha 7y ago
> Just Enough Batteries Included
This is just an anecdote, but recently I decided to give golang a spin, and wrote a small command line utility in it.
I won’t tell you the pain of trying to find an argument parser with support for GNU-style long options. I suppose that’s something I should write myself rather than use the standard library or find a third party library, because simplicity. /s Edit: folks posted helpful suggestions, I take this paragraph back.
Instead, let’s talk about something basic, regexp. Turns out golang’s regexp package supports named capture groups, but if you want to actually refer to the capture groups by names, you need to manually pair FindStringSubmatch result with SubexpNames in a loop. Can’t make this shit up. Seriously, what’s the point of named capture groups if you can’t refer to the groups by names? If this is just enough battery included, your expectations might be a tad too low.
- dev_dull 7y agoFor CLI it’s true to the Go mantra. If the flags library is “good enough” why not just use it and avoid being fancy? Besides, isn’t it feasible someone might confuse long and short options? “Is -d and —delete the same, or is -d —debug??” KISS even if it’s annoying. As for loops, I know that they’re generally more common in Go. Another example is finding if an element exists in a slice. Most of the implementations is this stuff is just a loop under the hood in the end. From a Java background it was strange to me because I’m used to dealing with classes and there’s always a method for this or that...
- oefrha 7y ago> For CLI it’s true to the Go mantra. If the flags library is “good enough” why not just use it and avoid being fancy? Users of a command line utility ultimately don’t care what language it’s written in; having a familiar interface is a much more important goal. Since the vast majority of Linux utilities these days use GNU-style rather than Unix-style long options, it stands to reason that the latter is a better choice for users. (There are also technical advantages I won’t get into.) I know Rob Pike probably isn’t a fan given his background, but why not give developers the choice to build what they want to build, instead of shoehorning them into something they dislike? What’s the point of being opinionated here? (Edit: Sorry, didn’t mean to imply that the standard library ought to have it; however, I do find it strange that in the ten years of golang the community didn’t bother to come up with a reasonably good library for it.) Btw, flags is very primitive and can hardly handle anything complex, even if you accept the Unix-style.
- masklinn 7y agoSorry for the bother but I'm a big lost due to the lingo and the interwebs are unclear: by unix-style long options do you mean single-dash long options (e.g. `-long` being a single option), and therefore flags not allowing concatenating short options (e.g. `-r -x` can't be written `-rx`)?
- oefrha 7y ago> by unix-style long options do you mean single-dash long options (e.g. `-long` being a single option) Yes. > and therefore flags not allowing concatenating short options (e.g. `-r -x` can't be written `-rx`)? Depending on the implementation concatenation of short options may be allowed; it certainly could be ambiguous. I'm not sure about flags (the golang package) though.
- kemitche 7y agoThere are several libraries for it. urfave/cli is my go to.
- oefrha 7y agoI literally couldn't disable subcommands forced by urfave/cli. I tried very hard to disable the help subcommand.
- nickcw 7y agoFor gnu style long options you want: https://github.com/spf13/pflag https://github.com/spf13/pflag It is a drop in replacement for the built in flag library. I ususally start out with the built in flag library and migrate to pflag when things get a bit more complicated.
- oefrha 7y agoThanks, not sure why I missed this. Probably should have used this instead of settling on urfave/cli. I take back some of what I said.
- andrewksl 7y agoI've found that Cobra[0] does pretty much everything I've needed via a reasonable API, but I'll admin I'm not fussy about CLI utility behavior. [0] https://github.com/spf13/cobra https://github.com/spf13/cobra
- Thaxll 7y agoThis is what you're looking for: https://github.com/spf13/cobra https://github.com/spf13/cobra They are many libs that do that, you probably didn't search very long ...
- oefrha 7y agoThis is one of the options I evaluated but rejected.
- deleted 7y ago[deleted]