diff --git a/build.gradle b/build.gradle index 14fb9089..11953a15 100644 --- a/build.gradle +++ b/build.gradle @@ -29,6 +29,7 @@ dependencies { compile group: 'com.itextpdf', name: 'itextpdf', version: '5.5.13' compile group: 'com.jcraft', name: 'jsch', version: '0.1.54' compile group: 'org.jfree', name: 'jfreechart', version: '1.5.0' + compile group: 'info.picocli', name: 'picocli', version: '3.8.1' testCompile 'junit:junit:4.12' } diff --git a/src/main/java/net/atomique/ksar/CmdLineArgs.java b/src/main/java/net/atomique/ksar/CmdLineArgs.java new file mode 100644 index 00000000..be77f2ba --- /dev/null +++ b/src/main/java/net/atomique/ksar/CmdLineArgs.java @@ -0,0 +1,40 @@ +/* + * Copyright 2018 The kSAR Project. All rights reserved. + * See the LICENSE file in the project root for more information. + */ + +package net.atomique.ksar; + +import picocli.CommandLine.Option; + +public class CmdLineArgs { + + @Option(names = "-input", description = "sar file to be processed") + private String filename; + + @Option(names = "-help", description = "show usage", help = true) + private boolean help; + + @Option(names = "-version", description = "print version information") + private boolean version = false; + + @Option(names = "-test", description = "debug mode") + private boolean debug = false; + + public String getFilename() { + return filename; + } + + public boolean isHelp() { + return help; + } + + public boolean isVersion() { + return version; + } + + public boolean isDebug() { + return debug; + } + +} diff --git a/src/main/java/net/atomique/ksar/Main.java b/src/main/java/net/atomique/ksar/Main.java index d15bd676..34e44dd4 100644 --- a/src/main/java/net/atomique/ksar/Main.java +++ b/src/main/java/net/atomique/ksar/Main.java @@ -1,5 +1,5 @@ /* - * Copyright 2008 The kSAR Project. All rights reserved. + * Copyright 2018 The kSAR Project. All rights reserved. * See the LICENSE file in the project root for more information. */ @@ -8,6 +8,7 @@ import net.atomique.ksar.ui.Desktop; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import picocli.CommandLine; import java.util.ResourceBundle; @@ -61,8 +62,6 @@ public void run() { } public static void main(String[] args) { - int i = 0; - String arg; log.info("ksar Version : {}", VersionNumber.getVersionString()); log.info("Java runtime Version : {}", System.getProperty("java.runtime.version")); @@ -79,30 +78,30 @@ public static void main(String[] args) { config = Config.getInstance(); globaloptions = GlobalOptions.getInstance(); + CmdLineArgs cmdl_args = new CmdLineArgs(); + new CommandLine(cmdl_args).parse(args); - if (args.length > 0) { - while (i < args.length && args[i].startsWith("-")) { - arg = args[i++]; - if ("-version".equals(arg)) { - show_version(); - System.exit(0); - } - if ("-help".equals(arg)) { - usage(); - continue; - } - if ("-test".equals(arg)) { - GlobalOptions.setDodebug(true); - continue; - } - if ("-input".equals(arg)) { - if (i < args.length) { - GlobalOptions.setCLfilename(args[i++]); - } else { - exit_error(resource.getString("INPUT_REQUIRE_ARG")); - } - continue; - } + if (cmdl_args.isVersion()) { + show_version(); + System.exit(0); + } + + if (cmdl_args.isHelp()) { + usage(); + System.exit(0); + } + + if (cmdl_args.isDebug()) { + GlobalOptions.setDodebug(true); + } else { + GlobalOptions.setDodebug(false); + } + + if (cmdl_args.getFilename() != null) { + if (cmdl_args.getFilename().isEmpty()) { + exit_error(resource.getString("INPUT_REQUIRE_ARG")); + } else { + GlobalOptions.setCLfilename(cmdl_args.getFilename()); } }