XP Class RunnerScope of ChangeA runner script (similar to the unittest runner) will be created to run classes as scripts. Rationale
Simplify command line utilities development. Functionality
Overview
Instead of writing a complete script we now declare a class as follows: class DumpNews extends Command { We can run this class by using the cli-runner: $ xpcli -c ~/.xp/news/ de.thekid.blog.DumpNews 1
Rules
* The class must extend the util.cmd.Command class * Arguments and resources are injected by means of annotated methods * We use an output stream to write data * The class can be reused in different environments because it is not dependant on anything console-specific.
The xpcli runnerUsage: $ xpcli [runner-args]* [fully.qualified.ClassName] [class-args]*
The runner-args are any arguments beginning with a dash ("-"). The class
name argument is the first argument without a dash in front of it. * -c | --config Path to configuration files, defaults to "etc/". Class-args: * -? | --help Always supported. Shows a usage generated from the @arg-methods' apidoc comments.
The util.cmd.Command class
class Command extends Object { The out and err members are created by the runner and are
PrintOutputStream objects. For the console runner, they write data
to STDOUT and STDERR. The @arg annotation
This annotation marks methods as parameter acceptors. * Methods that wish to accept a command line argument must have exactly one argument. * This argument may have a default value. * If the method does not have a default value, the parameter passing stops hard at this point. * Otherwise, the method is invoked without arguments, so that the default value will be passed. How to accept arguments:
#[@arg(position= 0)] This will take the first command line argument
#[@arg(name= 'verbosity')] This will take the value passed as --verbosity=<value> or -v <value>
#[@arg] This will take the value passed as --classname=<value> or -c <value>
#[@arg] This method will be called only if --debug is supplied. Note for cases 2), 3) and 4): The short argument's name is calculated by using the first character of the long argument's name. It can be overwritten to resolve ambiguous names by adding a short= '[CHARACTER]' to the @arg annotation.
The @inject annotation
This annotation marks methods as resource injectors. * Resources are one of the following: database connections, logger
categories, properties.
* Resources are configured by property objects:
- The property manager's lookup path is set to "etc/" per default
and may be set via xpcli -c <path>
- The connection manager is configured by database.ini
- The logger is configured by log.ini
#[@inject(type= 'rdbms.DBConnection', name= 'news')] This method will receive a DBConnection instance (via
ConnectionManager::getInstance()->getByHost('news', 0))
#[@inject(type= 'util.log.LogCategory', name= 'default')] This method will receive a LogCategory instance (via
Logger::getInstance()->getCategory('default'))
#[@inject(type= 'util.Properties', name= 'app')] This method will receive a LogCategory instance (via
PropertyManager::getInstance()->getProperties('app')) Security considerations
n/a Speed impact
n/a Dependencies
- RFC #0088
I/O Streams API Related documents
- http://experiments.xp-framework.net/?people,friebe,cli
Experiment Comments
| Table of contents |