-
Notifications
You must be signed in to change notification settings - Fork 422
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
No defaultValue if @ArgGroup #844
Comments
Thank you for raising this. I could reproduce your result, thanks for providing the example program. Picocli does not instantiate an The The The user manual does not mention anything about this, and you are not the first to ask about this, see this StackOverflow question. So I need to at least update the user manual to clarify the current behaviour. |
I updated the user manual to clarify when Can you take a look and see if this is sufficient? |
@AdrienDS72 I also added a new section Default Values in Argument Groups to the user manual and furthermore made the Could you take a look at the docs and see if the updated section on Argument Groups explains things better? Any feedback welcome! |
@AdrienDS72, can you give feedback on whether this meets your needs? |
Yes it's clearer to me import picocli.CommandLine;
import picocli.CommandLine.ArgGroup;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
@Command()
public class Cmd {
@ArgGroup()
Groups groups = new Groups();
static class Groups {
@Option(names = { "-nameGrp" }, defaultValue = "${env:USERNAME}")
String name;
String getName() {
if (name == null) {
name = System.getenv("USERNAME");
}
return name;
}
}
public static void main(String[] args) {
Cmd cmd = new Cmd();
new CommandLine(cmd).parseArgs("-nameGrp=foo");
System.out.println(cmd.groups.getName());
cmd = new Cmd();
new CommandLine(cmd).parseArgs();
System.out.println(cmd.groups.getName());
}
} |
Hmm, yes the duplication there does not look ideal. Thinking out loud: One idea is to make this possible programmatically, maybe by making the @Command public class Cmd {
@ArgGroup
Groups groups; // we instantiate with initUserObject (see main method below)
static class Groups {
@Option(names = { "-nameGrp" }, defaultValue = "${env:USERNAME}")
String name;
}
public static void main(String[] args) {
Cmd cmd = new Cmd();
CommandLine commandLine = new CommandLine(cmd);
// this would create a new Groups instance,
// set all its options to their default value
// and assign it to the `groups` field.
commandLine.getCommandSpec().argGroups().get(0).initUserObject(commandLine);
commandLine.parseArgs("-nameGrp=foo");
System.out.println(cmd.groups.name);
cmd = new Cmd();
commandLine = new CommandLine(cmd);
commandLine.getCommandSpec().argGroups().get(0).initUserObject(commandLine);
commandLine.parseArgs();
System.out.println(cmd.groups.name);
}
} This is verbose but that may not be a bad thing if this is a rare use case. Another idea is to do this via the annotations API. That would make it much easier to get a non- // one idea is to do something like this in the annotations
@ArgGroup(initializeBeforeMatch = true)
Groups groups; // initialize this group even if none of its options are matched CAUTION multi-value groups should not do this, or you would not be able to distinguish zero matches from one match: @ArgGroup(multiplicity = "0..*", initializeBeforeMatch = true) // this would be bad...
List<Groups> groupList; // this list would now always have a Groups object even if nothing matched Thoughts? |
I wonder if there has been any update on this in the meantime? I am facing the same issue here. I have defined a class Now, my |
@clausnagel My understanding is that there is a workaround that will get the above use case to work, but the drawback is that there is some duplication. If you can provide a pull request that addresses this I will certainly look at it. |
@remkop, could we please reopen this issue as the workaround is not working. The solution seems to be rather simple.
logs:
so when manually initializing the group field the values are set. The validation still fails, though:
In GroupMatchContainer.validate() the group is treated as an unmatched subgroup:
and in that case |
@siilike thank you for raising this! Could you open a separate ticket for this? That makes it easier for me to track which changes went into which release. |
When using
@ArgGroup
, we can't usedefaultValue
Output is :
The text was updated successfully, but these errors were encountered: