-
-
Notifications
You must be signed in to change notification settings - Fork 102
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
Args with type parameters don't work when not passed #184
Comments
Do you have a default value configured, perhaps with a struct tag like type Args struct {
// ...
Environment JSONValue[map[string]string] `arg:"--env" default:"something"`
// ....
} If so then go-arg will use that default value and pass it to your UnmarshalText. If you have a |
I do not have any default value configured. :( I'm currently on version v1.4.2 if that'd affect things. The full entry is: type Args struct {
Environment JSONValue[map[string]string] `arg:"ENVIRONMENT" help:"a JSON blob containing a dict of NAME=VALUE environment variables"`
} |
Is this perhaps related to #160? I was expecting that the arg not being passed would have just left the zero value in place. |
Very interesting, I was able to reproduce this in a unit test! I don't know exactly what's happening but a short-term fix seems to be to make your argument a pointer, like this: type Args struct {
Environment *JSONValue[map[string]string] `arg:"ENVIRONMENT" help:"a JSON blob containing a dict of NAME=VALUE environment variables"`
} Nevertheless, this is still definitely a bug and I will continue to investigate. |
OK I have a fix but it will require a bit more work to merge it. A second short-term fix for you is to implement func (v *JSONValue[T]) MarshalText() ([]byte, error) {
return json.Marshal(v.val)
} |
The basic problem here is that when a struct has a non-zero value, we take its value and turn it into a string to use as a default value in help text. Then, later, when a struct supports The fix should be to store default values both as strings and structs, and do the decoding from strings to structs in |
So I have this struct I've defined:
I am trying to use it my args like this:
(It has to be this way for Reasons™)
When I pass
--env
it works just fine, however if I omit it then I get the following error:The text was updated successfully, but these errors were encountered: