Apache Commons CLI লাইব্রেরি ব্যবহার করার সময় Command Line Parsing প্রক্রিয়ায় বিভিন্ন ধরনের Errors এবং Exceptions ঘটতে পারে। এগুলি সাধারণত Invalid Arguments, Missing Options, Unrecognized Options বা Incorrect Value Types হতে পারে। এই সমস্যাগুলোর সমাধান করতে Apache Commons CLI তে কিছু ইনবিল্ট ফিচার রয়েছে, যেমন ParseException এবং HelpFormatter এর মাধ্যমে ভুল ইনপুট হ্যান্ডেল করা এবং ব্যবহারকারীকে সঠিক নির্দেশনা প্রদান করা।
এখানে Command Line Parsing Errors এবং Exception Handling নিয়ে আলোচনা করা হবে।
1. Apache Commons CLI - Command Line Parsing Errors
Command Line Parsing এর সময় যে সাধারণ এররগুলো হতে পারে, সেগুলো হলো:
- Invalid Option: একটি অজানা অপশন দেওয়া হলে।
- Missing Argument: কোনো অপশনের জন্য আর্গুমেন্ট প্রয়োজন হলেও তা দেওয়া হয়নি।
- Unrecognized Option: ক্লাসে উল্লেখিত অপশনের বাইরে অন্য কোনো অপশন দেওয়া হলে।
- Invalid Argument Type: অপশনের জন্য যে আর্গুমেন্ট টাইপ প্রত্যাশিত, তা না দেওয়া হলে।
এই ধরনের ত্রুটি ParseException দ্বারা ধরা পড়ে, যেটি CommandLineParser ক্লাস থেকে আসে।
2. Exception Handling এবং Errors Handling
কমান্ড লাইন পার্সিং এরর হ্যান্ডলিং
ParseException সাধারণত কমান্ড লাইন আর্গুমেন্ট পার্স করার সময় ঘটে। যদি কোনো অপশন ভুলভাবে ইনপুট দেওয়া হয়, তাহলে এটি একটি এক্সসেপশন থ্রো করবে। এই এক্সসেপশনটি হ্যান্ডল করতে try-catch block ব্যবহার করা হয় এবং ত্রুটি মেসেজ প্রদর্শনের জন্য HelpFormatter ব্যবহার করা যেতে পারে।
এখানে একটি উদাহরণ দেওয়া হলো:
import org.apache.commons.cli.*;
public class CommandLineParserExample {
public static void main(String[] args) {
// Create options object
Options options = new Options();
// Add short and long options
options.addOption("h", "help", false, "Show help");
options.addOption("f", "file", true, "Input file");
options.addOption("v", "verbose", false, "Verbose output");
// Create parser
CommandLineParser parser = new DefaultParser();
CommandLine cmd = null;
try {
// Parse the command line arguments
cmd = parser.parse(options, args);
// If help option is provided
if (cmd.hasOption("h")) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("CommandLineParserExample", options);
return;
}
// Process the file option
if (cmd.hasOption("f")) {
System.out.println("File: " + cmd.getOptionValue("f"));
}
// Verbose mode
if (cmd.hasOption("v")) {
System.out.println("Verbose mode enabled");
}
} catch (ParseException e) {
// Handle parsing errors
System.err.println("Error parsing command line arguments: " + e.getMessage());
// Print help if the error is related to incorrect input
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("CommandLineParserExample", options);
}
}
}
ব্যাখ্যা:
- Options অবজেক্টে Short এবং Long Options যুক্ত করা হয়েছে।
- CommandLineParser ক্লাস ব্যবহার করে আর্গুমেন্ট পার্স করা হচ্ছে।
- যদি পার্সিংয়ের সময় কোনো ত্রুটি ঘটে (যেমন, অজানা অপশন অথবা অপশন মিসিং), তাহলে এটি একটি ParseException থ্রো করবে।
- ত্রুটি হ্যান্ডল করার জন্য try-catch block ব্যবহার করা হয়েছে এবং HelpFormatter দিয়ে ব্যবহারকারীকে সঠিক নির্দেশনা দেওয়া হচ্ছে।
3. Common Errors এবং Exception Handling
Error 1: Missing Argument for an Option
কিছু অপশন এমন হয়, যেগুলোর জন্য আর্গুমেন্ট প্রয়োজন। যদি আপনি আর্গুমেন্ট ছাড়া অপশনটি প্রদান করেন, তাহলে এটি ত্রুটি সৃষ্টি করবে।
উদাহরণ:
options.addOption("f", "file", true, "Input file");
যদি আপনি কমান্ড লাইনে -f প্রদান করেন কিন্তু আর্গুমেন্ট হিসেবে কোনো ফাইল নাম না দেন, তাহলে এটি একটি ত্রুটি সৃষ্টি করবে।
Error Handling:
try {
cmd = parser.parse(options, args);
} catch (MissingArgumentException e) {
System.err.println("Missing argument for option: " + e.getMessage());
// Print help message
}
Error 2: Unrecognized Option
যদি আপনি এমন একটি অপশন দেন যা Options অবজেক্টে সংজ্ঞায়িত নয়, তবে এটি ত্রুটি সৃষ্টি করবে।
উদাহরণ:
java CommandLineParserExample -z
এখানে -z একটি অজানা অপশন, যা Options এ নেই। এটি UnrecognizedOptionException ছুড়ে দিবে।
Error Handling:
try {
cmd = parser.parse(options, args);
} catch (UnrecognizedOptionException e) {
System.err.println("Unrecognized option: " + e.getMessage());
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("CommandLineParserExample", options);
}
Error 3: Invalid Argument Type
অপশনের জন্য যদি আপনি ভুল টাইপের আর্গুমেন্ট দেন (যেমন, একটি পূর্ণসংখ্যার জন্য স্ট্রিং বা একটি ফাইলের জন্য অন্য কোনো ইনপুট), এটি একটি ত্রুটি সৃষ্টি করবে।
Error Handling:
try {
cmd = parser.parse(options, args);
} catch (NumberFormatException e) {
System.err.println("Invalid argument type: " + e.getMessage());
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("CommandLineParserExample", options);
}
4. Error Handling Example:
import org.apache.commons.cli.*;
public class CommandLineParserExample {
public static void main(String[] args) {
// Create options object
Options options = new Options();
// Add short and long options
options.addOption("h", "help", false, "Show help");
options.addOption("f", "file", true, "Input file");
options.addOption("v", "verbose", false, "Verbose output");
CommandLineParser parser = new DefaultParser();
CommandLine cmd = null;
try {
// Parse the command line arguments
cmd = parser.parse(options, args);
// If help option is provided
if (cmd.hasOption("h")) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("CommandLineParserExample", options);
return;
}
// Process the file option
if (cmd.hasOption("f")) {
System.out.println("File: " + cmd.getOptionValue("f"));
}
// Verbose mode
if (cmd.hasOption("v")) {
System.out.println("Verbose mode enabled");
}
} catch (ParseException e) {
// Handle parsing errors
System.err.println("Error parsing command line arguments: " + e.getMessage());
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("CommandLineParserExample", options);
}
}
}
Output for Errors:
Missing Argument:
Error parsing command line arguments: Missing argument for option: fUnrecognized Option:
Error parsing command line arguments: Unrecognized option: -zInvalid Argument Type:
Error parsing command line arguments: Invalid argument for option: -f
সারাংশ
Apache Commons CLI লাইব্রেরি কমান্ড লাইন পার্সিংয়ের সময় Errors এবং Exception Handling করতে সহায়তা করে। আপনি ParseException ব্যবহার করে ত্রুটিগুলি হ্যান্ডেল করতে পারেন এবং HelpFormatter ব্যবহার করে ব্যবহারকারীকে সঠিক হেল্প মেসেজ প্রদর্শন করতে পারেন। সাধারণ ত্রুটিগুলোর মধ্যে Invalid Arguments, Missing Options, এবং Unrecognized Options রয়েছে, যা সহজেই try-catch ব্লক দিয়ে হ্যান্ডেল করা যায়।
Read more