1. import java.util.Arrays;
  2. public class CLIArgsParser {
  3. public static void main(String[] args) {
  4. // Check if arguments are provided
  5. if (args == null || args.length == 0) {
  6. System.err.println("Error: No arguments provided.");
  7. System.exit(1); // Exit with an error code
  8. }
  9. try {
  10. // Parse arguments
  11. String filename = args[0];
  12. int count = Integer.parseInt(args[1]);
  13. // Perform operations with parsed arguments
  14. processData(filename, count);
  15. } catch (NumberFormatException e) {
  16. System.err.println("Error: Invalid argument format. Please provide an integer for the count.");
  17. System.err.println(e.getMessage()); //Print the specific exception message
  18. System.exit(1);
  19. } catch (ArrayIndexOutOfBoundsException e) {
  20. System.err.println("Error: Missing argument. Please provide both filename and count.");
  21. System.err.println(e.getMessage());
  22. System.exit(1);
  23. } catch (Exception e) {
  24. System.err.println("An unexpected error occurred: " + e.getMessage());
  25. System.exit(1);
  26. }
  27. }
  28. // Placeholder for data processing logic
  29. private static void processData(String filename, int count) {
  30. System.out.println("Processing data from file: " + filename);
  31. System.out.println("Number of records: " + count);
  32. // Add your data processing logic here
  33. }
  34. }

Add your comment