Skip to content
lefou edited this page Dec 2, 2014 · 4 revisions

Welcome to the CmdOption wiki!

For an official introductory read, please refer to the projects Readme document (absolute link).

In this wiki you find further information, documentation and examples. Feel free to edit this wiki to provide more information, examples or corrections.

Examples and Solutions

Using a CmdOption Config Object in a Spring Application Context

The following example is in Scala, but it works with Java too.

This example uses the Java-based configuration mechanism introduced in Spring Framework 3.0 instead of a XML-based configuration.

It shows to commandline-based configuration of a datasource, and how this configuration can be injected into the Spring Application Context to make it available for further use.

The Config Class
classConfig {
@CmdOption(names =Array("--db-driver"), args =Array("CLASSNAME"), description ="The class name of the JDBC driver")
vardriverClassName:String=classOf[org.h2.Driver].getName
@CmdOption(names =Array("--db-url"), args =Array("URL"), description ="The Database URL", minCount =1)
vardbUrl:String= _
@CmdOption(names =Array("--db-username"), args =Array("NAME"), description ="Database connection username")
vardbUsername:String= _
@CmdOption(names =Array("--db-password"), args =Array("PASSWORD", description ="Database connection password")
vardbPassword:String= _
}
The Spring Configuration
@ConfigurationclassMySpringConfig {
@Autowiredvarconfig:Config= _
@Bean(destroyMethod ="close") defdataSource:DataSource= {
valdataSource=newBasicDataSource()
dataSource.setDriverClassName(config.driverClassName)
dataSource.setUrl(config.dbUrl)
if(config.dbUsername !=null) {
dataSource.setUsername(config.dbUsername)
dataSource.setPassword(config.dbPassword)
}
dataSource
}
@BeandefmyService:MyService=newMyServiceImpl(dataSource)
}
The Application
objectMyApplication {
defmain(args: Array[String]) {
valgeneralOpts=new {
@CmdOption(names =Array("-h", "--help"), isHelp =true, description ="Show this help") varhelp=false
}
valconfig=newConfig()
valcp=newCmdlineParser(config, generalOpts)
cp.parse(args: _*)
if(generalOpts.help) {
cp.usage
System.exit(0)
}
newMyApplication(config).run
}
}
classMyApplication(config: Config) {
privatevalspringContext= {
valstaticContext=newStaticApplicationContext()
staticContext.getBeanFactory.registerSingleton("config", config)
staticContext.refresh
valspringContext=newAnnotationConfigApplicationContext()
springContext.register(classOf[MySpringConfig])
springContext.setParent(staticContext)
springContext.refresh
springContext
}
defrun {
valmyService= springContext.getBean(classOf[MyService])
myService.doSomethingWithDatabase
}
}

Annotated Methods Example

This example shows, how to annotation methods (instead of fields) to control what should happen, if CmdOption parses a given option.

publicclassConfig {
privateintlogLevel = 0;
privatefinalMap<String, String> options = newLinkedHashMap<String, String>();
privatefinalList<String> names = newLinkedList<String>();
// With each "-v" option increase the log level.@CmdOption(names = {"--verbose", "-v"}, maxCount = -1, description = "Be verbose, more verbose, ... (can be used multiple times)")
publicaddincrementLogLevel() {
++logLevel;
}
@CmdOption(names = {"--options", "-o"}, args = {"name", "value"}, maxCount = -1, description = "Additional options when processing names")
publicvoidaddToOptions(Stringname, Stringvalue) {
options.put(key, value);
}
@CmdOption(args = {"file"}, description = "Names to process", minCount = 1, maxCount = -1)
publicvoidaddToNames(Stringfile) {
names.add(file);
}
}

Simple Scala Example

This simple example shows, that it is easy to use CmdOption in Scala.

importde.tototec.cmdoption.CmdlineParserimportde.tototec.cmdoption.CmdOptionclassConfig {
@CmdOption(names =Array("--verbose", "-v"), description ="Be verbose when running.")
varverbose=false
}
objectMyApp {
defmain(args: Array[String]) {
valconfig=newConfig()
valcp=newCmdlineParser(config)
cp.parse(args: _*)
if(config.verbose) println("Starting...")
// ...
}
}