अपाचे कॉमन्स सीएलआई - पहला आवेदन

आइए एक नमूना कंसोल आधारित एप्लिकेशन बनाएं, जिसका उद्देश्य उपयोग किए गए विकल्पों के आधार पर या तो उत्तीर्ण संख्याओं या उत्तीर्ण संख्याओं का गुणा करना है।

CLITester नाम से एक जावा क्लास बनाएं।

उदाहरण

CLITester.java

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
public class CLITester {
   public static void main(String[] args) throws ParseException {
      //***Definition Stage***
      // create Options object
      Options options = new Options();
      
      // add option "-a"
      options.addOption("a", false, "add numbers");
      
      // add option "-m"
      options.addOption("m", false, "multiply numbers");

      //***Parsing Stage***
      //Create a parser
      CommandLineParser parser = new DefaultParser();

      //parse the options passed as command line arguments
      CommandLine cmd = parser.parse( options, args);

      //***Interrogation Stage***
      //hasOptions checks if option is present or not
      if(cmd.hasOption("a")) {
         System.out.println("Sum of the numbers: " + getSum(args));
      } else if(cmd.hasOption("m")) {
         System.out.println("Multiplication of the numbers: " + getMultiplication(args));
      }
   }
   public static int getSum(String[] args) {
      int sum = 0;
      for(int i = 1; i < args.length ; i++) {
         sum += Integer.parseInt(args[i]);
      }
      return sum;
   }
   public static int getMultiplication(String[] args) {
      int multiplication = 1;
      for(int i = 1; i < args.length ; i++) {
         multiplication *= Integer.parseInt(args[i]);
      }
      return multiplication;
   }
}

उत्पादन

परिणाम के रूप में संख्याओं का योग प्राप्त करने के लिए विकल्प और संख्याओं के रूप में पास करते समय फ़ाइल चलाएँ।

java CLITester -a 1 2 3 4 5
Sum of the numbers: 15

परिणाम के रूप में संख्याओं का गुणन प्राप्त करने के लिए विकल्प और संख्याओं के रूप में पास करते समय फ़ाइल को चलाएँ।

java CLITester -m 1 2 3 4 5
Multiplication of the numbers: 120