Natural CLI home | Downloads | Examples | Changelog | Javadocs | Contact
// Create the commands Command showDateCommand = new Command( "show date", "Shows the current date and time", new ICommandExecutor () { public void execute(ParseResult pr) { System.out.println(new java.util.Date().toString()); } } ); Command helloWorldCommand = new Command( "hello world", "Says hello.", new ICommandExecutor () { public void execute(ParseResult pr ) { System.out.println("Hello world!"); } } ); // Create the set of commands Set<Command> cs = new HashSet<Command>(); cs.add(showDateCommand); cs.add(helloWorldCommand); // Execute new NaturalCLI(cs).execute(args);
Example 2. A command with a parameter.
The parameters are typed and you can define your own type also.
A parameter can have a type name or a type name with a parameter name to be
shown when showing the help.
// Create the command Command helloWorldCommand = new Command( "hello world <name:string>", "Says hello to the world and especially to some one.", new ICommandExecutor () { public void execute(ParseResult pr) { System.out.println("Hello world! And hello especially to "+pr.getParameterValue(0)); } } ); // Create the set of commands Set<CommandGt; cs = new HashSet<Command>(); cs.add(helloWorldCommand); // Execute new NaturalCLI(cs).execute(args);
Example 3. A command with an optional parameter.
Defining optional parameters the command lines are more powerful.
Command showDateCommand = new Command( "hello world [<name:string>]", "Says hello to the world and, may be, especially to some one.", new ICommandExecutor () { public void execute(ParseResult pr) { System.out.print("Hello world!"); String p0 = pr.getParameterValue(0).toString(); if (p0 == null) System.out.println(); else System.out.println(" And hello especially to "+p0); } } );
Example 4. An user defined type.
It's possible to define your own types to extend the parameter type and value checking.
// Create the type IParameterType type = new IParameterType() { private final String[] dof = new String[] {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; public Object convertParameterValue(String strRepresentation) { return strRepresentation; } public String getParameterTypeName() { return "dayofweek"; } public boolean validateParameter(String value) { return Arrays.binarySearch(dof, value) != -1; } public String validationMessage(String value) { return this.validateParameter(value) ? null : "Bad day of the week name."; } }; // Create the command Command helloWorldCommand = new Command( "hello world on <day:dayofweek>", "Says hello to the world for that day.", new ICommandExecutor () { public void execute(ParseResult pr) { System.out.println("Hello world on "+pr.getParameterValue(0)); } } ); // Create the parameter validator Set<IParameterType> pts = new HashSet<IParameterType>(); pts.add(type); ParameterValidator pv = new ParameterValidator(pts); // Create the set of commands Set<Command> cs = new HashSet<Command>(); cs.add(helloWorldCommand); // Execute new NaturalCLI(cs, pv).execute(args);
Example 5. Using the default commands.
There are default commands that you can use. Help commands o be able
to output the command possibilities, execute a file with a list of
commands, ...
// Create an empty command set Set<Command> cs = new HashSet<Command>(); // Create the interpreter NaturalCLI nc = new NaturalCLI(cs); // Add the commands that can be understood cs.add(new HelpCommand(cs)); // help cs.add(new HTMLHelpCommand(cs)); // htmlhelp cs.add(new SleepCommand()); // sleep <seconds:number> cs.add(new ExecuteFileCommand(nc)); // execute file <filename:string> // Execute the command line nc.execute(args, 0);
Example 6. Variable parameter values number.
For the last parameter (and token) it's possible to define it tp get variable values number.
// Create the command Command helloWorldCommand = new Command( "hello world <name:string> ...", "Says hello to the world and especially to some people.", new ICommandExecutor () { public void execute(ParseResult pr) { System.out.print("Hello world! And hello especially to "); for (int i= 0 ; i < pr.getParameterCount() ; i++) System.out.print(pr.getParameterValue(i)+" "); System.out.println(); } } ); // Create the set of commands Set<Command> cs = new HashSet<Command>(); cs.add(helloWorldCommand); // Execute new NaturalCLI(cs).execute(args);