Menu

Example1 - basic usage

Sly Technologies

This example demonstrates the basics of how to setup some options quickly and parse the command line. The usage of the Args API is very simple and uses java annotation to save coding steps. All you have to do is mark up some class fields (static or dynamic but static for this example) and let Args do the command line parsing for you. The values of the class fields which are marked with @Property annotation will automatically reflect the command line arguments. See [Annotations explained] section for more information on annotations.



Step by step instructions

Lets break down this simple example into step by step options.

Step 1 - setup a our java class

First we setup our a normal java class and define a couple of fields we would like act as command line options. The class fields will reflect values which are provided by the user on the command line.

public static class Example1 {
  static boolean a;
  static int b;
}

Very simple class with a couple of static fields.

Step 2 - mark class fields to be used as options

Next we mark our fields with @Option annotation which tells the Args library that we want to build option objects of type OptionInfo<?> class based on these two class fields.

public static class Example1 {
  @Option
  static boolean a;

  @Option
  static int b;
}

We can further specify option name and aliases with additional attributes on the @Option annotation like so @Option("option1") static boolean a; which would change the name of the first option to option1, instead Args builds a boolean value-type option named a from the information it has.

Same goes for secod property but its value-type is int or Integer. Notice that class OptionInfo<T> takes a generic type? The type corresponds to the class field type declared in the class. So first option becomes OptionInfo<Boolean> and the second OptionInfo<Integer> allowing us to retrieve the correct value types programatically directly from option objects. However, in this example we skip option object completely and access the values directly using java class fields.

Step 3 - add entry point - main()

Next we need create a static main method which is typically the entry point of any java application and it includes command line arguments.

public static void main(String[] args) {
}

Where the parameter args is the command line arguments gathered from the shell or the command line.

Step 4 - parse the command line arguments

Now that we have an entry point, we need to parse or process the array containing the command line arguments and assign the results to our defined class fields a and b.

public static void main(String[] args) {
    Args.parseArgs(args, Example1.class);

    System.out.printf("option a = '%s'%n", Example1.a);
    System.out.printf("option b = '%d'%n", Example1.b);
}

Step 5 - execute and validate results

Now we run our little program and see if we obtain the expected results.

If we provide the command line option shell> prog -a true should change the default state of the class field a from false to true.

shell> java Example1 -a
option 'a' = true
option 'b' = 0

which is the expected result.

Now lets try without the a option on the command line.

shell> java Example1 -b 10
option 'a' = false
option 'b' = 10

This is also the expected result.
I hope you can now see how this simple concept can be applied to any number of data types, including collections.

See full description of all builtin [builtin option types].

Complete Example

public class Example1 {

    /* Mark this class field as a property/option */
    @Option
    static boolean a;

    /* Mark this class field as a property/option */
    @Option
    static int b;

    public static void main(String[] args) {
        /*
         * Create Option tree from Example1.class and parse command line
         * arguments at the same time, all in single step.
         */
        Args.parseArgs(args, Example1.class);

        /*
         * Option value are stored using our class fields
         */

        System.out.printf("option a = '%s'%n", Example1.a);
        System.out.printf("option b = '%d'%n", Example1.b);
    }
}

Related

Wiki: Annotations explained
Wiki: Args - A java configuration file and command line parser

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.