E. Example

Back to JCli index

A simple example that shows how JCli works with POJO for command line parsing step by step

Define your POJO

The POJO doesn't have to extend any class or implements any interface. Make sure the fields you want to set or get are correctly defined in POJO class.

public class APojo {
    private int level;
    ...
    public int getLevel() { return level; }
    public void setLevel( int level ) { this.level = level; }
    ...

Annotate the options

Annotation defines which command line option is mapped to where. Add annotation around the getter or setter of field, if there are any options.

...
import org.cyclopsgroup.jcli.annotation.*;
...
@Cli(name="test")
public class APojo {
...
@Option(name = "l", longName = "level", description = "A integer value")
public int getLevel() { return level; }
...

Annotate the argument

Argument is what's left after options are parsed from original command line input. It's often a multi-value list.

...
private List<String> arguments = new ArrayList<String>();
 
@MultiValue
@Argument( description = "Left over arguments" )
public List<String> getArguments() { return arguments; }
...

Parse arguments with ArgumentProcessor

Now the POJO is ready, rules are defined. At runtime, use ArgumentProcessor class to parse string array and pass result to POJO.

import org.cyclopsgroup.jcli.ArgumentProcessor;
...
APojo pojo = new APojo();
ArgumentProcessor.newInstance( APojo.class ).process( args, pojo );
doSomethingWith( pojo.getLevel() );
...

More

At this point I'm sure you are wondering how complicated cases are handled. What happens to customized conversion rule for a field, how to define option that can take more than one values, or doesn't take a value, what if argument type is not String.

Please keep your curiosity and continue to FAQ page.

Back to JCli index


seanfseanf 1268966700|%e %b %Y, %H:%M %Z|agohover

Couple of missing things:

The main class APojo should have the annotation Cli:
@Cli(name="something")
public class APojo {

The left over arguments will not be passed in unless the class has a setter method (naturally enough):
public void setArguments(List<String> arguments) {
this.arguments = arguments;
}

See also https://bugs.launchpad.net/jcli/+bug/537132 .

Reply  |  Options
Unfold by seanfseanf, 1268966700|%e %b %Y, %H:%M %Z|agohover
Add a New Comment
page_revision: 1, last_edited: 1268982358|%e %b %Y, %H:%M %Z (%O ago)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License