0

I wasn't able to get apache commons cli to work.

apache commons-cli

I have the most simple start: This is the only class. Resources are added with maven(commandline).

import org.apache.commons.cli.*;

public class App {

    public static void main(String[] args) {

        // create Options object
        Options options = new Options();
        CommandLineParser parser = new DefaultParser();

        // add t option
        options.addOption("t", false, "display current time");

        try{
            CommandLine cmd = parser.parse( options, args);
        }catch(ParseExeption ex){

        }

        if(cmd.hasOption("t")) {
            // print the date and time
        }else {
            // print the date
        }
    }
}

No matter what I tried. I get the "cannot find symbol". This is the last part of the error:

 [ERROR] COMPILATION ERROR : 
 [INFO] -------------------------------------------------------------
 [ERROR] /Users/peter/Code/java/using_archetypes/using_cli_1/src/main
  /java/com/mkyong/core/utils/App.java:[27,8] cannot find symbol
  symbol:   class ParseExeption
  location: class com.mkyong.core.utils.App
  [ERROR] /Users/peter/Code/java/using_archetypes/using_cli_1/src/main 
  /java/com/mkyong/core/utils/App.java:[31,4] cannot find symbol
  symbol:   variable cmd
  location: class com.mkyong.core.utils.App
  [INFO] 2 errors 
  [INFO] -------------------------------------------------------------
  [INFO]   

  -------------------------------------------------------------------
  [INFO] BUILD FAILURE
  [INFO]   
  -------------------------------------------------------------------
  [INFO] Total time: 0.842 s
  [INFO] Finished at: 2016-10-31T12:17:29+01:00
  [INFO] Final Memory: 15M/309M
  [INFO]      
   ---------------------------------------------------------------
  [ERROR] Failed to execute goal org.apache.maven.plugins:maven-  
  compiler-plugin:3.1:compile (default-compile) on project   
  dateUtils2:  Compilation failure: Compilation failure:
  [ERROR] /Users/peter/Code/java/using_archetypes/using_cli_1/src/main
  /java/com/mkyong/core/utils/App.java:[27,8] cannot find symbol
  [ERROR] symbol:   class ParseExeption
  [ERROR] location: class com.mkyong.core.utils.App
  [ERROR] /Users/peter/Code/java/using_archetypes/using_cli_1/src/main 
   /java/com/mkyong/core/utils/App.java:[31,4] cannot find symbol
  [ERROR] symbol:   variable cmd
  [ERROR] location: class com.mkyong.core.utils.App
  [ERROR] -> [Help 1]
  [ERROR] 
  [ERROR] To see the full stack trace of the errors, re-run Maven   
   with     the -e switch.
   [ERROR] Re-run Maven using the -X switch to enable full debug  
   logging.
   [ERROR] 
   [ERROR] For more information about the errors and possible   
    solutions, please read the following articles:
   [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN   
   /MojoFailureException

Please help me to start with the commons-cli. This is compiled with the maven compiler. thank you.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Peter-TI
  • 91
  • 8

1 Answers1

1

Simply replace ParseExeption with ParseExceptionand move the if/else block in the same code block as where you defined your variable cmd otherwise it won't be visible, for example as next:

public static void main(String[] args) throws ParseException{
    // create Options object
    Options options = new Options();
    CommandLineParser parser = new DefaultParser();

    // add t option
    options.addOption("t", false, "display current time");

    CommandLine cmd = parser.parse( options, args);

    if(cmd.hasOption("t")) {
        // print the date and time
    }else {
        // print the date
    }

}
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
  • Thank you. It works. I'm about to read about Java exceptions. Is there a way to get the options inside a cli-gui if no argument is passed? – Peter-TI Oct 31 '16 at 13:39
  • If I would run my application without arguments: java -jar target/dateutils.jar (without the "-t" argument). Then I would like to get a cli-gui, in which I have the option "t". Can I use apache-common-cli? – Peter-TI Oct 31 '16 at 14:43
  • what do you mean by `cli-gui`? – Nicolas Filotto Oct 31 '16 at 14:48
  • Off-course, ui not gui. Just cli-options. Example: choose option: t ->"print date and time" e ->"exit" – Peter-TI Oct 31 '16 at 14:54
  • If you use mvn archetype:generate {arg1,arg2} with arguments. You would have an archetype.. But if you use it without arguments. You would get : "Choose a number or apply filter". In which you can specify the arguments by filling in the arguments as an int. Can I achieve this with apache commons-cli? – Peter-TI Oct 31 '16 at 15:04
  • ah ok I see what you mean, you want something interactive – Nicolas Filotto Oct 31 '16 at 15:14
  • It is good question I don't know for sure, I would say no but you could do it easily with a [Scanner](https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html) – Nicolas Filotto Oct 31 '16 at 15:18
  • Maybe I use a Scanner and go completely custom. An alternative could be https://code.google.com/archive/p/cliche/ .I was actually expecting that commons-cli would do this kind of job. I'm still searching for the right tools for this job. Thanks again. Interactive would be nice using up and down arrows. Scanner would also just do the job. – Peter-TI Oct 31 '16 at 15:45
  • thx for the link, I did not know this lib – Nicolas Filotto Oct 31 '16 at 15:48
  • But I will use https://github.com/jline/jline2 and normal scanner. That's it for me and command-lines :) – Peter-TI Oct 31 '16 at 16:34
  • ok good luck then – Nicolas Filotto Oct 31 '16 at 16:35