0

When I pass more than three options in cmd..the result is displaying as NULL

 package main.java;
import org.apache.commons.cli.*;

public class cli {

    public static void main(String[] args) throws Exception {
         Options options = new Options();
     Option token = new Option("t", "token", true, "token");
     token.setRequired(true);
     options.addOption(token);

     Option projectname = new Option("p", "projectname", true, "project");
     projectname.setRequired(true);
     options.addOption(projectname);
     
     Option branch = new Option("b", "branchname", true, "branch");
     branch.setRequired(true);
     options.addOption(branch);
     
     Option pullreq = new Option("PR", "pullreq", true, "pullreq");
     pullreq.setRequired(true);
     options.addOption(pullreq);
     CommandLineParser parser = new DefaultParser();
     HelpFormatter formatter = new HelpFormatter();
     CommandLine cmd = null;

     try {
         cmd = parser.parse(options, args);
     } catch (ParseException e) {
         System.out.println(e.getMessage());
         formatter.printHelp("utility-name", options);

         System.exit(1);
     }

     String token1 = cmd.getOptionValue("token");
     String projectname1 = cmd.getOptionValue("projectname");
     String branch1 = cmd.getOptionValue("branch");
     String pullreq1 = cmd.getOptionValue("pullreq");
     
   /*if(pullreq != null){
         String pullreq1 = cmd.getOptionValue("pullreq");
         System.out.println(pullreq1);
     }
     */
     System.out.println(token1);
     System.out.println(projectname1);
     System.out.println(branch1);

 }
    }

when I built it, the third and 4th option values are taken as null. java -jar testreport-1.0.2-SNAPSHOT.jar -t token -p project -b branch -PR pullreq

token project null

1 Answers1

0

The problem lies in this line:

String branch1 = cmd.getOptionValue("branch");

You simply forgot that the corresponding option, as given to Options instance, has a long option property of "branchname" instead of only "branch". So you can simply change it to:

String branch1 = cmd.getOptionValue("branchname");

and it will work. At least for me it did.

gthanop
  • 3,035
  • 2
  • 10
  • 27
  • Hi, I have tried this way..but when am giving more than 2 options..the value is getting printed as null..not sure why? I have edited my question. – Padmapriya Kp Jan 22 '21 at 06:56
  • Why did you change your question so much? The question you posted first, is totaly different from the question as posted now. Now my given answer seems irrelevant. But it was a possible answer to your question as you posted it the first time. If you have different questions you should better be posting them as different posts. For example your first question was "how to parse CLI args?", and then you changed it to "Why Apache Commons CLI parses my args wrong?" or something similar... Anyway, I will try to answer you current (second) question as is. – gthanop Jan 22 '21 at 14:40
  • Ok, I edited my answer to comply with your last question. – gthanop Jan 22 '21 at 15:12