From: Banana Date: Thu, 31 May 2018 08:10:18 +0000 (+0200) Subject: java read command line example X-Git-Url: http://91.132.146.200/gitweb/?a=commitdiff_plain;h=81f5effad83709f8ec4b45539a97a0ae8172b0ed;p=klimbim.git java read command line example --- diff --git a/java/read-commandline-arguments-from-file.java b/java/read-commandline-arguments-from-file.java new file mode 100644 index 0000000..3a81587 --- /dev/null +++ b/java/read-commandline-arguments-from-file.java @@ -0,0 +1,92 @@ +/** + * This program is free software: you can redistribute it and/or modify + * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE + * + * You should have received a copy of the + * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0 + * along with this program. If not, see http://www.sun.com/cddl/cddl.html + * + * 2018 http://www.bananas-playground.net + * + */ + +/** + * This is an example how to simulate / read command line arguments from a file + * It will use a comandline argument to point to the file to read from + * so some code is kinda duplicate since we need to read the commandline options + * twice. + * + * more about the commandline usage can be found here + * https://www.tutorialspoint.com/commons_cli/commons_cli_usage_example.htm + */ + +public class Main { + + public static void main(String[] args) { + // command line options and parser + Options options = new Options(); + CommandLineParser parser = new DefaultParser(); + CommandLine cmdline; + + // option for the commandline file + Option commandFile = Option.builder("cf") + .argName("FILENAME") + .longOpt("command-file") + .desc("Provide command line options read from a file") + .hasArg() + .build(); + options.addOption(commandFile); + } + + // this is kinda duplicate but we need the options first + // and then decide if there are options from a file + try { + cmdline = parser.parse(options, args); + } + catch (UnrecognizedOptionException e) { + HelpFormatter formatter = new HelpFormatter(); + formatter.printHelp("example", options); + + System.exit(1); + return; + } + + // check if we use a command line file + if((cmdline != null) && cmdline.hasOption("cf") && (cmdline.getOptionValue("cf") != null && !cmdline.getOptionValue("cf").isEmpty() )) { + // now read the file and use the given input as the arguments + + String fileContent = null; + String filePath = cmdline.getOptionValue("cf"); + + // check if file exists + File f = new File(filePath); + if(f.exists() && !f.isDirectory()) { + try { + fileContent = new String ( Files.readAllBytes( Paths.get(filePath) ) ); + } + catch (IOException e) { + e.printStackTrace(); + + System.exit(1); + } + } + + args = CommandLineUtils.translateCommandline(fileContent); + + try { + cmdline = parser.parse(options, args); + } + catch (UnrecognizedOptionException e) { + HelpFormatter formatter = new HelpFormatter(); + formatter.printHelp("example", options); + + System.exit(1); + return; + } + + // do something + } + else { + System.out.println("Please provide a parameter"); + } +}