Skip to content

Commit 27e933b

Browse files
remkopMarkoMackic
authored andcommitted
[remkop#1348] example of hiding specific positional parameters
1 parent e302194 commit 27e933b

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package picocli.examples.customhelp;
2+
3+
import picocli.CommandLine;
4+
import picocli.CommandLine.Command;
5+
import picocli.CommandLine.Model.CommandSpec;
6+
import picocli.CommandLine.Model.PositionalParamSpec;
7+
import picocli.CommandLine.Parameters;
8+
9+
import java.io.File;
10+
import java.io.PrintWriter;
11+
import java.io.StringWriter;
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
15+
/**
16+
* https://github.com/remkop/picocli/issues/1348
17+
*/
18+
@Command(name = "rename")
19+
public class HideSomePositionalParams {
20+
21+
static class MyHelpFactory implements CommandLine.IHelpFactory {
22+
@Override
23+
public CommandLine.Help create(CommandSpec commandSpec, CommandLine.Help.ColorScheme colorScheme) {
24+
return new CommandLine.Help(commandSpec, colorScheme) {
25+
@Override
26+
public String parameterList(List<PositionalParamSpec> positionalParams) {
27+
return super.parameterList(filter(positionalParams));
28+
}
29+
30+
private List<PositionalParamSpec> filter(List<PositionalParamSpec> optionList) {
31+
List<PositionalParamSpec> shown = new ArrayList<PositionalParamSpec>();
32+
for (PositionalParamSpec param : optionList) {
33+
34+
// exclude parameters whose type has the IExcluded marker interface
35+
if (!IExcluded.class.isAssignableFrom(param.auxiliaryTypes()[0])) {
36+
shown.add(param);
37+
}
38+
}
39+
return shown;
40+
}
41+
};
42+
}
43+
}
44+
interface IExcluded {}
45+
enum ToKeyword implements IExcluded {to}
46+
47+
@Parameters(index = "0", description = "File to rename.")
48+
File file;
49+
50+
@Parameters(index = "1", paramLabel = "to")
51+
ToKeyword to;
52+
53+
@Parameters(index = "2", description = "New file name.")
54+
File newFile;
55+
56+
public static void main(String[] args) {
57+
StringWriter sw = new StringWriter();
58+
59+
CommandLine cmd = new CommandLine(new HideSomePositionalParams())
60+
.setHelpFactory(new MyHelpFactory())
61+
.setErr(new PrintWriter(sw, true));
62+
63+
String expected = String.format("" +
64+
"Missing required parameters: '<file>', 'to', '<newFile>'%n" +
65+
"Usage: rename <file> to <newFile>%n" +
66+
" <file> File to rename.%n" +
67+
" <newFile> New file name.%n");
68+
69+
cmd.execute(); // no args
70+
if (!expected.equals(sw.toString())) {
71+
throw new IllegalStateException(expected + " != " + sw.toString());
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)