1
0

read-commandline-arguments-from-file.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * Klimbim Software collection, A bag full of things
  3. * Copyright (C) 2011-2023 Johannes 'Banana' Keßler
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. *
  18. * 2018 http://www.bananas-playground.net
  19. */
  20. /**
  21. * This is an example how to simulate / read command line arguments from a file
  22. * It will use a comandline argument to point to the file to read from
  23. * so some code is kinda duplicate since we need to read the commandline options
  24. * twice.
  25. *
  26. * more about the commandline usage can be found here
  27. * https://www.tutorialspoint.com/commons_cli/commons_cli_usage_example.htm
  28. */
  29. import org.apache.commons.cli.*;
  30. import org.codehaus.plexus.util.cli.CommandLineUtils;
  31. public class Main {
  32. public static void main(String[] args) {
  33. // command line options and parser
  34. Options options = new Options();
  35. CommandLineParser parser = new DefaultParser();
  36. CommandLine cmdline;
  37. // option for the commandline file
  38. Option commandFile = Option.builder("cf")
  39. .argName("FILENAME")
  40. .longOpt("command-file")
  41. .desc("Provide command line options read from a file")
  42. .hasArg()
  43. .build();
  44. options.addOption(commandFile);
  45. }
  46. // this is kinda duplicate but we need the options first
  47. // and then decide if there are options from a file
  48. try {
  49. cmdline = parser.parse(options, args);
  50. }
  51. catch (UnrecognizedOptionException e) {
  52. HelpFormatter formatter = new HelpFormatter();
  53. formatter.printHelp("example", options);
  54. System.exit(1);
  55. return;
  56. }
  57. // check if we use a command line file
  58. if((cmdline != null) && cmdline.hasOption("cf") && (cmdline.getOptionValue("cf") != null && !cmdline.getOptionValue("cf").isEmpty() )) {
  59. // now read the file and use the given input as the arguments
  60. String fileContent = null;
  61. String filePath = cmdline.getOptionValue("cf");
  62. // check if file exists
  63. File f = new File(filePath);
  64. if(f.exists() && !f.isDirectory()) {
  65. try {
  66. fileContent = new String ( Files.readAllBytes( Paths.get(filePath) ) );
  67. }
  68. catch (IOException e) {
  69. e.printStackTrace();
  70. System.exit(1);
  71. }
  72. }
  73. args = CommandLineUtils.translateCommandline(fileContent);
  74. try {
  75. cmdline = parser.parse(options, args);
  76. }
  77. catch (UnrecognizedOptionException e) {
  78. HelpFormatter formatter = new HelpFormatter();
  79. formatter.printHelp("example", options);
  80. System.exit(1);
  81. return;
  82. }
  83. // do something
  84. }
  85. else {
  86. System.out.println("Please provide a parameter");
  87. }
  88. }