aboutsummaryrefslogtreecommitdiffhomepage
path: root/third_party/java/jopt-simple/src/test/java/joptsimple/examples/HelpFormatterExample.java
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/java/jopt-simple/src/test/java/joptsimple/examples/HelpFormatterExample.java')
-rw-r--r--third_party/java/jopt-simple/src/test/java/joptsimple/examples/HelpFormatterExample.java69
1 files changed, 69 insertions, 0 deletions
diff --git a/third_party/java/jopt-simple/src/test/java/joptsimple/examples/HelpFormatterExample.java b/third_party/java/jopt-simple/src/test/java/joptsimple/examples/HelpFormatterExample.java
new file mode 100644
index 0000000000..0262d2a76a
--- /dev/null
+++ b/third_party/java/jopt-simple/src/test/java/joptsimple/examples/HelpFormatterExample.java
@@ -0,0 +1,69 @@
+package joptsimple.examples;
+
+import java.io.File;
+import java.util.HashSet;
+import java.util.Map;
+
+import static java.io.File.*;
+import static java.util.Arrays.*;
+
+import joptsimple.HelpFormatter;
+import joptsimple.OptionDescriptor;
+import joptsimple.OptionParser;
+
+import static joptsimple.util.DateConverter.*;
+
+public class HelpFormatterExample {
+ static class MyFormatter implements HelpFormatter {
+ public String format( Map<String, ? extends OptionDescriptor> options ) {
+ StringBuilder buffer = new StringBuilder();
+ for ( OptionDescriptor each : new HashSet<>( options.values() ) ) {
+ buffer.append( lineFor( each ) );
+ }
+ return buffer.toString();
+ }
+
+ private String lineFor( OptionDescriptor descriptor ) {
+ if ( descriptor.representsNonOptions() ) {
+ return descriptor.argumentDescription() + '(' + descriptor.argumentTypeIndicator() + "): "
+ + descriptor.description() + System.getProperty( "line.separator" );
+ }
+
+ StringBuilder line = new StringBuilder( descriptor.options().toString() );
+ line.append( ": description = " ).append( descriptor.description() );
+ line.append( ", required = " ).append( descriptor.isRequired() );
+ line.append( ", accepts arguments = " ).append( descriptor.acceptsArguments() );
+ line.append( ", requires argument = " ).append( descriptor.requiresArgument() );
+ line.append( ", argument description = " ).append( descriptor.argumentDescription() );
+ line.append( ", argument type indicator = " ).append( descriptor.argumentTypeIndicator() );
+ line.append( ", default values = " ).append( descriptor.defaultValues() );
+ line.append( System.getProperty( "line.separator" ) );
+ return line.toString();
+ }
+ }
+
+ public static void main( String[] args ) throws Exception {
+ OptionParser parser = new OptionParser() {
+ {
+ accepts( "c" ).withRequiredArg().ofType( Integer.class )
+ .describedAs( "count" ).defaultsTo( 1 );
+ accepts( "q" ).withOptionalArg().ofType( Double.class )
+ .describedAs( "quantity" );
+ accepts( "d", "some date" ).withRequiredArg().required()
+ .withValuesConvertedBy( datePattern( "MM/dd/yy" ) );
+ acceptsAll( asList( "v", "talkative", "chatty" ), "be more verbose" );
+ accepts( "output-file" ).withOptionalArg().ofType( File.class )
+ .describedAs( "file" );
+ acceptsAll( asList( "h", "?" ), "show help" ).forHelp();
+ acceptsAll( asList( "cp", "classpath" ) ).withRequiredArg()
+ .describedAs( "path1" + pathSeparatorChar + "path2:..." )
+ .ofType( File.class )
+ .withValuesSeparatedBy( pathSeparatorChar );
+ nonOptions( "files to chew on" ).ofType( File.class ).describedAs( "input files" );
+ }
+ };
+
+ parser.formatHelpWith( new MyFormatter() );
+ parser.printHelpOn( System.out );
+ }
+}