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
structs should default to "embed" behaviour if "cmd" isn't specified. #168
Comments
I believe it's better to be explicit. For example while you think |
well I guess to me, you need more configuration for I guess if you think that the library should just give up on missing tags, I think panicking is still the wrong response. would you be opposed to updating some of these functions to return errors rather than just crash? it'd be a lot nicer to deal with than the current behaviour I'm seeing. |
You do not need more configuration for cmd to work - the effort required is identical. The only difference is that As a developer error (as opposed to a user error), panicking rather than erroring is deliberate. What would be the benefit to returning an error? |
well, I mean, you have to actually implement a sub-command when using as for the panic case... well, incorrectly parsed commandline args do not indicate a fatal error case in most of my apps, as there's plenty of other sources of configuration (files, env vars, sensible defaults, config stores, etc). it's certainly an error that should be handled, but the point of a panic (and the way in which it's handled differently from functions that returns errors) is centered around the idea that the error is fatal to program execution. a misplaced struct tag (especially with structs that are built through reflect from other sources) shouldn't constitute a fatal error, IMO. certainly it's a lot easier to use standard go patterns for dealing with a returned error defined by the library than it is to create a deferred recovery function and backtrack through your entire lib to see where the panic happened, and if it's a real problem that actually should hald to program or if it's just a missing tag. 🤷♀️ in any case, it sounds like you prefer the behaviour of the lib always treating parsing errors as fatal, so it sounds like it isn't a good fit for me. thanks anyway! |
If your code is incorrect, crashing the program seems appropriate. Kong doesn't crash if the user enters an invalid command, which is expected. End users should never see these panics, unless you are shipping without testing your app. |
@mickael-menu I probably wasn't totally clear from my last comment, but I'm synthesizing config structures from sources outside go, as should be indicated from the comments about other config sources and reflect. so the structs being parsed are in fact user content, not static content generated before compile time. I realize this is an unusual use case, but yes, my code gets fully tested, and no, I can't account for all config structs that may be used ahead of time. by necessity, my code needs to gracefully handle loading data into structs that are unknown, and likely not correctly tagged, at least not initially. now, you may not want to handle meta-programming use cases, and that's fine. it's just the problem space I'm working in and so kong evidently isn't a good fit. I'm still not convinced "panic is just as good as regular errors", but ultimately it's the behaviour y'all want for your lib, and you seem very firm on that stance. so I'll move on, no worries. |
Sorry, I misunderstood your use case. I don't know if it would help in your situation, but Kong now supports registering dynamic commands: Line 67 in 49417fe
|
hmmm. that actually might be helpful, yeah. that combined with using reflect to inject |
This is not accurate at all. Kong uses panic to convey programmer errors. That is normal, idiomatic Go. It sounds like your use case isn't a great fit as you're using Kong in a way it wasn't designed for. Best of luck. |
just getting going with kong, liking it a lot more than other cli parsers I've used. however, there's one minor feature request I'd love. If I've got some nested structs, and the internat struct is tagged with neither "embed" or "cmd", it'd be great if it defaults to the "embed" behaviour, rather than throwing an error:
without any tags on
Inline
:with the embed tag:
I don't see the value in having an untagged embedded struct cause a runtime panic, I think it'd be more intuitive if
embed
was the default.The text was updated successfully, but these errors were encountered: