mirror of
https://github.com/jlengrand/picocli.git
synced 2026-03-10 08:41:17 +00:00
Excludes hidden options, params, and subcommands from man page generation
Fixes: #1063
This commit is contained in:
committed by
Remko Popma
parent
af0fb418bf
commit
b5e3db360b
@@ -196,7 +196,7 @@ public class ManPageGenerator {
|
||||
// recursively create man pages for subcommands
|
||||
for (CommandLine sub : spec.subcommands().values()) {
|
||||
CommandSpec subSpec = sub.getCommandSpec();
|
||||
if (done.contains(subSpec)) {continue;}
|
||||
if (done.contains(subSpec) || subSpec.usageMessage().hidden()) {continue;}
|
||||
done.add(subSpec);
|
||||
result = generateManPage(config, subSpec);
|
||||
if (result != CommandLine.ExitCode.OK) {
|
||||
@@ -384,6 +384,14 @@ public class ManPageGenerator {
|
||||
IParameterRenderer parameterRenderer = spec.commandLine().getHelp().createDefaultParameterRenderer();
|
||||
|
||||
List<OptionSpec> options = new ArrayList<OptionSpec>(spec.options()); // options are stored in order of declaration
|
||||
|
||||
// remove hidden options
|
||||
for (Iterator<OptionSpec> iter = options.iterator(); iter.hasNext();) {
|
||||
if (iter.next().hidden()) {
|
||||
iter.remove();
|
||||
}
|
||||
}
|
||||
|
||||
List<ArgGroupSpec> groups = optionListGroups(spec);
|
||||
for (ArgGroupSpec group : groups) { options.removeAll(group.options()); }
|
||||
|
||||
@@ -444,12 +452,14 @@ public class ManPageGenerator {
|
||||
}
|
||||
|
||||
private static void writePositional(PrintWriter pw, PositionalParamSpec positional, IParameterRenderer parameterRenderer, IParamLabelRenderer paramLabelRenderer) {
|
||||
pw.println();
|
||||
Text[][] rows = parameterRenderer.render(positional, paramLabelRenderer, COLOR_SCHEME);
|
||||
pw.printf("%s::%n", join(", ", rows[0][1], rows[0][3]));
|
||||
pw.printf(" %s%n", rows[0][4]);
|
||||
for (int i = 1; i < rows.length; i++) {
|
||||
pw.printf("+%n%s%n", rows[i][4]);
|
||||
if (!positional.hidden()) {
|
||||
pw.println();
|
||||
Text[][] rows = parameterRenderer.render(positional, paramLabelRenderer, COLOR_SCHEME);
|
||||
pw.printf("%s::%n", join(", ", rows[0][1], rows[0][3]));
|
||||
pw.printf(" %s%n", rows[0][4]);
|
||||
for (int i = 1; i < rows.length; i++) {
|
||||
pw.printf("+%n%s%n", rows[i][4]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -484,6 +494,15 @@ public class ManPageGenerator {
|
||||
}
|
||||
|
||||
static void genCommands(PrintWriter pw, CommandSpec spec) {
|
||||
|
||||
// remove hidden subcommands
|
||||
Map<String, CommandLine> subCommands = new LinkedHashMap<String, CommandLine>(spec.subcommands());
|
||||
for (Iterator<Map.Entry<String, CommandLine>> iter = subCommands.entrySet().iterator(); iter.hasNext();) {
|
||||
if (iter.next().getValue().getCommandSpec().usageMessage().hidden()) {
|
||||
iter.remove();
|
||||
}
|
||||
}
|
||||
|
||||
if (spec.subcommands().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -21,10 +21,6 @@ import static org.junit.Assert.*;
|
||||
|
||||
public class ManPageGeneratorTest {
|
||||
|
||||
@Test
|
||||
public void main() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void generateManPage() throws IOException {
|
||||
@Command(name = "myapp", mixinStandardHelpOptions = true,
|
||||
@@ -153,6 +149,53 @@ public class ManPageGeneratorTest {
|
||||
assertEquals(expected, sw.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHidden() throws IOException {
|
||||
|
||||
@Command(name = "a-sub", mixinStandardHelpOptions = true, description = "A sub command")
|
||||
class ASubCommand {
|
||||
@Option(names = "input-a")
|
||||
String inputA;
|
||||
}
|
||||
|
||||
@Command(name = "hidden-sub", mixinStandardHelpOptions = true, hidden = true)
|
||||
class HiddenSubCommand {
|
||||
@Option(names = "input-b")
|
||||
String inputB;
|
||||
}
|
||||
|
||||
@Command(name = "testHidden", mixinStandardHelpOptions = true,
|
||||
version = {
|
||||
"Versioned Command 1.0",
|
||||
"Picocli " + picocli.CommandLine.VERSION,
|
||||
"JVM: ${java.version} (${java.vendor} ${java.vm.name} ${java.vm.version})",
|
||||
"OS: ${os.name} ${os.version} ${os.arch}"},
|
||||
description = "This app does great things.",
|
||||
subcommands = { ASubCommand.class, HiddenSubCommand.class }
|
||||
|
||||
)
|
||||
class MyApp {
|
||||
@Option(names = {"-o", "--output"}, description = "Output location full path.")
|
||||
File outputFolder;
|
||||
|
||||
@Option(names = {"--hidden-test"}, hidden = true)
|
||||
File hidden;
|
||||
|
||||
@Parameters(hidden = true)
|
||||
List<String> values;
|
||||
}
|
||||
|
||||
StringWriter sw = new StringWriter();
|
||||
PrintWriter pw = new PrintWriter(sw); //System.out, true
|
||||
ManPageGenerator.writeSingleManPage(pw, new CommandLine(new MyApp()).getCommandSpec());
|
||||
pw.flush();
|
||||
|
||||
String expected = read("/testHidden.manpage.adoc");
|
||||
expected = expected.replace("\r\n", "\n");
|
||||
expected = expected.replace("\n", System.getProperty("line.separator"));
|
||||
assertEquals(expected, sw.toString());
|
||||
}
|
||||
|
||||
private String read(String resource) throws IOException {
|
||||
return readAndClose(getClass().getResourceAsStream(resource));
|
||||
}
|
||||
|
||||
60
picocli-codegen/src/test/resources/testHidden.manpage.adoc
Normal file
60
picocli-codegen/src/test/resources/testHidden.manpage.adoc
Normal file
@@ -0,0 +1,60 @@
|
||||
// tag::picocli-generated-full-manpage[]
|
||||
// tag::picocli-generated-man-section-header[]
|
||||
:doctype: manpage
|
||||
:revnumber: Versioned Command 1.0
|
||||
:manmanual: TestHidden Manual
|
||||
:mansource: Versioned Command 1.0
|
||||
:man-linkstyle: pass:[blue R < >]
|
||||
= testHidden(1)
|
||||
|
||||
// end::picocli-generated-man-section-header[]
|
||||
|
||||
// tag::picocli-generated-man-section-name[]
|
||||
== Name
|
||||
|
||||
testHidden - This app does great things.
|
||||
|
||||
// end::picocli-generated-man-section-name[]
|
||||
|
||||
// tag::picocli-generated-man-section-synopsis[]
|
||||
== Synopsis
|
||||
|
||||
*testHidden* [*-hV*] [*-o*=_<outputFolder>_] [COMMAND]
|
||||
|
||||
// end::picocli-generated-man-section-synopsis[]
|
||||
|
||||
// tag::picocli-generated-man-section-description[]
|
||||
== Description
|
||||
|
||||
This app does great things.
|
||||
|
||||
// end::picocli-generated-man-section-description[]
|
||||
|
||||
// tag::picocli-generated-man-section-options[]
|
||||
== Options
|
||||
|
||||
*-h*, *--help*::
|
||||
Show this help message and exit.
|
||||
|
||||
*-o*, *--output*=_<outputFolder>_::
|
||||
Output location full path.
|
||||
|
||||
*-V*, *--version*::
|
||||
Print version information and exit.
|
||||
|
||||
// end::picocli-generated-man-section-options[]
|
||||
|
||||
// tag::picocli-generated-man-section-arguments[]
|
||||
== Arguments
|
||||
|
||||
// end::picocli-generated-man-section-arguments[]
|
||||
|
||||
// tag::picocli-generated-man-section-commands[]
|
||||
== Commands
|
||||
|
||||
*a-sub*::
|
||||
A sub command
|
||||
|
||||
// end::picocli-generated-man-section-commands[]
|
||||
|
||||
// end::picocli-generated-full-manpage[]
|
||||
Reference in New Issue
Block a user