picocli: Missing default value in ArgGroup.
I understand that by default defaultValue in ArgGroup are not applied.
The behavior is explained at : https://github.com/remkop/picocli/issues/876#issuecomment-558866624 (Note: The default behavior seems surprising to me at first sight but maybe it’s justified)
I faced a situation where it seems that this behavior is not respected (but maybe I missed something) Sorry for the not so short example, I was not able to make it smaller.
The goal is to have this kind of options : X AND ((1A AND 1B) OU (2A OU 2B)) I set default value for X, 2A and 2B. I share the code :
@Command(name = "myCommand",
mixinStandardHelpOptions = true,
description = "A command with default value in section ?")
public class Example implements Runnable {
@ArgGroup(exclusive = false,
heading = "%n@|italic " //
+ "Options to be used with group 1 OR group 2 options." //
+ "|@%n")
public OptXAndGroupOneOrGroupTwo optXAndGroupOneOrGroupTwo = new OptXAndGroupOneOrGroupTwo();
public static class OptXAndGroupOneOrGroupTwo {
@Option(names = { "-x", "--option-x" }, required = true, defaultValue = "Default X", description = "option X")
private String x;
@ArgGroup(exclusive = true)
private OneOrTwo oneORtwo = new OneOrTwo();
}
public static class OneOrTwo {
@ArgGroup(exclusive = false,
heading = "%n@|bold Group 1|@ %n%n"//
+ "@|italic " //
+ "Description of the group 1 ." //
+ "|@%n")
public GroupOne one = new GroupOne();
@ArgGroup(exclusive = false,
heading = "%n@|bold Group 2|@ %n%n"//
+ "@|italic " //
+ "Description of the group 2 ." //
+ "|@%n")
public GroupTwo two = new GroupTwo();
}
public static class GroupOne {
@Option(names = { "-1a", "--option-1a" },required=true,description = "option A of group 1")
private String _1a;
@Option(names = { "-1b", "--option-1b" },required=true,description = "option B of group 1")
private String _1b;
}
public static class GroupTwo {
@Option(names = { "-2a", "--option-2a" },required=true, defaultValue = "Default 2A", description = "option A of group 2")
private String _2a;
@Option(names = { "-2b", "--option-2b" },required=true, defaultValue = "Default 2B", description = "option B of group 2")
private String _2b;
}
public void run() {
System.out.println();
System.out.println(" X = " + optXAndGroupOneOrGroupTwo.x);
System.out.println("1A = " + optXAndGroupOneOrGroupTwo.oneORtwo.one._1a);
System.out.println("1B = " + optXAndGroupOneOrGroupTwo.oneORtwo.one._1b);
System.out.println("2A = " + optXAndGroupOneOrGroupTwo.oneORtwo.two._2a);
System.out.println("2B = " + optXAndGroupOneOrGroupTwo.oneORtwo.two._2b);
}
public static void main(String... args) {
Example example = new Example();
int exitCode = new CommandLine(example).execute(args);
System.exit(exitCode);
}
}
When I run my command, it outputs value of each options. E.g if I run it without any options :
X = Default X
1A = null
1B = null
2A = Default 2A
2B = Default 2B
Now if I run it just with option -x : myCommand -x ANOTHER_VALUE, I get :
X = ANOTHER_VALUE
1A = null
1B = null
2A = null
2B = null
Default value of 2A and 2B are lost I don’t know if this is expected but this is a surprising behavior.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 18 (18 by maintainers)
Commits related to this issue
- [#1409] add failing test for investigation purposes — committed to remkop/picocli by remkop 3 years ago
- [#1409][#1463] DOC: update RELEASE-NOTES.md for new user manual section — committed to remkop/picocli by remkop 3 years ago
- [#1409][#1463] DOC: update — committed to remkop/picocli by remkop 3 years ago
- [#1409][#1463] DOC: minor tweaks — committed to remkop/picocli by remkop 3 years ago
- [#1409][#1463] DOC: fix typo — committed to remkop/picocli by remkop 3 years ago
- [#1409][#1463] DOC: more minor improvements — committed to remkop/picocli by remkop 3 years ago
- [#1409][#1463] DOC: more minor improvements — committed to remkop/picocli by remkop 3 years ago
- [#1409][#1463] DOC: more minor improvements — committed to remkop/picocli by remkop 3 years ago
- [#1409][#1463] DOC: fix typo — committed to remkop/picocli by remkop 3 years ago
- [#1409][#1463] DOC: update generated HTML — committed to remkop/picocli by remkop 3 years ago
I’ll make these changes and re-submit.
Update 1- looks like the default values do get properly stored in argSpec inside the applyGroupDefaults function correctly. There is something about the way it gets merged back into this that is causing an issue.