123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- /**
- * Klimbim Software collection, A bag full of things
- * Copyright (C) 2011-2023 Johannes 'Banana' Keßler
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- *
- * 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
- */
- import org.apache.commons.cli.*;
- import org.codehaus.plexus.util.cli.CommandLineUtils;
- 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");
- }
- }
|