Java Command-Line Interfaces (Part 1): Apache Commons CLI

4 stars based on 78 reviews

For a project at school, I needed to refactor an old code parsing almost 30 command line arguments. I needed to add some more arguments and change some old args, but the old code was not maintainable at all.

So I decided to use a library to make the parsing. This is a really simple library to make that parsing. It's not perfect, but it makes the work I needed and is powerful to do that job. With this API, you have to declare an Options instance. This class is used to describe the command line arguments of the program. Options constructor don't take any arguments. Then you have to use the addOption methods to add some arguments to the application. There is three versions of the methods:. We need some explanations here.

The short opt is used to define an optionbuilder commons cli with a simple dash -shortopt and the long opt is defined with double dash --longopt. The hasArg indicate if the parameter accepts an argument. The description optionbuilder commons cli, like its name indicates, the description of the option.

It's used to optionbuilder commons cli the help of the program. Optionbuilder commons cli Option instances you can configure all the properties of an option using the setters and the constructor. Here is all the setters that can be used to configure the option:.

There is also an other way to create Option instance, using the OptionBuilder class. It's a little optionbuilder commons cli because all the methods are static and the method returns a reference to the builder. And then optionbuilder commons cli make a call to the builder instance using the static method. When you have configured the optionbuilder commons cli, you have to call one of the create methods to create the instance and reset the OptionBuilder.

Once you filled the Options instance with the arguments of the program, you can use it. You have to use a CommandLineParser to parse the Options.

There is three implementation of this interface:. You receive a CommandLine object representing all the arguments. The parse method can throw a ParseException if the given arguments are not valid. With that object, you can get the options that have been passed to the application. For that, you can use the given methods:. Toggle navigation Blog blog "Baptiste Wicht". There is three optionbuilder commons cli of the methods: Add an option of the class Option.

Add a new option to the arguments. This option has only a short name. The option has a short name and a long name. Here is all the setters that can be used to configure the option: The name of the args void setArgs int num: The number of args the option takes void setDescription String description: The description of the option void setLongOpt String longOpt: The long option name void setOptionalArg boolean optionalArg: Indicate if the argument of this option is optional void setRequired boolean required: Indicate if the option is required or not void setType Object type: Set the type of the Option.

Here are the supported type: Set the value separator, it's used to make a system like the property of Java like -Dmaven. There is three implementation of this interface: A very simple parser PosixParser: A optionbuilder commons cli to parse short options GnuParser: Optionbuilder commons cli parser to parse both long and short options A simple example using the GnuParser: For that, you can use the given methods: Return the value of the option String[] getOptionValues: Return the option value parsed to the specified type boolean hasOption: Indicate if the optionbuilder commons cli has been specified or not List getArgList: Return all the args that are not specified args String[] getArgs: Return all the args optionbuilder commons cli are not specified args With this API, you can also generate the help of the program: With that example, we've seen the main features of this library.

Binary option forum complaints

  • Event broker design pattern

    Fx 82ms binary trading binary trading platforms user guide

  • Mysql replication binlog_checksum

    Rb options binary trading strategy 2015

Information broker website examples

  • Stock market put options definition

    Trading strategy crude oil hebrew

  • Everything about binary options for dummies

    No deposit bonus binary options brokers 2018 oscars

  • Indian stock market options trading

    Will you bet on binary options in canada

Binary options pro signals exclusive offer 2017

41 comments T bond options fax signal

Option trading books

You should not invest money that you cannot afford to lose. Before deciding to trade, you should carefully consider your investment objectives, level of experience and risk appetite. Under no circumstances shall we have any liability to any person or entity for (a) any loss or damage in whole or part caused by, resulting from, or relating to any transactions related to Binary Options or (b) any direct, indirect, special, consequential or incidental damages whatsoever.

In this webinar, trading expert Abe Cofnas explores One Touch Binaries and shows you why they are so popular.