Uses of Class options-try-best-platform-binary.pwBuilder ( Apache."/>

Optionbuilder cli


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 optionbuilder cli 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 arg 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 is, like its name indicates, the description of optionbuilder cli option. It's optionbuilder cli to print the help of the program. Using Option instances you can configure all the properties of an option using the setters and the constructor.

Here optionbuilder cli 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 weird because all the methods are static and the method returns a reference to the builder. And then you make a call to the builder instance using the static method. When you have configured the option, you have to call one optionbuilder cli 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:. Optionbuilder cli navigation Blog blog "Baptiste Wicht".

There is three versions 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 optionbuilder cli a long name. Here is all optionbuilder cli 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 Optionbuilder cli 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 optionbuilder cli separator, it's used to optionbuilder cli a system like the property of Java optionbuilder cli -Dmaven. There is three implementation of this interface: A very simple parser PosixParser: A parser to parse short options GnuParser: A parser to parse both long and short options A simple optionbuilder cli using the GnuParser: For that, you can use the given methods: Return the value of the option String[] getOptionValues: Return optionbuilder cli option value parsed to the specified type boolean hasOption: Indicate if the option has been specified or not Optionbuilder cli getArgList: Return all the args that are not specified optionbuilder cli String[] getArgs: Return all the args that 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.