Skip to content
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

Add API to facilitate tabular custom Help sections #865

Closed
remkop opened this issue Nov 14, 2019 · 0 comments
Closed

Add API to facilitate tabular custom Help sections #865

remkop opened this issue Nov 14, 2019 · 0 comments

Comments

@remkop
Copy link
Owner

remkop commented Nov 14, 2019

Related: #856

Currently, if a user wants to show some data (for example environment variables) in tabular format, they need to implement IHelpSectionRenderer and have custom logic that builds a TextTable.

Picocli internally has similar logic to build a TextTable for the exit codes section. It makes sense to expose some of the mechanics for this as public API to reduce custom code in client applications.

Proposed new methods:

  • Help.createHeading(String, Object...)
  • Help.createTextTable(Map<?, ?>)

Example usage:

// help section keys
static final String SECTION_KEY_ENV_HEADING = "environmentVariablesHeading";
static final String SECTION_KEY_ENV_DETAILS = "environmentVariables";
// ...

// the data to display
Map<String, String> env = new LinkedHashMap<>();
env.put("FOO", "explanation of foo");
env.put("BAR", "explanation of bar");
env.put("XYZ", "xxxx yyyy zzz");

// register the custom section renderers
CommandLine cmd = new CommandLine(new MyApp());
cmd.getHelpSectionMap().put(SECTION_KEY_ENV_HEADING,
                            help -> help.createHeading("Environment Variables:%n"));
cmd.getHelpSectionMap().put(SECTION_KEY_ENV_DETAILS,
                            help -> help.createTextTable(env).toString());

// specify the location of the new sections
List<String> keys = new ArrayList<>(cmd.getHelpSectionKeys());
int index = keys.indexOf(CommandLine.Model.UsageMessageSpec.SECTION_KEY_FOOTER_HEADING);
keys.add(index, SECTION_KEY_ENV_HEADING);
keys.add(index + 1, SECTION_KEY_ENV_DETAILS);
cmd.setHelpSectionKeys(keys);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant