http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/dd8ded97/log4j-core/src/test/java/org/apache/logging/log4j/core/tools/picocli/CommandLineHelpTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/tools/picocli/CommandLineHelpTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/tools/picocli/CommandLineHelpTest.java deleted file mode 100644 index 3651b22..0000000 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/tools/picocli/CommandLineHelpTest.java +++ /dev/null @@ -1,2536 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache license, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the license for the specific language governing permissions and - * limitations under the license. - */ -package org.apache.logging.log4j.core.tools.picocli; - -import org.junit.After; -import org.junit.Ignore; -import org.junit.Test; -import org.apache.logging.log4j.core.tools.picocli.CommandLine.Help; -import org.apache.logging.log4j.core.tools.picocli.CommandLine.Help.Ansi.IStyle; -import org.apache.logging.log4j.core.tools.picocli.CommandLine.Help.Ansi.Style; -import org.apache.logging.log4j.core.tools.picocli.CommandLine.Help.Ansi.Text; -import org.apache.logging.log4j.core.tools.picocli.CommandLine.Help.ColorScheme; -import org.apache.logging.log4j.core.tools.picocli.CommandLine.Help.TextTable; -import org.apache.logging.log4j.core.tools.picocli.CommandLine.Option; -import org.apache.logging.log4j.core.tools.picocli.CommandLine.Parameters; -import org.apache.logging.log4j.core.tools.picocli.CommandLine.Command; - -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.IOException; -import java.io.PrintStream; -import java.io.UnsupportedEncodingException; -import java.lang.String; -import java.lang.reflect.Field; -import java.net.InetAddress; -import java.net.URI; -import java.net.URL; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.UUID; -import java.util.concurrent.TimeUnit; - -import static java.lang.String.format; -import static org.junit.Assert.*; - -/** - * Tests for picoCLI's "Usage" help functionality. - */ -public class CommandLineHelpTest { - private static final String LINESEP = System.getProperty("line.separator"); - - @After - public void after() { - System.getProperties().remove("picocli.color.commands"); - System.getProperties().remove("picocli.color.options"); - System.getProperties().remove("picocli.color.parameters"); - System.getProperties().remove("picocli.color.optionParams"); - } - private static String usageString(Object annotatedObject, Help.Ansi ansi) throws UnsupportedEncodingException { - return usageString(new CommandLine(annotatedObject), ansi); - } - private static String usageString(CommandLine commandLine, Help.Ansi ansi) throws UnsupportedEncodingException { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - commandLine.usage(new PrintStream(baos, true, "UTF8"), ansi); - String result = baos.toString("UTF8"); - - if (ansi == Help.Ansi.AUTO) { - baos.reset(); - commandLine.usage(new PrintStream(baos, true, "UTF8")); - assertEquals(result, baos.toString("UTF8")); - } else if (ansi == Help.Ansi.ON) { - baos.reset(); - commandLine.usage(new PrintStream(baos, true, "UTF8"), Help.defaultColorScheme(Help.Ansi.ON)); - assertEquals(result, baos.toString("UTF8")); - } - return result; - } - private static Field field(Class<?> cls, String fieldName) throws NoSuchFieldException { - return cls.getDeclaredField(fieldName); - } - private static Field[] fields(Class<?> cls, String... fieldNames) throws NoSuchFieldException { - Field[] result = new Field[fieldNames.length]; - for (int i = 0; i < fieldNames.length; i++) { - result[i] = cls.getDeclaredField(fieldNames[i]); - } - return result; - } - - @Test - public void testWithoutShowDefaultValues() throws Exception { - @CommandLine.Command() - class Params { - @Option(names = {"-f", "--file"}, required = true, description = "the file to use") File file; - } - String result = usageString(new Params(), Help.Ansi.OFF); - assertEquals(format("" + - "Usage: <main class> -f=<file>%n" + - " -f, --file=<file> the file to use%n", - ""), result); - } - - @Test - public void testShowDefaultValues() throws Exception { - @CommandLine.Command(showDefaultValues = true) - class Params { - @Option(names = {"-f", "--file"}, required = true, description = "the file to use") - File file = new File("theDefault.txt"); - } - String result = usageString(new Params(), Help.Ansi.OFF); - assertEquals(format("" + - "Usage: <main class> -f=<file>%n" + - " -f, --file=<file> the file to use%n" + - " Default: theDefault.txt%n"), result); - } - - @Test - public void testShowDefaultValuesArrayField() throws Exception { - @CommandLine.Command(showDefaultValues = true) - class Params { - @Option(names = {"-x", "--array"}, required = true, description = "the array") - int[] array = {1, 5, 11, 23}; - } - String result = usageString(new Params(), Help.Ansi.OFF); - assertEquals(format("" + - "Usage: <main class> -x=<array> [-x=<array>]...%n" + - " -x, --array=<array> the array%n" + - " Default: [1, 5, 11, 23]%n"), result); - } - - @Test - public void testUsageSeparatorWithoutDefault() throws Exception { - @Command() - class Params { - @Option(names = {"-f", "--file"}, required = true, description = "the file to use") File file = new File("def.txt"); - } - String result = usageString(new Params(), Help.Ansi.OFF); - assertEquals(format("" + - "Usage: <main class> -f=<file>%n" + - " -f, --file=<file> the file to use%n", - ""), result); - } - - @Test - public void testUsageSeparator() throws Exception { - @Command(showDefaultValues = true) - class Params { - @Option(names = {"-f", "--file"}, required = true, description = "the file to use") File file = new File("def.txt"); - } - String result = usageString(new Params(), Help.Ansi.OFF); - assertEquals(format("" + - "Usage: <main class> -f=<file>%n" + - " -f, --file=<file> the file to use%n" + - " Default: def.txt%n", - ""), result); - } - - @Test - public void testUsageParamLabels() throws Exception { - @Command() - class ParamLabels { - @Option(names = "-P", paramLabel = "KEY=VALUE", type = {String.class, String.class}, - description = "Project properties (key-value pairs)") Map<String, String> props; - @Option(names = "-f", paramLabel = "FILE", description = "files") File[] f; - @Option(names = "-n", description = "a number option") int number; - @Parameters(index = "0", paramLabel = "NUM", description = "number param") int n; - @Parameters(index = "1", description = "the host parameter") InetAddress host; - } - String result = usageString(new ParamLabels(), Help.Ansi.OFF); - assertEquals(format("" + - "Usage: <main class> [-n=<number>] [-f=FILE]... [-P=KEY=VALUE]... NUM <host>%n" + - " NUM number param%n" + - " <host> the host parameter%n" + - " -f= FILE files%n" + - " -n= <number> a number option%n" + - " -P= KEY=VALUE Project properties (key-value pairs)%n", - ""), result); - } - - @Test - public void testUsageParamLabelsWithLongMapOptionName() throws Exception { - @Command() - class ParamLabels { - @Option(names = {"-P", "--properties"}, - paramLabel = "KEY=VALUE", type = {String.class, String.class}, - description = "Project properties (key-value pairs)") Map<String, String> props; - @Option(names = "-f", paramLabel = "FILE", description = "a file") File f; - @Option(names = "-n", description = "a number option") int number; - @Parameters(index = "0", paramLabel = "NUM", description = "number param") int n; - @Parameters(index = "1", description = "the host parameter") InetAddress host; - } - String result = usageString(new ParamLabels(), Help.Ansi.OFF); - assertEquals(format("" + - "Usage: <main class> [-f=FILE] [-n=<number>] [-P=KEY=VALUE]... NUM <host>%n" + - " NUM number param%n" + - " <host> the host parameter%n" + - " -f= FILE a file%n" + - " -n= <number> a number option%n" + - " -P, --properties=KEY=VALUE Project properties (key-value pairs)%n", - ""), result); - } - - // --------------- - @Test - public void testUsageVariableArityRequiredShortOptionArray() throws UnsupportedEncodingException { - // if option is required at least once and can be specified multiple times: - // -f=ARG [-f=ARG]... - class Args { - @Option(names = "-a", required = true, paramLabel = "ARG") // default - String[] a; - @Option(names = "-b", required = true, paramLabel = "ARG", arity = "0..*") - List<String> b; - @Option(names = "-c", required = true, paramLabel = "ARG", arity = "1..*") - String[] c; - @Option(names = "-d", required = true, paramLabel = "ARG", arity = "2..*") - List<String> d; - } - String expected = String.format("" + - "Usage: <main class> -a=ARG [-a=ARG]... -b=[ARG]... [-b=[ARG]...]... -c=ARG...%n" + - " [-c=ARG...]... -d=ARG ARG... [-d=ARG ARG...]...%n" + - " -a= ARG%n" + - " -b= [ARG]...%n" + - " -c= ARG...%n" + - " -d= ARG ARG...%n"); - //CommandLine.usage(new Args(), System.out); - assertEquals(expected, usageString(new Args(), Help.Ansi.OFF)); - } - - @Test - public void testUsageVariableArityShortOptionArray() throws UnsupportedEncodingException { - class Args { - @Option(names = "-a", paramLabel = "ARG") // default - List<String> a; - @Option(names = "-b", paramLabel = "ARG", arity = "0..*") - String[] b; - @Option(names = "-c", paramLabel = "ARG", arity = "1..*") - List<String> c; - @Option(names = "-d", paramLabel = "ARG", arity = "2..*") - String[] d; - } - String expected = String.format("" + - "Usage: <main class> [-a=ARG]... [-b=[ARG]...]... [-c=ARG...]... [-d=ARG%n" + - " ARG...]...%n" + - " -a= ARG%n" + - " -b= [ARG]...%n" + - " -c= ARG...%n" + - " -d= ARG ARG...%n"); - //CommandLine.usage(new Args(), System.out); - assertEquals(expected, usageString(new Args(), Help.Ansi.OFF)); - } - - @Test - public void testUsageRangeArityRequiredShortOptionArray() throws UnsupportedEncodingException { - // if option is required at least once and can be specified multiple times: - // -f=ARG [-f=ARG]... - class Args { - @Option(names = "-a", required = true, paramLabel = "ARG", arity = "0..1") - List<String> a; - @Option(names = "-b", required = true, paramLabel = "ARG", arity = "1..2") - String[] b; - @Option(names = "-c", required = true, paramLabel = "ARG", arity = "1..3") - String[] c; - @Option(names = "-d", required = true, paramLabel = "ARG", arity = "2..4") - String[] d; - } - String expected = String.format("" + - "Usage: <main class> -a[=ARG] [-a[=ARG]]... -b=ARG [ARG] [-b=ARG [ARG]]...%n" + - " -c=ARG [ARG [ARG]] [-c=ARG [ARG [ARG]]]... -d=ARG ARG [ARG%n" + - " [ARG]] [-d=ARG ARG [ARG [ARG]]]...%n" + - " -a= [ARG]%n" + - " -b= ARG [ARG]%n" + - " -c= ARG [ARG [ARG]]%n" + - " -d= ARG ARG [ARG [ARG]]%n"); - //CommandLine.usage(new Args(), System.out); - assertEquals(expected, usageString(new Args(), Help.Ansi.OFF)); - } - - @Test - public void testUsageRangeArityShortOptionArray() throws UnsupportedEncodingException { - class Args { - @Option(names = "-a", paramLabel = "ARG", arity = "0..1") - List<String> a; - @Option(names = "-b", paramLabel = "ARG", arity = "1..2") - String[] b; - @Option(names = "-c", paramLabel = "ARG", arity = "1..3") - String[] c; - @Option(names = "-d", paramLabel = "ARG", arity = "2..4") - String[] d; - } - String expected = String.format("" + - "Usage: <main class> [-a[=ARG]]... [-b=ARG [ARG]]... [-c=ARG [ARG [ARG]]]...%n" + - " [-d=ARG ARG [ARG [ARG]]]...%n" + - " -a= [ARG]%n" + - " -b= ARG [ARG]%n" + - " -c= ARG [ARG [ARG]]%n" + - " -d= ARG ARG [ARG [ARG]]%n"); - //CommandLine.usage(new Args(), System.out); - assertEquals(expected, usageString(new Args(), Help.Ansi.OFF)); - } - - @Test - public void testUsageFixedArityRequiredShortOptionArray() throws UnsupportedEncodingException { - // if option is required at least once and can be specified multiple times: - // -f=ARG [-f=ARG]... - class Args { - @Option(names = "-a", required = true, paramLabel = "ARG") // default - String[] a; - @Option(names = "-b", required = true, paramLabel = "ARG", arity = "0") - String[] b; - @Option(names = "-c", required = true, paramLabel = "ARG", arity = "1") - String[] c; - @Option(names = "-d", required = true, paramLabel = "ARG", arity = "2") - String[] d; - } - String expected = String.format("" + - "Usage: <main class> -b [-b]... -a=ARG [-a=ARG]... -c=ARG [-c=ARG]... -d=ARG ARG%n" + - " [-d=ARG ARG]...%n" + - " -a= ARG%n" + - " -b%n" + - " -c= ARG%n" + - " -d= ARG ARG%n"); - //CommandLine.usage(new Args(), System.out); - assertEquals(expected, usageString(new Args(), Help.Ansi.OFF)); - } - - @Test - public void testUsageFixedArityShortOptionArray() throws UnsupportedEncodingException { - class Args { - @Option(names = "-a", paramLabel = "ARG") // default - String[] a; - @Option(names = "-b", paramLabel = "ARG", arity = "0") - String[] b; - @Option(names = "-c", paramLabel = "ARG", arity = "1") - String[] c; - @Option(names = "-d", paramLabel = "ARG", arity = "2") - String[] d; - } - String expected = String.format("" + - "Usage: <main class> [-b]... [-a=ARG]... [-c=ARG]... [-d=ARG ARG]...%n" + - " -a= ARG%n" + - " -b%n" + - " -c= ARG%n" + - " -d= ARG ARG%n"); - //CommandLine.usage(new Args(), System.out); - assertEquals(expected, usageString(new Args(), Help.Ansi.OFF)); - } - //-------------- - @Test - public void testUsageVariableArityRequiredLongOptionArray() throws UnsupportedEncodingException { - // if option is required at least once and can be specified multiple times: - // -f=ARG [-f=ARG]... - class Args { - @Option(names = "--aa", required = true, paramLabel = "ARG") // default - String[] a; - @Option(names = "--bb", required = true, paramLabel = "ARG", arity = "0..*") - List<String> b; - @Option(names = "--cc", required = true, paramLabel = "ARG", arity = "1..*") - String[] c; - @Option(names = "--dd", required = true, paramLabel = "ARG", arity = "2..*") - List<String> d; - } - String expected = String.format("" + - "Usage: <main class> --aa=ARG [--aa=ARG]... --bb=[ARG]... [--bb=[ARG]...]...%n" + - " --cc=ARG... [--cc=ARG...]... --dd=ARG ARG... [--dd=ARG%n" + - " ARG...]...%n" + - " --aa=ARG%n" + - " --bb=[ARG]...%n" + - " --cc=ARG...%n" + - " --dd=ARG ARG...%n"); - //CommandLine.usage(new Args(), System.out); - assertEquals(expected, usageString(new Args(), Help.Ansi.OFF)); - } - - @Test - public void testUsageVariableArityLongOptionArray() throws UnsupportedEncodingException { - class Args { - @Option(names = "--aa", paramLabel = "ARG") // default - List<String> a; - @Option(names = "--bb", paramLabel = "ARG", arity = "0..*") - String[] b; - @Option(names = "--cc", paramLabel = "ARG", arity = "1..*") - List<String> c; - @Option(names = "--dd", paramLabel = "ARG", arity = "2..*") - String[] d; - } - String expected = String.format("" + - "Usage: <main class> [--aa=ARG]... [--bb=[ARG]...]... [--cc=ARG...]... [--dd=ARG%n" + - " ARG...]...%n" + - " --aa=ARG%n" + - " --bb=[ARG]...%n" + - " --cc=ARG...%n" + - " --dd=ARG ARG...%n"); - //CommandLine.usage(new Args(), System.out); - assertEquals(expected, usageString(new Args(), Help.Ansi.OFF)); - } - - @Test - public void testUsageRangeArityRequiredLongOptionArray() throws UnsupportedEncodingException { - // if option is required at least once and can be specified multiple times: - // -f=ARG [-f=ARG]... - class Args { - @Option(names = "--aa", required = true, paramLabel = "ARG", arity = "0..1") - List<String> a; - @Option(names = "--bb", required = true, paramLabel = "ARG", arity = "1..2") - String[] b; - @Option(names = "--cc", required = true, paramLabel = "ARG", arity = "1..3") - String[] c; - @Option(names = "--dd", required = true, paramLabel = "ARG", arity = "2..4", description = "foobar") - String[] d; - } - String expected = String.format("" + - "Usage: <main class> --aa[=ARG] [--aa[=ARG]]... --bb=ARG [ARG] [--bb=ARG%n" + - " [ARG]]... --cc=ARG [ARG [ARG]] [--cc=ARG [ARG [ARG]]]...%n" + - " --dd=ARG ARG [ARG [ARG]] [--dd=ARG ARG [ARG [ARG]]]...%n" + - " --aa[=ARG]%n" + - " --bb=ARG [ARG]%n" + - " --cc=ARG [ARG [ARG]]%n" + - " --dd=ARG ARG [ARG [ARG]]%n" + - " foobar%n"); - //CommandLine.usage(new Args(), System.out); - assertEquals(expected, usageString(new Args(), Help.Ansi.OFF)); - } - - @Test - public void testUsageRangeArityLongOptionArray() throws UnsupportedEncodingException { - class Args { - @Option(names = "--aa", paramLabel = "ARG", arity = "0..1") - List<String> a; - @Option(names = "--bb", paramLabel = "ARG", arity = "1..2") - String[] b; - @Option(names = "--cc", paramLabel = "ARG", arity = "1..3") - String[] c; - @Option(names = "--dd", paramLabel = "ARG", arity = "2..4", description = "foobar") - String[] d; - } - String expected = String.format("" + - "Usage: <main class> [--aa[=ARG]]... [--bb=ARG [ARG]]... [--cc=ARG [ARG%n" + - " [ARG]]]... [--dd=ARG ARG [ARG [ARG]]]...%n" + - " --aa[=ARG]%n" + - " --bb=ARG [ARG]%n" + - " --cc=ARG [ARG [ARG]]%n" + - " --dd=ARG ARG [ARG [ARG]]%n" + - " foobar%n"); - //CommandLine.usage(new Args(), System.out); - assertEquals(expected, usageString(new Args(), Help.Ansi.OFF)); - } - - @Test - public void testUsageFixedArityRequiredLongOptionArray() throws UnsupportedEncodingException { - // if option is required at least once and can be specified multiple times: - // -f=ARG [-f=ARG]... - class Args { - @Option(names = "--aa", required = true, paramLabel = "ARG") // default - String[] a; - @Option(names = "--bb", required = true, paramLabel = "ARG", arity = "0") - String[] b; - @Option(names = "--cc", required = true, paramLabel = "ARG", arity = "1") - String[] c; - @Option(names = "--dd", required = true, paramLabel = "ARG", arity = "2") - String[] d; - } - String expected = String.format("" + - "Usage: <main class> --bb [--bb]... --aa=ARG [--aa=ARG]... --cc=ARG%n" + - " [--cc=ARG]... --dd=ARG ARG [--dd=ARG ARG]...%n" + - " --aa=ARG%n" + - " --bb%n" + - " --cc=ARG%n" + - " --dd=ARG ARG%n"); - //CommandLine.usage(new Args(), System.out); - assertEquals(expected, usageString(new Args(), Help.Ansi.OFF)); - } - - @Test - public void testUsageFixedArityLongOptionArray() throws UnsupportedEncodingException { - class Args { - @Option(names = "--aa", paramLabel = "ARG") // default - String[] a; - @Option(names = "--bb", paramLabel = "ARG", arity = "0") - String[] b; - @Option(names = "--cc", paramLabel = "ARG", arity = "1") - String[] c; - @Option(names = "--dd", paramLabel = "ARG", arity = "2") - String[] d; - } - String expected = String.format("" + - "Usage: <main class> [--bb]... [--aa=ARG]... [--cc=ARG]... [--dd=ARG ARG]...%n" + - " --aa=ARG%n" + - " --bb%n" + - " --cc=ARG%n" + - " --dd=ARG ARG%n"); - //CommandLine.usage(new Args(), System.out); - assertEquals(expected, usageString(new Args(), Help.Ansi.OFF)); - } - - //------------------ - @Test - public void testUsageVariableArityRequiredShortOptionMap() throws UnsupportedEncodingException { - // if option is required at least once and can be specified multiple times: - // -f=ARG [-f=ARG]... - class Args { - @Option(names = "-a", required = true, paramLabel = "KEY=VAL") // default - Map<String, String> a; - @Option(names = "-b", required = true, arity = "0..*") - @SuppressWarnings("unchecked") - Map b; - @Option(names = "-c", required = true, arity = "1..*", type = {String.class, TimeUnit.class}) - Map<String, TimeUnit> c; - @Option(names = "-d", required = true, arity = "2..*", type = {Integer.class, URL.class}, description = "description") - Map<Integer, URL> d; - } - String expected = String.format("" + - "Usage: <main class> -a=KEY=VAL [-a=KEY=VAL]... -b=[<String=String>]... [-b=%n" + - " [<String=String>]...]... -c=<String=TimeUnit>...%n" + - " [-c=<String=TimeUnit>...]... -d=<Integer=URL>%n" + - " <Integer=URL>... [-d=<Integer=URL> <Integer=URL>...]...%n" + - " -a= KEY=VAL%n" + - " -b= [<String=String>]...%n" + - " -c= <String=TimeUnit>...%n" + - " -d= <Integer=URL> <Integer=URL>...%n" + - " description%n"); - //CommandLine.usage(new Args(), System.out); - assertEquals(expected, usageString(new Args(), Help.Ansi.OFF)); - } - - @Test - public void testUsageVariableArityOptionMap() throws UnsupportedEncodingException { - class Args { - @Option(names = "-a") // default - Map<String, String> a; - @Option(names = "-b", arity = "0..*", type = {Integer.class, Integer.class}) - Map<Integer, Integer> b; - @Option(names = "-c", paramLabel = "KEY=VALUE", arity = "1..*", type = {String.class, TimeUnit.class}) - Map<String, TimeUnit> c; - @Option(names = "-d", arity = "2..*", type = {String.class, URL.class}, description = "description") - Map<String, URL> d; - } - String expected = String.format("" + - "Usage: <main class> [-a=<String=String>]... [-b=[<Integer=Integer>]...]...%n" + - " [-c=KEY=VALUE...]... [-d=<String=URL> <String=URL>...]...%n" + - " -a= <String=String>%n" + - " -b= [<Integer=Integer>]...%n" + - " -c= KEY=VALUE...%n" + - " -d= <String=URL> <String=URL>...%n" + - " description%n"); - //CommandLine.usage(new Args(), System.out); - assertEquals(expected, usageString(new Args(), Help.Ansi.OFF)); - } - - @Test - public void testUsageRangeArityRequiredOptionMap() throws UnsupportedEncodingException { - // if option is required at least once and can be specified multiple times: - // -f=ARG [-f=ARG]... - class Args { - @Option(names = "-a", required = true, arity = "0..1", description = "a description") - Map<String, String> a; - @Option(names = "-b", required = true, arity = "1..2", type = {Integer.class, Integer.class}, description = "b description") - Map<Integer, Integer> b; - @Option(names = "-c", required = true, arity = "1..3", type = {String.class, URL.class}, description = "c description") - Map<String, URL> c; - @Option(names = "-d", required = true, paramLabel = "K=URL", arity = "2..4", description = "d description") - Map<String, URL> d; - } - String expected = String.format("" + - "Usage: <main class> -a[=<String=String>] [-a[=<String=String>]]...%n" + - " -b=<Integer=Integer> [<Integer=Integer>]%n" + - " [-b=<Integer=Integer> [<Integer=Integer>]]...%n" + - " -c=<String=URL> [<String=URL> [<String=URL>]]%n" + - " [-c=<String=URL> [<String=URL> [<String=URL>]]]... -d=K=URL%n" + - " K=URL [K=URL [K=URL]] [-d=K=URL K=URL [K=URL [K=URL]]]...%n" + - " -a= [<String=String>] a description%n" + - " -b= <Integer=Integer> [<Integer=Integer>]%n" + - " b description%n" + - " -c= <String=URL> [<String=URL> [<String=URL>]]%n" + - " c description%n" + - " -d= K=URL K=URL [K=URL [K=URL]]%n" + - " d description%n"); - //CommandLine.usage(new Args(), System.out); - assertEquals(expected, usageString(new Args(), Help.Ansi.OFF)); - } - - @Test - public void testUsageRangeArityOptionMap() throws UnsupportedEncodingException { - class Args { - @Option(names = "-a", arity = "0..1"/*, type = {UUID.class, URL.class}*/, description = "a description") - Map<UUID, URL> a; - @Option(names = "-b", arity = "1..2", type = {Long.class, UUID.class}, description = "b description") - Map<?, ?> b; - @Option(names = "-c", arity = "1..3", type = {Long.class}, description = "c description") - Map<?, ?> c; - @Option(names = "-d", paramLabel = "K=V", arity = "2..4", description = "d description") - Map<?, ?> d; - } - String expected = String.format("" + - "Usage: <main class> [-a[=<UUID=URL>]]... [-b=<Long=UUID> [<Long=UUID>]]...%n" + - " [-c=<String=String> [<String=String> [<String=String>]]]...%n" + - " [-d=K=V K=V [K=V [K=V]]]...%n" + - " -a= [<UUID=URL>] a description%n" + - " -b= <Long=UUID> [<Long=UUID>]%n" + - " b description%n" + - " -c= <String=String> [<String=String> [<String=String>]]%n" + - " c description%n" + - " -d= K=V K=V [K=V [K=V]] d description%n"); - //CommandLine.usage(new Args(), System.out); - assertEquals(expected, usageString(new Args(), Help.Ansi.OFF)); - } - - @Test - public void testUsageFixedArityRequiredOptionMap() throws UnsupportedEncodingException { - // if option is required at least once and can be specified multiple times: - // -f=ARG [-f=ARG]... - class Args { - @Option(names = "-a", required = true, description = "a description") - Map<Short, Field> a; - @Option(names = "-b", required = true, paramLabel = "KEY=VAL", arity = "0", description = "b description") - @SuppressWarnings("unchecked") - Map b; - @Option(names = "-c", required = true, arity = "1", type = {Long.class, File.class}, description = "c description") - Map<Long, File> c; - @Option(names = "-d", required = true, arity = "2", type = {URI.class, URL.class}, description = "d description") - Map<URI, URL> d; - } - String expected = String.format("" + - "Usage: <main class> -b [-b]... -a=<Short=Field> [-a=<Short=Field>]...%n" + - " -c=<Long=File> [-c=<Long=File>]... -d=<URI=URL> <URI=URL>%n" + - " [-d=<URI=URL> <URI=URL>]...%n" + - " -a= <Short=Field> a description%n" + - " -b b description%n" + - " -c= <Long=File> c description%n" + - " -d= <URI=URL> <URI=URL> d description%n"); - //CommandLine.usage(new Args(), System.out); - assertEquals(expected, usageString(new Args(), Help.Ansi.OFF)); - } - - @Test - public void testUsageFixedArityOptionMap() throws UnsupportedEncodingException { - class Args { - @Option(names = "-a", type = {Short.class, Field.class}, description = "a description") - Map<Short, Field> a; - @Option(names = "-b", arity = "0", type = {UUID.class, Long.class}, description = "b description") - @SuppressWarnings("unchecked") - Map b; - @Option(names = "-c", arity = "1", description = "c description") - Map<Long, File> c; - @Option(names = "-d", arity = "2", type = {URI.class, URL.class}, description = "d description") - Map<URI, URL> d; - } - String expected = String.format("" + - "Usage: <main class> [-b]... [-a=<Short=Field>]... [-c=<Long=File>]...%n" + - " [-d=<URI=URL> <URI=URL>]...%n" + - " -a= <Short=Field> a description%n" + - " -b b description%n" + - " -c= <Long=File> c description%n" + - " -d= <URI=URL> <URI=URL> d description%n"); - //CommandLine.usage(new Args(), System.out); - assertEquals(expected, usageString(new Args(), Help.Ansi.OFF)); - } - //-------------- - @Test - public void testUsageVariableArityParametersArray() throws UnsupportedEncodingException { - // if option is required at least once and can be specified multiple times: - // -f=ARG [-f=ARG]... - class Args { - @Parameters(paramLabel = "APARAM", description = "APARAM description") - String[] a; - @Parameters(arity = "0..*", description = "b description") - List<String> b; - @Parameters(arity = "1..*", description = "c description") - String[] c; - @Parameters(arity = "2..*", description = "d description") - List<String> d; - } - String expected = String.format("" + - "Usage: <main class> [APARAM]... [<b>]... <c>... <d> <d>...%n" + - " [APARAM]... APARAM description%n" + - " [<b>]... b description%n" + - " <c>... c description%n" + - " <d> <d>... d description%n"); - //CommandLine.usage(new Args(), System.out); - assertEquals(expected, usageString(new Args(), Help.Ansi.OFF)); - } - - @Test - public void testUsageRangeArityParameterArray() throws UnsupportedEncodingException { - class Args { - @Parameters(index = "0", paramLabel = "PARAMA", arity = "0..1", description = "PARAMA description") - List<String> a; - @Parameters(index = "0", paramLabel = "PARAMB", arity = "1..2", description = "PARAMB description") - String[] b; - @Parameters(index = "0", paramLabel = "PARAMC", arity = "1..3", description = "PARAMC description") - String[] c; - @Parameters(index = "0", paramLabel = "PARAMD", arity = "2..4", description = "PARAMD description") - String[] d; - } - String expected = String.format("" + - "Usage: <main class> [PARAMA] PARAMB [PARAMB] PARAMC [PARAMC [PARAMC]] PARAMD%n" + - " PARAMD [PARAMD [PARAMD]]%n" + - " [PARAMA] PARAMA description%n" + - " PARAMB [PARAMB] PARAMB description%n" + - " PARAMC [PARAMC [PARAMC]]%n" + - " PARAMC description%n" + - " PARAMD PARAMD [PARAMD [PARAMD]]%n" + - " PARAMD description%n"); - //CommandLine.usage(new Args(), System.out); - assertEquals(expected, usageString(new Args(), Help.Ansi.OFF)); - } - - @Test - public void testUsageFixedArityParametersArray() throws UnsupportedEncodingException { - class Args { - @Parameters(description = "a description (default arity)") - String[] a; - @Parameters(index = "0", arity = "0", description = "b description (arity=0)") - String[] b; - @Parameters(index = "1", arity = "1", description = "b description (arity=1)") - String[] c; - @Parameters(index = "2", arity = "2", description = "b description (arity=2)") - String[] d; - } - String expected = String.format("" + - "Usage: <main class> [<a>]... <c> <d> <d>%n" + - " b description (arity=0)%n" + - " [<a>]... a description (default arity)%n" + - " <c> b description (arity=1)%n" + - " <d> <d> b description (arity=2)%n"); - //CommandLine.usage(new Args(), System.out); - assertEquals(expected, usageString(new Args(), Help.Ansi.OFF)); - } - - @Test - public void testUsageVariableArityParametersMap() throws UnsupportedEncodingException { - class Args { - @Parameters() - Map<String, String> a; - @Parameters(arity = "0..*", description = "a description (arity=0..*)") - Map<Integer, Integer> b; - @Parameters(paramLabel = "KEY=VALUE", arity = "1..*", type = {String.class, TimeUnit.class}) - Map<String, TimeUnit> c; - @Parameters(arity = "2..*", type = {String.class, URL.class}, description = "description") - Map<String, URL> d; - } - String expected = String.format("" + - "Usage: <main class> [<String=String>]... [<Integer=Integer>]... KEY=VALUE...%n" + - " <String=URL> <String=URL>...%n" + - " [<String=String>]...%n" + - " [<Integer=Integer>]... a description (arity=0..*)%n" + - " KEY=VALUE...%n" + - " <String=URL> <String=URL>...%n" + - " description%n"); - //CommandLine.usage(new Args(), System.out); - assertEquals(expected, usageString(new Args(), Help.Ansi.OFF)); - } - - @Test - public void testUsageRangeArityParametersMap() throws UnsupportedEncodingException { - class Args { - @Parameters(index = "0", arity = "0..1"/*, type = {UUID.class, URL.class}*/, description = "a description") - Map<UUID, URL> a; - @Parameters(index = "1", arity = "1..2", type = {Long.class, UUID.class}, description = "b description") - Map<?, ?> b; - @Parameters(index = "2", arity = "1..3", type = {Long.class}, description = "c description") - Map<?, ?> c; - @Parameters(index = "3", paramLabel = "K=V", arity = "2..4", description = "d description") - Map<?, ?> d; - } - String expected = String.format("" + - "Usage: <main class> [<UUID=URL>] <Long=UUID> [<Long=UUID>] <String=String>%n" + - " [<String=String> [<String=String>]] K=V K=V [K=V [K=V]]%n" + - " [<UUID=URL>] a description%n" + - " <Long=UUID> [<Long=UUID>]%n" + - " b description%n" + - " <String=String> [<String=String> [<String=String>]]%n" + - " c description%n" + - " K=V K=V [K=V [K=V]] d description%n"); - //CommandLine.usage(new Args(), System.out); - assertEquals(expected, usageString(new Args(), Help.Ansi.OFF)); - } - - @Test - public void testUsageFixedArityParametersMap() throws UnsupportedEncodingException { - class Args { - @Parameters(type = {Short.class, Field.class}, description = "a description") - Map<Short, Field> a; - @Parameters(index = "0", arity = "0", type = {UUID.class, Long.class}, description = "b description (arity=0)") - @SuppressWarnings("unchecked") - Map b; - @Parameters(index = "1", arity = "1", description = "c description") - Map<Long, File> c; - @Parameters(index = "2", arity = "2", type = {URI.class, URL.class}, description = "d description") - Map<URI, URL> d; - } - String expected = String.format("" + - "Usage: <main class> [<Short=Field>]... <Long=File> <URI=URL> <URI=URL>%n" + - " b description (arity=0)%n" + - " [<Short=Field>]... a description%n" + - " <Long=File> c description%n" + - " <URI=URL> <URI=URL> d description%n"); - //CommandLine.usage(new Args(), System.out); - assertEquals(expected, usageString(new Args(), Help.Ansi.OFF)); - } - //---------- - @Test - public void testShortestFirstComparator_sortsShortestFirst() { - String[] values = {"12345", "12", "123", "123456", "1", "", "1234"}; - Arrays.sort(values, new Help.ShortestFirst()); - String[] expected = {"", "1", "12", "123", "1234", "12345", "123456"}; - assertArrayEquals(expected, values); - } - - @Test - public void testShortestFirstComparator_sortsDeclarationOrderIfEqualLength() { - String[] values = {"-d", "-", "-a", "--alpha", "--b", "--a", "--beta"}; - Arrays.sort(values, new Help.ShortestFirst()); - String[] expected = {"-", "-d", "-a", "--b", "--a", "--beta", "--alpha"}; - assertArrayEquals(expected, values); - } - - @Test - public void testSortByShortestOptionNameComparator() throws Exception { - class App { - @Option(names = {"-t", "--aaaa"}) boolean aaaa; - @Option(names = {"--bbbb", "-k"}) boolean bbbb; - @Option(names = {"-c", "--cccc"}) boolean cccc; - } - Field[] fields = fields(App.class, "aaaa", "bbbb", "cccc"); // -tkc - Arrays.sort(fields, new Help.SortByShortestOptionNameAlphabetically()); - Field[] expected = fields(App.class, "cccc", "bbbb", "aaaa"); // -ckt - assertArrayEquals(expected, fields); - } - - @Test - public void testSortByOptionArityAndNameComparator_sortsByMaxThenMinThenName() throws Exception { - class App { - @Option(names = {"-t", "--aaaa"} ) boolean tImplicitArity0; - @Option(names = {"-e", "--EEE"}, arity = "1" ) boolean explicitArity1; - @Option(names = {"--bbbb", "-k"} ) boolean kImplicitArity0; - @Option(names = {"--AAAA", "-a"} ) int aImplicitArity1; - @Option(names = {"--BBBB", "-z"} ) String[] zImplicitArity1; - @Option(names = {"--ZZZZ", "-b"}, arity = "1..3") String[] bExplicitArity1_3; - @Option(names = {"-f", "--ffff"} ) boolean fImplicitArity0; - } - Field[] fields = fields(App.class, "tImplicitArity0", "explicitArity1", "kImplicitArity0", - "aImplicitArity1", "zImplicitArity1", "bExplicitArity1_3", "fImplicitArity0"); - Arrays.sort(fields, new Help.SortByOptionArityAndNameAlphabetically()); - Field[] expected = fields(App.class, - "fImplicitArity0", - "kImplicitArity0", - "tImplicitArity0", - "aImplicitArity1", - "explicitArity1", - "zImplicitArity1", - "bExplicitArity1_3"); - assertArrayEquals(expected, fields); - } - - @Test - public void testCreateMinimalOptionRenderer_ReturnsMinimalOptionRenderer() { - assertEquals(Help.MinimalOptionRenderer.class, Help.createMinimalOptionRenderer().getClass()); - } - - @Test - public void testMinimalOptionRenderer_rendersFirstDeclaredOptionNameAndDescription() { - class Example { - @Option(names = {"---long", "-L"}, description = "long description") String longField; - @Option(names = {"-b", "-a", "--alpha"}, description = "other") String otherField; - } - Help.IOptionRenderer renderer = Help.createMinimalOptionRenderer(); - Help help = new Help(new Example(), Help.defaultColorScheme(Help.Ansi.ON)); - Help.IParamLabelRenderer parameterRenderer = help.createDefaultParamLabelRenderer(); - Field field = help.optionFields.get(0); - Text[][] row1 = renderer.render(field.getAnnotation(Option.class), field, parameterRenderer, Help.defaultColorScheme( - help.ansi())); - assertEquals(1, row1.length); - //assertArrayEquals(new String[]{"---long=<longField>", "long description"}, row1[0]); - assertArrayEquals(new Text[]{ - help.ansi().new Text(format("%s---long%s=%s<longField>%s", "@|fg(yellow) ", "|@", "@|italic ", "|@")), - help.ansi().new Text("long description")}, row1[0]); - - field = help.optionFields.get(1); - Text[][] row2 = renderer.render(field.getAnnotation(Option.class), field, parameterRenderer, Help.defaultColorScheme( - help.ansi())); - assertEquals(1, row2.length); - //assertArrayEquals(new String[]{"-b=<otherField>", "other"}, row2[0]); - assertArrayEquals(new Text[]{ - help.ansi().new Text(format("%s-b%s=%s<otherField>%s", "@|fg(yellow) ", "|@", "@|italic ", "|@")), - help.ansi().new Text("other")}, row2[0]); - } - - @Test - public void testCreateDefaultOptionRenderer_ReturnsDefaultOptionRenderer() { - assertEquals(Help.DefaultOptionRenderer.class, new Help(new UsageDemo()).createDefaultOptionRenderer().getClass()); - } - - private static Text[] textArray(Help help, String... str) { - return textArray(help.ansi(), str); - } - private static Text[] textArray(Help.Ansi ansi, String... str) { - Text[] result = new Text[str.length]; - for (int i = 0; i < str.length; i++) { - result[i] = str[i] == null ? Help.Ansi.EMPTY_TEXT : ansi.new Text(str[i]); - } - return result; - } - - @Test - public void testDefaultOptionRenderer_rendersShortestOptionNameThenOtherOptionNamesAndDescription() { - @Command(showDefaultValues = true) - class Example { - @Option(names = {"---long", "-L"}, description = "long description") String longField; - @Option(names = {"-b", "-a", "--alpha"}, description = "other") String otherField = "abc"; - } - Help help = new Help(new Example()); - Help.IOptionRenderer renderer = help.createDefaultOptionRenderer(); - Help.IParamLabelRenderer parameterRenderer = help.createDefaultParamLabelRenderer(); - Field field = help.optionFields.get(0); - Text[][] row1 = renderer.render(field.getAnnotation(Option.class), field, parameterRenderer, help.colorScheme); - assertEquals(1, row1.length); - assertArrayEquals(Arrays.toString(row1[0]), textArray(help, "", "-L", ",", "---long=<longField>", "long description"), row1[0]); - //assertArrayEquals(Arrays.toString(row1[1]), textArray(help, "", "", "", "", " Default: null"), row1[1]); // #201 don't show null defaults - - field = help.optionFields.get(1); - Text[][] row2 = renderer.render(field.getAnnotation(Option.class), field, parameterRenderer, help.colorScheme); - assertEquals(2, row2.length); - assertArrayEquals(Arrays.toString(row2[0]), textArray(help, "", "-b", ",", "-a, --alpha=<otherField>", "other"), row2[0]); - assertArrayEquals(Arrays.toString(row2[1]), textArray(help, "", "", "", "", " Default: abc"), row2[1]); - } - - @Test - public void testDefaultOptionRenderer_rendersSpecifiedMarkerForRequiredOptionsWithDefault() { - @Command(requiredOptionMarker = '*', showDefaultValues = true) - class Example { - @Option(names = {"-b", "-a", "--alpha"}, required = true, description = "other") String otherField ="abc"; - } - Help help = new Help(new Example()); - Help.IOptionRenderer renderer = help.createDefaultOptionRenderer(); - Help.IParamLabelRenderer parameterRenderer = help.createDefaultParamLabelRenderer(); - Field field = help.optionFields.get(0); - Text[][] row = renderer.render(field.getAnnotation(Option.class), field, parameterRenderer, help.colorScheme); - assertEquals(2, row.length); - assertArrayEquals(Arrays.toString(row[0]), textArray(help, "*", "-b", ",", "-a, --alpha=<otherField>", "other"), row[0]); - assertArrayEquals(Arrays.toString(row[1]), textArray(help, "", "", "", "", " Default: abc"), row[1]); - } - - @Test - public void testDefaultOptionRenderer_rendersSpecifiedMarkerForRequiredOptionsWithoutDefault() { - @Command(requiredOptionMarker = '*') - class Example { - @Option(names = {"-b", "-a", "--alpha"}, required = true, description = "other") String otherField ="abc"; - } - Help help = new Help(new Example()); - Help.IOptionRenderer renderer = help.createDefaultOptionRenderer(); - Help.IParamLabelRenderer parameterRenderer = help.createDefaultParamLabelRenderer(); - Field field = help.optionFields.get(0); - Text[][] row = renderer.render(field.getAnnotation(Option.class), field, parameterRenderer, help.colorScheme); - assertEquals(1, row.length); - assertArrayEquals(Arrays.toString(row[0]), textArray(help, "*", "-b", ",", "-a, --alpha=<otherField>", "other"), row[0]); - } - - @Test - public void testDefaultOptionRenderer_rendersSpacePrefixByDefaultForRequiredOptionsWithoutDefaultValue() { - class Example { - @Option(names = {"-b", "-a", "--alpha"}, required = true, description = "other") String otherField; - } - Help help = new Help(new Example()); - Help.IOptionRenderer renderer = help.createDefaultOptionRenderer(); - Help.IParamLabelRenderer parameterRenderer = help.createDefaultParamLabelRenderer(); - Field field = help.optionFields.get(0); - Text[][] row = renderer.render(field.getAnnotation(Option.class), field, parameterRenderer, help.colorScheme); - assertEquals(1, row.length); - assertArrayEquals(Arrays.toString(row[0]), textArray(help, " ", "-b", ",", "-a, --alpha=<otherField>", "other"), row[0]); - } - - @Test - public void testDefaultOptionRenderer_rendersSpacePrefixByDefaultForRequiredOptionsWithDefaultValue() { - //@Command(showDefaultValues = true) // set programmatically - class Example { - @Option(names = {"-b", "-a", "--alpha"}, required = true, description = "other") String otherField; - } - Help help = new Help(new Example()); - help.showDefaultValues = true; - Help.IOptionRenderer renderer = help.createDefaultOptionRenderer(); - Help.IParamLabelRenderer parameterRenderer = help.createDefaultParamLabelRenderer(); - Field field = help.optionFields.get(0); - Text[][] row = renderer.render(field.getAnnotation(Option.class), field, parameterRenderer, help.colorScheme); - assertEquals(1, row.length); - assertArrayEquals(Arrays.toString(row[0]), textArray(help, " ", "-b", ",", "-a, --alpha=<otherField>", "other"), row[0]); - // assertArrayEquals(Arrays.toString(row[1]), textArray(help, "", "", "", "", " Default: null"), row[1]); // #201 don't show null defaults - } - - @Test - public void testDefaultParameterRenderer_rendersSpacePrefixByDefaultForParametersWithPositiveArity() { - class Required { - @Parameters(description = "required") String required; - } - Help help = new Help(new Required()); - Help.IParameterRenderer renderer = help.createDefaultParameterRenderer(); - Help.IParamLabelRenderer parameterRenderer = Help.createMinimalParamLabelRenderer(); - Field field = help.positionalParametersFields.get(0); - Text[][] row1 = renderer.render(field.getAnnotation(Parameters.class), field, parameterRenderer, help.colorScheme); - assertEquals(1, row1.length); - assertArrayEquals(Arrays.toString(row1[0]), textArray(help, " ", "", "", "<required>", "required"), row1[0]); - } - - @Test - public void testDefaultParameterRenderer_rendersSpecifiedMarkerForParametersWithPositiveArity() { - @Command(requiredOptionMarker = '*') - class Required { - @Parameters(description = "required") String required; - } - Help help = new Help(new Required()); - Help.IParameterRenderer renderer = help.createDefaultParameterRenderer(); - Help.IParamLabelRenderer parameterRenderer = Help.createMinimalParamLabelRenderer(); - Field field = help.positionalParametersFields.get(0); - Text[][] row1 = renderer.render(field.getAnnotation(Parameters.class), field, parameterRenderer, help.colorScheme); - assertEquals(1, row1.length); - assertArrayEquals(Arrays.toString(row1[0]), textArray(help, "*", "", "", "<required>", "required"), row1[0]); - } - - @Test - public void testDefaultParameterRenderer_rendersSpacePrefixForParametersWithZeroArity() { - @Command(requiredOptionMarker = '*') - class Optional { - @Parameters(arity = "0..1", description = "optional") String optional; - } - Help help = new Help(new Optional()); - Help.IParameterRenderer renderer = help.createDefaultParameterRenderer(); - Help.IParamLabelRenderer parameterRenderer = Help.createMinimalParamLabelRenderer(); - Field field = help.positionalParametersFields.get(0); - Text[][] row1 = renderer.render(field.getAnnotation(Parameters.class), field, parameterRenderer, help.colorScheme); - assertEquals(1, row1.length); - assertArrayEquals(Arrays.toString(row1[0]), textArray(help, "", "", "", "<optional>", "optional"), row1[0]); - } - - @Test - public void testDefaultOptionRenderer_rendersCommaOnlyIfBothShortAndLongOptionNamesExist() { - class Example { - @Option(names = {"-v"}, description = "shortBool") boolean shortBoolean; - @Option(names = {"--verbose"}, description = "longBool") boolean longBoolean; - @Option(names = {"-x", "--xeno"}, description = "combiBool") boolean combiBoolean; - @Option(names = {"-s"}, description = "shortOnly") String shortOnlyField; - @Option(names = {"--long"}, description = "longOnly") String longOnlyField; - @Option(names = {"-b", "--beta"}, description = "combi") String combiField; - } - Help help = new Help(new Example()); - help.showDefaultValues = false; // omit default values from description column - Help.IOptionRenderer renderer = help.createDefaultOptionRenderer(); - Help.IParamLabelRenderer parameterRenderer = help.createDefaultParamLabelRenderer(); - - String[][] expected = new String[][] { - {"", "-v", "", "", "shortBool"}, - {"", "", "", "--verbose", "longBool"}, - {"", "-x", ",", "--xeno", "combiBool"}, - {"", "-s", "=", "<shortOnlyField>", "shortOnly"}, - {"", "", "", "--long=<longOnlyField>", "longOnly"}, - {"", "-b", ",", "--beta=<combiField>", "combi"}, - }; - int i = -1; - for (Field field : help.optionFields) { - Text[][] row = renderer.render(field.getAnnotation(Option.class), field, parameterRenderer, help.colorScheme); - assertEquals(1, row.length); - assertArrayEquals(Arrays.toString(row[0]), textArray(help, expected[++i]), row[0]); - } - } - - @Test - public void testDefaultOptionRenderer_omitsDefaultValuesForBooleanFields() { - @Command(showDefaultValues = true) - class Example { - @Option(names = {"-v"}, description = "shortBool") boolean shortBoolean; - @Option(names = {"--verbose"}, description = "longBool") Boolean longBoolean; - @Option(names = {"-s"}, description = "shortOnly") String shortOnlyField = "short"; - @Option(names = {"--long"}, description = "longOnly") String longOnlyField = "long"; - @Option(names = {"-b", "--beta"}, description = "combi") int combiField = 123; - } - Help help = new Help(new Example()); - Help.IOptionRenderer renderer = help.createDefaultOptionRenderer(); - Help.IParamLabelRenderer parameterRenderer = help.createDefaultParamLabelRenderer(); - - String[][] expected = new String[][] { - {"", "-v", "", "", "shortBool"}, - {"", "", "", "--verbose", "longBool"}, - {"", "-s", "=", "<shortOnlyField>", "shortOnly"}, - {"", "", "", "", "Default: short"}, - {"", "", "", "--long=<longOnlyField>", "longOnly"}, - {"", "", "", "", "Default: long"}, - {"", "-b", ",", "--beta=<combiField>", "combi"}, - {"", "", "", "", "Default: 123"}, - }; - int[] rowCount = {1, 1, 2, 2, 2}; - int i = -1; - int rowIndex = 0; - for (Field field : help.optionFields) { - Text[][] row = renderer.render(field.getAnnotation(Option.class), field, parameterRenderer, help.colorScheme); - assertEquals(rowCount[++i], row.length); - assertArrayEquals(Arrays.toString(row[0]), textArray(help, expected[rowIndex]), row[0]); - rowIndex += rowCount[i]; - } - } - - @Test - public void testCreateDefaultParameterRenderer_ReturnsDefaultParameterRenderer() { - assertEquals(Help.DefaultParamLabelRenderer.class, new Help(new UsageDemo()).createDefaultParamLabelRenderer().getClass()); - } - - @Test - public void testDefaultParameterRenderer_showsParamLabelIfPresentOrFieldNameOtherwise() { - class Example { - @Option(names = "--without" ) String longField; - @Option(names = "--with", paramLabel = "LABEL") String otherField; - } - Help help = new Help(new Example()); - Help.IParamLabelRenderer equalSeparatedParameterRenderer = help.createDefaultParamLabelRenderer(); - help.separator = " "; - Help.IParamLabelRenderer spaceSeparatedParameterRenderer = help.createDefaultParamLabelRenderer(); - - String[] expected = new String[] { - "<longField>", - "LABEL", - }; - int i = -1; - for (Field field : help.optionFields) { - i++; - Text withSpace = spaceSeparatedParameterRenderer.renderParameterLabel(field, help.ansi(), Collections.<IStyle>emptyList()); - assertEquals(withSpace.toString(), " " + expected[i], withSpace.toString()); - Text withEquals = equalSeparatedParameterRenderer.renderParameterLabel(field, help.ansi(), Collections.<IStyle>emptyList()); - assertEquals(withEquals.toString(), "=" + expected[i], withEquals.toString()); - } - } - - @Test - public void testDefaultParameterRenderer_appliesToPositionalArgumentsIgnoresSeparator() { - class WithLabel { @Parameters(paramLabel = "POSITIONAL_ARGS") String positional; } - class WithoutLabel { @Parameters() String positional; } - - Help withLabel = new Help(new WithLabel()); - Help.IParamLabelRenderer equals = withLabel.createDefaultParamLabelRenderer(); - withLabel.separator = "="; - Help.IParamLabelRenderer spaced = withLabel.createDefaultParamLabelRenderer(); - - Text withSpace = spaced.renderParameterLabel(withLabel.positionalParametersFields.get(0), withLabel.ansi(), Collections.<IStyle>emptyList()); - assertEquals(withSpace.toString(), "POSITIONAL_ARGS", withSpace.toString()); - Text withEquals = equals.renderParameterLabel(withLabel.positionalParametersFields.get(0), withLabel.ansi(), Collections.<IStyle>emptyList()); - assertEquals(withEquals.toString(), "POSITIONAL_ARGS", withEquals.toString()); - - Help withoutLabel = new Help(new WithoutLabel()); - withSpace = spaced.renderParameterLabel(withoutLabel.positionalParametersFields.get(0), withoutLabel.ansi(), Collections.<IStyle>emptyList()); - assertEquals(withSpace.toString(), "<positional>", withSpace.toString()); - withEquals = equals.renderParameterLabel(withoutLabel.positionalParametersFields.get(0), withoutLabel.ansi(), Collections.<IStyle>emptyList()); - assertEquals(withEquals.toString(), "<positional>", withEquals.toString()); - } - - @Test - public void testDefaultLayout_addsEachRowToTable() { - final Text[][] values = { - textArray(Help.Ansi.OFF, "a", "b", "c", "d"), - textArray(Help.Ansi.OFF, "1", "2", "3", "4") - }; - final int[] count = {0}; - TextTable tt = new TextTable(Help.Ansi.OFF) { - @Override public void addRowValues(Text[] columnValues) { - assertArrayEquals(values[count[0]], columnValues); - count[0]++; - } - }; - Help.Layout layout = new Help.Layout(Help.defaultColorScheme(Help.Ansi.OFF), tt); - layout.layout(null, values); - assertEquals(2, count[0]); - } - - @Test - public void testAbreviatedSynopsis_withoutParameters() { - @CommandLine.Command(abbreviateSynopsis = true) - class App { - @Option(names = {"--verbose", "-v"}) boolean verbose; - @Option(names = {"--count", "-c"}) int count; - @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; - } - Help help = new Help(new App(), Help.Ansi.OFF); - assertEquals("<main class> [OPTIONS]" + LINESEP, help.synopsis(0)); - } - - @Test - public void testAbreviatedSynopsis_withoutParameters_ANSI() { - @CommandLine.Command(abbreviateSynopsis = true) - class App { - @Option(names = {"--verbose", "-v"}) boolean verbose; - @Option(names = {"--count", "-c"}) int count; - @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; - } - Help help = new Help(new App(), Help.defaultColorScheme(Help.Ansi.ON)); - assertEquals(Help.Ansi.ON.new Text("@|bold <main class>|@ [OPTIONS]" + LINESEP).toString(), help.synopsis(0)); - } - - @Test - public void testAbreviatedSynopsis_withParameters() { - @CommandLine.Command(abbreviateSynopsis = true) - class App { - @Option(names = {"--verbose", "-v"}) boolean verbose; - @Option(names = {"--count", "-c"}) int count; - @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; - @Parameters File[] files; - } - Help help = new Help(new App(), Help.Ansi.OFF); - assertEquals("<main class> [OPTIONS] [<files>]..." + LINESEP, help.synopsis(0)); - } - - @Test - public void testAbreviatedSynopsis_withParameters_ANSI() { - @CommandLine.Command(abbreviateSynopsis = true) - class App { - @Option(names = {"--verbose", "-v"}) boolean verbose; - @Option(names = {"--count", "-c"}) int count; - @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; - @Parameters File[] files; - } - Help help = new Help(new App(), Help.defaultColorScheme(Help.Ansi.ON)); - assertEquals(Help.Ansi.ON.new Text("@|bold <main class>|@ [OPTIONS] [@|yellow <files>|@]..." + LINESEP).toString(), help.synopsis(0)); - } - - @Test - public void testAbreviatedSynopsis_commandNameCustomizableDeclaratively() throws UnsupportedEncodingException { - @CommandLine.Command(abbreviateSynopsis = true, name = "aprogram") - class App { - @Option(names = {"--verbose", "-v"}) boolean verbose; - @Option(names = {"--count", "-c"}) int count; - @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; - @Parameters File[] files; - } - String expected = "" + - "Usage: aprogram [OPTIONS] [<files>]...%n" + - " [<files>]...%n" + - " -c, --count=<count>%n" + - " -v, --verbose%n"; - String actual = usageString(new CommandLine(new App()), Help.Ansi.OFF); - assertEquals(String.format(expected), actual); - } - - @Test - public void testAbreviatedSynopsis_commandNameCustomizableProgrammatically() throws UnsupportedEncodingException { - @CommandLine.Command(abbreviateSynopsis = true) - class App { - @Option(names = {"--verbose", "-v"}) boolean verbose; - @Option(names = {"--count", "-c"}) int count; - @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; - @Parameters File[] files; - } - String expected = "" + - "Usage: anotherProgram [OPTIONS] [<files>]...%n" + - " [<files>]...%n" + - " -c, --count=<count>%n" + - " -v, --verbose%n"; - String actual = usageString(new CommandLine(new App()).setCommandName("anotherProgram"), Help.Ansi.OFF); - assertEquals(String.format(expected), actual); - } - - @Test - public void testSynopsis_commandNameCustomizableDeclaratively() throws UnsupportedEncodingException { - @CommandLine.Command(name = "aprogram") - class App { - @Option(names = {"--verbose", "-v"}) boolean verbose; - @Option(names = {"--count", "-c"}) int count; - @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; - @Parameters File[] files; - } - String expected = "" + - "Usage: aprogram [-v] [-c=<count>] [<files>]...%n" + - " [<files>]...%n" + - " -c, --count=<count>%n" + - " -v, --verbose%n"; - String actual = usageString(new CommandLine(new App()), Help.Ansi.OFF); - assertEquals(String.format(expected), actual); - } - - @Test - public void testSynopsis_commandNameCustomizableProgrammatically() throws UnsupportedEncodingException { - class App { - @Option(names = {"--verbose", "-v"}) boolean verbose; - @Option(names = {"--count", "-c"}) int count; - @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; - @Parameters File[] files; - } - String expected = "" + - "Usage: anotherProgram [-v] [-c=<count>] [<files>]...%n" + - " [<files>]...%n" + - " -c, --count=<count>%n" + - " -v, --verbose%n"; - String actual = usageString(new CommandLine(new App()).setCommandName("anotherProgram"), Help.Ansi.OFF); - assertEquals(String.format(expected), actual); - } - - @Test - public void testSynopsis_optionalOptionArity1_n_withDefaultSeparator() { - @Command() class App { - @Option(names = {"--verbose", "-v"}) boolean verbose; - @Option(names = {"--count", "-c"}, arity = "1..*") int count; - @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; - } - Help help = new Help(new App(), Help.Ansi.OFF); - assertEquals("<main class> [-v] [-c=<count>...]" + LINESEP, help.synopsis(0)); - } - - @Test - public void testSynopsis_optionalOptionArity1_n_withDefaultSeparator_ANSI() { - @Command() class App { - @Option(names = {"--verbose", "-v"}) boolean verbose; - @Option(names = {"--count", "-c"}, arity = "1..*") int count; - @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; - } - Help help = new Help(new App(), Help.defaultColorScheme(Help.Ansi.ON)); - assertEquals(Help.Ansi.ON.new Text("@|bold <main class>|@ [@|yellow -v|@] [@|yellow -c|@=@|italic <count>|@...]" + LINESEP), - help.synopsis(0)); - } - - @Test - public void testSynopsis_optionalOptionArity0_1_withSpaceSeparator() { - @CommandLine.Command(separator = " ") class App { - @Option(names = {"--verbose", "-v"}) boolean verbose; - @Option(names = {"--count", "-c"}, arity = "0..1") int count; - @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; - } - Help help = new Help(new App(), Help.Ansi.OFF); - assertEquals("<main class> [-v] [-c [<count>]]" + LINESEP, help.synopsis(0)); - } - - @Test - public void testSynopsis_optionalOptionArity0_1_withSpaceSeparator_ANSI() { - @CommandLine.Command(separator = " ") class App { - @Option(names = {"--verbose", "-v"}) boolean verbose; - @Option(names = {"--count", "-c"}, arity = "0..1") int count; - @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; - } - Help help = new Help(new App(), Help.defaultColorScheme(Help.Ansi.ON)); - assertEquals(Help.Ansi.ON.new Text("@|bold <main class>|@ [@|yellow -v|@] [@|yellow -c|@ [@|italic <count>|@]]" + LINESEP), help.synopsis(0)); - } - - @Test - public void testSynopsis_requiredOptionWithSeparator() { - @Command() class App { - @Option(names = {"--verbose", "-v"}) boolean verbose; - @Option(names = {"--count", "-c"}, required = true) int count; - @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; - } - Help help = new Help(new App(), Help.Ansi.OFF); - assertEquals("<main class> [-v] -c=<count>" + LINESEP, help.synopsis(0)); - } - - @Test - public void testSynopsis_requiredOptionWithSeparator_ANSI() { - @Command() class App { - @Option(names = {"--verbose", "-v"}) boolean verbose; - @Option(names = {"--count", "-c"}, required = true) int count; - @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; - } - Help help = new Help(new App(), Help.defaultColorScheme(Help.Ansi.ON)); - assertEquals(Help.Ansi.ON.new Text("@|bold <main class>|@ [@|yellow -v|@] @|yellow -c|@=@|italic <count>|@" + LINESEP), help.synopsis(0)); - } - - @Test - public void testSynopsis_optionalOption_withSpaceSeparator() { - @CommandLine.Command(separator = " ") class App { - @Option(names = {"--verbose", "-v"}) boolean verbose; - @Option(names = {"--count", "-c"}) int count; - @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; - } - Help help = new Help(new App(), Help.Ansi.OFF); - assertEquals("<main class> [-v] [-c <count>]" + LINESEP, help.synopsis(0)); - } - - @Test - public void testSynopsis_optionalOptionArity0_1__withSeparator() { - class App { - @Option(names = {"--verbose", "-v"}) boolean verbose; - @Option(names = {"--count", "-c"}, arity = "0..1") int count; - @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; - } - Help help = new Help(new App(), Help.Ansi.OFF); - assertEquals("<main class> [-v] [-c[=<count>]]" + LINESEP, help.synopsis(0)); - } - - @Test - public void testSynopsis_optionalOptionArity0_n__withSeparator() { - @CommandLine.Command(separator = "=") class App { - @Option(names = {"--verbose", "-v"}) boolean verbose; - @Option(names = {"--count", "-c"}, arity = "0..*") int count; - @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; - } - Help help = new Help(new App(), Help.Ansi.OFF); - // NOTE Expected :<main class> [-v] [-c[=<count>]...] but arity=0 for int field is weird anyway... - assertEquals("<main class> [-v] [-c=[<count>]...]" + LINESEP, help.synopsis(0)); - } - - @Test - public void testSynopsis_optionalOptionArity1_n__withSeparator() { - @CommandLine.Command(separator = "=") class App { - @Option(names = {"--verbose", "-v"}) boolean verbose; - @Option(names = {"--count", "-c"}, arity = "1..*") int count; - @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; - } - Help help = new Help(new App(), Help.Ansi.OFF); - assertEquals("<main class> [-v] [-c=<count>...]" + LINESEP, help.synopsis(0)); - } - - @Test - public void testSynopsis_withProgrammaticallySetSeparator_withParameters() throws UnsupportedEncodingException { - class App { - @Option(names = {"--verbose", "-v"}) boolean verbose; - @Option(names = {"--count", "-c"}) int count; - @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; - @Parameters File[] files; - } - CommandLine commandLine = new CommandLine(new App()).setSeparator(":"); - String actual = usageString(commandLine, Help.Ansi.OFF); - String expected = "" + - "Usage: <main class> [-v] [-c:<count>] [<files>]...%n" + - " [<files>]...%n" + - " -c, --count:<count>%n" + - " -v, --verbose%n"; - assertEquals(String.format(expected), actual); - } - - @Test - public void testSynopsis_withSeparator_withParameters() { - @CommandLine.Command(separator = ":") class App { - @Option(names = {"--verbose", "-v"}) boolean verbose; - @Option(names = {"--count", "-c"}) int count; - @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; - @Parameters File[] files; - } - Help help = new Help(new App(), Help.Ansi.OFF); - assertEquals("<main class> [-v] [-c:<count>] [<files>]..." + LINESEP, help.synopsis(0)); - } - - @Test - public void testSynopsis_withSeparator_withParameters_ANSI() { - @CommandLine.Command(separator = ":") class App { - @Option(names = {"--verbose", "-v"}) boolean verbose; - @Option(names = {"--count", "-c"}) int count; - @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; - @Parameters File[] files; - } - Help help = new Help(new App(), Help.defaultColorScheme(Help.Ansi.ON)); - assertEquals(Help.Ansi.ON.new Text("@|bold <main class>|@ [@|yellow -v|@] [@|yellow -c|@:@|italic <count>|@] [@|yellow <files>|@]..." + LINESEP), - help.synopsis(0)); - } - - @Test - public void testSynopsis_withSeparator_withLabeledParameters() { - @Command(separator = "=") class App { - @Option(names = {"--verbose", "-v"}) boolean verbose; - @Option(names = {"--count", "-c"}) int count; - @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; - @Parameters(paramLabel = "FILE") File[] files; - } - Help help = new Help(new App(), Help.Ansi.OFF); - assertEquals("<main class> [-v] [-c=<count>] [FILE]..." + LINESEP, help.synopsis(0)); - } - - @Test - public void testSynopsis_withSeparator_withLabeledParameters_ANSI() { - @Command(separator = "=") class App { - @Option(names = {"--verbose", "-v"}) boolean verbose; - @Option(names = {"--count", "-c"}) int count; - @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; - @Parameters(paramLabel = "FILE") File[] files; - } - Help help = new Help(new App(), Help.defaultColorScheme(Help.Ansi.ON)); - assertEquals(Help.Ansi.ON.new Text("@|bold <main class>|@ [@|yellow -v|@] [@|yellow -c|@=@|italic <count>|@] [@|yellow FILE|@]..." + LINESEP), - help.synopsis(0)); - } - - @Test - public void testSynopsis_withSeparator_withLabeledRequiredParameters() { - @CommandLine.Command(separator = "=") class App { - @Option(names = {"--verbose", "-v"}) boolean verbose; - @Option(names = {"--count", "-c"}) int count; - @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; - @Parameters(paramLabel = "FILE", arity = "1..*") File[] files; - } - Help help = new Help(new App(), Help.Ansi.OFF); - assertEquals("<main class> [-v] [-c=<count>] FILE..." + LINESEP, help.synopsis(0)); - } - - @Test - public void testSynopsis_withSeparator_withLabeledRequiredParameters_ANSI() { - @CommandLine.Command(separator = "=") class App { - @Option(names = {"--verbose", "-v"}) boolean verbose; - @Option(names = {"--count", "-c"}) int count; - @Option(names = {"--help", "-h"}, hidden = true) boolean helpRequested; - @Parameters(paramLabel = "FILE", arity = "1..*") File[] files; - } - Help help = new Help(new App(), Help.Ansi.ON); - assertEquals(Help.Ansi.ON.new Text("@|bold <main class>|@ [@|yellow -v|@] [@|yellow -c|@=@|italic <count>|@] @|yellow FILE|@..." + LINESEP), - help.synopsis(0)); - } - - @Test - public void testSynopsis_clustersBooleanOptions() { - @Command(separator = "=") class App { - @Option(names = {"--verbose", "-v"}) boolean verbose; - @Option(names = {"--aaaa", "-a"}) boolean aBoolean; - @Option(names = {"--xxxx", "-x"}) Boolean xBoolean; - @Option(names = {"--count", "-c"}, paramLabel = "COUNT") int count; - } - Help help = new Help(new App(), Help.Ansi.OFF); - assertEquals("<main class> [-avx] [-c=COUNT]" + LINESEP, help.synopsis(0)); - } - - @Test - public void testSynopsis_clustersRequiredBooleanOptions() { - @CommandLine.Command(separator = "=") class App { - @Option(names = {"--verbose", "-v"}, required = true) boolean verbose; - @Option(names = {"--aaaa", "-a"}, required = true) boolean aBoolean; - @Option(names = {"--xxxx", "-x"}, required = true) Boolean xBoolean; - @Option(names = {"--count", "-c"}, paramLabel = "COUNT") int count; - } - Help help = new Help(new App(), Help.Ansi.OFF); - assertEquals("<main class> -avx [-c=COUNT]" + LINESEP, help.synopsis(0)); - } - - @Test - public void testSynopsis_clustersRequiredBooleanOptionsSeparately() { - @CommandLine.Command(separator = "=") class App { - @Option(names = {"--verbose", "-v"}) boolean verbose; - @Option(names = {"--aaaa", "-a"}) boolean aBoolean; - @Option(names = {"--xxxx", "-x"}) Boolean xBoolean; - @Option(names = {"--Verbose", "-V"}, required = true) boolean requiredVerbose; - @Option(names = {"--Aaaa", "-A"}, required = true) boolean requiredABoolean; - @Option(names = {"--Xxxx", "-X"}, required = true) Boolean requiredXBoolean; - @Option(names = {"--count", "-c"}, paramLabel = "COUNT") int count; - } - Help help = new Help(new App(), Help.Ansi.OFF); - assertEquals("<main class> -AVX [-avx] [-c=COUNT]" + LINESEP, help.synopsis(0)); - } - - @Test - public void testSynopsis_clustersRequiredBooleanOptionsSeparately_ANSI() { - @CommandLine.Command(separator = "=") class App { - @Option(names = {"--verbose", "-v"}) boolean verbose; - @Option(names = {"--aaaa", "-a"}) boolean aBoolean; - @Option(names = {"--xxxx", "-x"}) Boolean xBoolean; - @Option(names = {"--Verbose", "-V"}, required = true) boolean requiredVerbose; - @Option(names = {"--Aaaa", "-A"}, required = true) boolean requiredABoolean; - @Option(names = {"--Xxxx", "-X"}, required = true) Boolean requiredXBoolean; - @Option(names = {"--count", "-c"}, paramLabel = "COUNT") int count; - } - Help help = new Help(new App(), Help.defaultColorScheme(Help.Ansi.ON)); - assertEquals(Help.Ansi.ON.new Text("@|bold <main class>|@ @|yellow -AVX|@ [@|yellow -avx|@] [@|yellow -c|@=@|italic COUNT|@]" + LINESEP), - help.synopsis(0)); - } - - @Test - public void testSynopsis_firstLineLengthAdjustedForSynopsisHeading() throws Exception { - //Usage: small-test-program [-acorv!?] [--version] [-h <number>] [-p <file>|<folder>] [-d -// <folder> [<folder>]] [-i <includePattern> -// [<includePattern>...]] - @CommandLine.Command(name="small-test-program", sortOptions = false, separator = " ") - class App { - @Option(names = "-a") boolean a; - @Option(names = "-c") boolean c; - @Option(names = "-o") boolean o; - @Option(names = "-r") boolean r; - @Option(names = "-v") boolean v; - @Option(names = "-!") boolean exclamation; - @Option(names = "-?") boolean question; - @Option(names = {"--version"}) boolean version; - @Option(names = {"--handle", "-h"}) int number; - @Option(names = {"--ppp", "-p"}, paramLabel = "<file>|<folder>") File f; - @Option(names = {"--ddd", "-d"}, paramLabel = "<folder>", arity="1..2") File[] d; - @Option(names = {"--include", "-i"}, paramLabel = "<includePattern>") String pattern; - } - Help help = new Help(new App(), Help.Ansi.OFF); - String expected = "" + - "Usage: small-test-program [-!?acorv] [--version] [-h <number>] [-i" + LINESEP + - " <includePattern>] [-p <file>|<folder>] [-d <folder>" + LINESEP + - " [<folder>]]..." + LINESEP; - assertEquals(expected, help.synopsisHeading() + help.synopsis(help.synopsisHeadingLength())); - - help.synopsisHeading = "Usage:%n"; - expected = "" + - "Usage:" + LINESEP + - "small-test-program [-!?acorv] [--version] [-h <number>] [-i <includePattern>]" + LINESEP + - " [-p <file>|<folder>] [-d <folder> [<folder>]]..." + LINESEP; - assertEquals(expected, help.synopsisHeading() + help.synopsis(help.synopsisHeadingLength())); - } - - @Test - public void testLongMultiLineSynopsisIndented() { - @Command(name = "<best-app-ever>") - class App { - @Option(names = "--long-option-name", paramLabel = "<long-option-value>") int a; - @Option(names = "--another-long-option-name", paramLabel = "<another-long-option-value>") int b; - @Option(names = "--third-long-option-name", paramLabel = "<third-long-option-value>") int c; - @Option(names = "--fourth-long-option-name", paramLabel = "<fourth-long-option-value>") int d; - } - Help help = new Help(new App(), Help.Ansi.OFF); - assertEquals(String.format( - "<best-app-ever> [--another-long-option-name=<another-long-option-value>]%n" + - " [--fourth-long-option-name=<fourth-long-option-value>]%n" + - " [--long-option-name=<long-option-value>]%n" + - " [--third-long-option-name=<third-long-option-value>]%n"), - help.synopsis(0)); - } - - @Test - public void testLongMultiLineSynopsisWithAtMarkIndented() { - @Command(name = "<best-app-ever>") - class App { - @Option(names = "--long-option@-name", paramLabel = "<long-option-valu@@e>") int a; - @Option(names = "--another-long-option-name", paramLabel = "^[<another-long-option-value>]") int b; - @Option(names = "--third-long-option-name", paramLabel = "<third-long-option-value>") int c; - @Option(names = "--fourth-long-option-name", paramLabel = "<fourth-long-option-value>") int d; - } - Help help = new Help(new App(), Help.Ansi.OFF); - assertEquals(String.format( - "<best-app-ever> [--another-long-option-name=^[<another-long-option-value>]]%n" + - " [--fourth-long-option-name=<fourth-long-option-value>]%n" + - " [--long-option@-name=<long-option-valu@@e>]%n" + - " [--third-long-option-name=<third-long-option-value>]%n"), - help.synopsis(0)); - } - - @Test - public void testLongMultiLineSynopsisWithAtMarkIndented_ANSI() { - @Command(name = "<best-app-ever>") - class App { - @Option(names = "--long-option@-name", paramLabel = "<long-option-valu@@e>") int a; - @Option(names = "--another-long-option-name", paramLabel = "^[<another-long-option-value>]") int b; - @Option(names = "--third-long-option-name", paramLabel = "<third-long-option-value>") int c; - @Option(names = "--fourth-long-option-name", paramLabel = "<fourth-long-option-value>") int d; - } - Help help = new Help(new App(), Help.defaultColorScheme(Help.Ansi.ON)); - assertEquals(Help.Ansi.ON.new Text(String.format( - "@|bold <best-app-ever>|@ [@|yellow --another-long-option-name|@=@|italic ^[<another-long-option-value>]|@]%n" + - " [@|yellow --fourth-long-option-name|@=@|italic <fourth-long-option-value>|@]%n" + - " [@|yellow --long-option@-name|@=@|italic <long-option-valu@@e>|@]%n" + - " [@|yellow --third-long-option-name|@=@|italic <third-long-option-value>|@]%n")), - help.synopsis(0)); - } - - @Test - public void testCustomSynopsis() { - @Command(customSynopsis = { - "<the-app> --number=NUMBER --other-option=<aargh>", - " --more=OTHER --and-other-option=<aargh>", - "<the-app> --number=NUMBER --and-other-option=<aargh>", - }) - class App {@Option(names = "--ignored") boolean ignored;} - Help help = new Help(new App(), Help.Ansi.OFF); - assertEquals(String.format( - "<the-app> --number=NUMBER --other-option=<aargh>%n" + - " --more=OTHER --and-other-option=<aargh>%n" + - "<the-app> --number=NUMBER --and-other-option=<aargh>%n"), - help.synopsis(0)); - } - @Test - public void testTextTable() { - TextTable table = new TextTable(Help.Ansi.OFF); - table.addRowValues(textArray(Help.Ansi.OFF, "", "-v", ",", "--verbose", "show what you're doing while you are doing it")); - table.addRowValues(textArray(Help.Ansi.OFF, "", "-p", null, null, "the quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog.")); - assertEquals(String.format( - " -v, --verbose show what you're doing while you are doing it%n" + - " -p the quick brown fox jumped over the lazy dog. The%n" + - " quick brown fox jumped over the lazy dog.%n" - ,""), table.toString(new StringBuilder()).toString()); - } - - @Test(expected = IllegalArgumentException.class) - public void testTextTableAddsNewRowWhenTooManyValuesSpecified() { - TextTable table = new TextTable(Help.Ansi.OFF); - table.addRowValues(textArray(Help.Ansi.OFF, "", "-c", ",", "--create", "description", "INVALID", "Row 3")); -// assertEquals(String.format("" + -// " -c, --create description %n" + -// " INVALID %n" + -// " Row 3 %n" -// ,""), table.toString(new StringBuilder()).toString()); - } - - @Test - public void testTextTableAddsNewRowWhenAnyColumnTooLong() { - TextTable table = new TextTable(Help.Ansi.OFF); - table.addRowValues("*", "-c", ",", - "--create, --create2, --create3, --create4, --create5, --create6, --create7, --create8", - "description"); - assertEquals(String.format("" +
<TRUNCATED>
