1. import org.apache.commons.cli.*;
  2. public class CommandLineOptions {
  3. public static void main(String[] args) {
  4. // Define the options
  5. Options options = new Options();
  6. // Add options
  7. options.addOption("input", true, "Path to the input data file"); // Required input file
  8. options.addOption("output", true, "Path to the output file"); // Required output file
  9. options.addOption("learningRate", true, "Learning rate for the algorithm"); // Required learning rate
  10. options.addOption("epochs", false, "Number of training epochs"); // Optional epochs
  11. options.addOption("batchSize", false, "Batch size for mini-batch gradient descent"); // Optional batch size
  12. options.addOption("modelType", false, "Type of model to use (e.g., linear, neural)"); // Optional model type
  13. options.addOption("verbose", true, "Enable verbose output"); // Required verbose flag
  14. // Parse the command-line arguments
  15. CommandLineParser parser = new DefaultParser();
  16. CommandLine cmd = parser.parse(args);
  17. // Access the option values
  18. String inputPath = cmd.getOptionValue("input");
  19. String outputPath = cmd.getOptionValue("output");
  20. double learningRate = Double.parseDouble(cmd.getOptionValue("learningRate"));
  21. int epochs = (cmd.getOptionValue("epochs") != null) ? Integer.parseInt(cmd.getOptionValue("epochs")) : 10; //default to 10 if not specified
  22. int batchSize = (cmd.getOptionValue("batchSize") != null) ? Integer.parseInt(cmd.getOptionValue("batchSize")) : 32; //default to 32 if not specified
  23. String modelType = cmd.getOptionValue("modelType");
  24. boolean verbose = cmd.getOptionValue("verbose").equals("true");
  25. // Print the values (for demonstration)
  26. System.out.println("Input Path: " + inputPath);
  27. System.out.println("Output Path: " + outputPath);
  28. System.out.println("Learning Rate: " + learningRate);
  29. System.out.println("Epochs: " + epochs);
  30. System.out.println("Batch Size: " + batchSize);
  31. System.out.println("Model Type: " + modelType);
  32. System.out.println("Verbose: " + verbose);
  33. //Further processing based on the options goes here.
  34. }
  35. }

Add your comment