LOG4J2-2011 replace JCommander command line parser with picocli to let users run Log4j2 utility applications without requiring an external dependency
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/c2818bec Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/c2818bec Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/c2818bec Branch: refs/heads/master Commit: c2818beca59e948c205c359f714f24eff7340477 Parents: 57bbd88 Author: rpopma <rpo...@apache.org> Authored: Tue Aug 15 01:18:19 2017 +0900 Committer: rpopma <rpo...@apache.org> Committed: Tue Aug 15 01:18:19 2017 +0900 ---------------------------------------------------------------------- NOTICE.txt | 3 + log4j-1.2-api/pom.xml | 6 - .../config/Log4j1ConfigurationConverter.java | 37 +- .../core/util/BasicCommandLineArguments.java | 17 +- .../log4j/core/util/InetAddressConverter.java | 35 - .../log4j/core/util/picocli/CommandLine.java | 3900 ++++++++++++++++++ .../core/util/picocli/CommandLineHelpTest.java | 1634 ++++++++ .../core/util/picocli/CommandLineTest.java | 2921 +++++++++++++ .../core/util/picocli/CustomLayoutDemo.java | 265 ++ .../logging/log4j/core/util/picocli/Demo.java | 652 +++ src/changes/changes.xml | 3 + 11 files changed, 9403 insertions(+), 70 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c2818bec/NOTICE.txt ---------------------------------------------------------------------- diff --git a/NOTICE.txt b/NOTICE.txt index 6c1b3b2..bd95322 100644 --- a/NOTICE.txt +++ b/NOTICE.txt @@ -12,3 +12,6 @@ Copyright 2004 Jason Paul Kitchen TypeUtil.java Copyright 2002-2012 Ramnivas Laddad, Juergen Hoeller, Chris Beams + +picocli (http://picocli.info) +Copyright 2017 Remko Popma http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c2818bec/log4j-1.2-api/pom.xml ---------------------------------------------------------------------- diff --git a/log4j-1.2-api/pom.xml b/log4j-1.2-api/pom.xml index 30e0157..15fa86c 100644 --- a/log4j-1.2-api/pom.xml +++ b/log4j-1.2-api/pom.xml @@ -33,12 +33,6 @@ <projectDir>/log4j12-api</projectDir> </properties> <dependencies> - <!-- Command line for configuration file conversion --> - <dependency> - <groupId>com.beust</groupId> - <artifactId>jcommander</artifactId> - <optional>true</optional> - </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c2818bec/log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationConverter.java ---------------------------------------------------------------------- diff --git a/log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationConverter.java b/log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationConverter.java index aef822e..516286b 100644 --- a/log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationConverter.java +++ b/log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationConverter.java @@ -30,20 +30,21 @@ import org.apache.logging.log4j.core.config.ConfigurationException; import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder; import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration; import org.apache.logging.log4j.core.util.BasicCommandLineArguments; - -import com.beust.jcommander.Parameter; +import org.apache.logging.log4j.core.util.picocli.CommandLine; +import org.apache.logging.log4j.core.util.picocli.CommandLine.Command; +import org.apache.logging.log4j.core.util.picocli.CommandLine.Option; /** * Tool for converting a Log4j 1.x properties configuration file to Log4j 2.x XML configuration file. - * + * * <p> * Run with "--help" on the command line. * </p> - * + * * <p> * Example: * </p> - * + * * <pre> * java org.apache.log4j.config.Log4j1ConfigurationConverter --recurse * E:\vcs\git\apache\logging\logging-log4j2\log4j-1.2-api\src\test\resources\config-1.2\hadoop --in log4j.properties --verbose @@ -51,21 +52,22 @@ import com.beust.jcommander.Parameter; */ public final class Log4j1ConfigurationConverter { - public static class CommandLineArguments extends BasicCommandLineArguments { + @Command(name = "Log4j1ConfigurationConverter") + public static class CommandLineArguments extends BasicCommandLineArguments implements Runnable { - @Parameter(names = { "--failfast", "-f" }, description = "Fails on the first failure in recurse mode.") + @Option(names = { "--failfast", "-f" }, description = "Fails on the first failure in recurse mode.") private boolean failFast; - @Parameter(names = { "--in", "-i" }, description = "Specifies the input file.") + @Option(names = { "--in", "-i" }, description = "Specifies the input file.") private Path pathIn; - @Parameter(names = { "--out", "-o" }, description = "Specifies the output file.") + @Option(names = { "--out", "-o" }, description = "Specifies the output file.") private Path pathOut; - @Parameter(names = { "--recurse", "-r" }, description = "Recurses into this folder looking for the input file") + @Option(names = { "--recurse", "-r" }, description = "Recurses into this folder looking for the input file") private Path recurseIntoPath; - @Parameter(names = { "--verbose", "-v" }, description = "Be verbose.") + @Option(names = { "--verbose", "-v" }, description = "Be verbose.") private boolean verbose; public Path getPathIn() { @@ -109,18 +111,25 @@ public final class Log4j1ConfigurationConverter { } @Override + public void run() { + if (isHelp()) { + CommandLine.usage(this, System.err); + return; + } + new Log4j1ConfigurationConverter(this).run(); + } + + @Override public String toString() { return "CommandLineArguments [recurseIntoPath=" + recurseIntoPath + ", verbose=" + verbose + ", pathIn=" + pathIn + ", pathOut=" + pathOut + "]"; } - } private static final String FILE_EXT_XML = ".xml"; public static void main(final String[] args) { - new Log4j1ConfigurationConverter(BasicCommandLineArguments.parseCommandLine(args, - Log4j1ConfigurationConverter.class, new CommandLineArguments())).run(); + CommandLine.run(new CommandLineArguments(), System.err, args); } public static Log4j1ConfigurationConverter run(final CommandLineArguments cla) { http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c2818bec/log4j-core/src/main/java/org/apache/logging/log4j/core/util/BasicCommandLineArguments.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/BasicCommandLineArguments.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/BasicCommandLineArguments.java index f51d186..59b4895 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/BasicCommandLineArguments.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/BasicCommandLineArguments.java @@ -16,24 +16,11 @@ */ package org.apache.logging.log4j.core.util; -import com.beust.jcommander.JCommander; -import com.beust.jcommander.Parameter; +import org.apache.logging.log4j.core.util.picocli.CommandLine.Option; public class BasicCommandLineArguments { - public static <T extends BasicCommandLineArguments> T parseCommandLine(final String[] mainArgs, final Class<?> clazz, - final T args) { - final JCommander jCommander = new JCommander(args); - jCommander.setProgramName(clazz.getName()); - jCommander.setCaseSensitiveOptions(false); // for sanity - jCommander.parse(mainArgs); - if (args.isHelp()) { - jCommander.usage(); - } - return args; - } - - @Parameter(names = { "--help", "-?", "-h" }, help = true, description = "Prints this help.") + @Option(names = { "--help", "-?", "-h" }, usageHelp = true, description = "Prints this help and exits.") private boolean help; public boolean isHelp() { http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c2818bec/log4j-core/src/main/java/org/apache/logging/log4j/core/util/InetAddressConverter.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/InetAddressConverter.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/InetAddressConverter.java deleted file mode 100644 index 8dd02c8..0000000 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/InetAddressConverter.java +++ /dev/null @@ -1,35 +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.util; - -import java.net.InetAddress; -import java.net.UnknownHostException; - -import com.beust.jcommander.IStringConverter; - -public class InetAddressConverter implements IStringConverter<InetAddress> { - - @Override - public InetAddress convert(final String host) { - try { - return InetAddress.getByName(host); - } catch (final UnknownHostException e) { - throw new IllegalArgumentException(host, e); - } - } - -} \ No newline at end of file