[
https://issues.apache.org/jira/browse/GROOVY-6537?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Paul King resolved GROOVY-6537.
-------------------------------
Resolution: Fixed
Assignee: Paul King
Fix Version/s: 2.5.0-beta-1
I am going to close this. There is now some minimal CLI type support in the
upcoming 2.5 beta. Any further work, if required, should be re-cast as an
enhancement to what is currently provided.
> Support getParsedOptionValue() in OptionAccessor.getProperty()
> --------------------------------------------------------------
>
> Key: GROOVY-6537
> URL: https://issues.apache.org/jira/browse/GROOVY-6537
> Project: Groovy
> Issue Type: Improvement
> Components: command line processing
> Affects Versions: 2.2.1
> Reporter: Björn Kautler
> Assignee: Paul King
> Labels: CliBuilder
> Fix For: 2.5.0-beta-1
>
>
> It would be nice if OptionAccessor.getProperty() would also use the
> getParsedOptionValue() before falling back to getOptionValue()
> Currently to use a parsed option value you have to do something like
> {code}
> def cliBuilder = new CliBuilder()
> cliBuilder.d longOpt: 'directory', args: 1, type: File, 'd option'
> def arguments = cliBuilder.parse args
> if (!arguments) {
> System.exit 1
> }
> def directory
> if (arguments.directory) {
> directory = arguments.getParsedOptionValue 'directory'
> } else {
> directory = System.properties.'user.dir' as File
> }
> {code}
> if would be nicer to instead just do
> {code}
> def cliBuilder = new CliBuilder()
> cliBuilder.d longOpt: 'directory', args: 1, type: File, 'd option'
> def arguments = cliBuilder.parse args
> if (!arguments) {
> System.exit 1
> }
> def directory
> if (arguments.directory) {
> directory = arguments.directory
> } else {
> directory = System.properties.'user.dir' as File
> }
> {code}
> I think something along the lines of changing
> {code}
> def getProperty(String name) {
> def methodname = 'getOptionValue'
> if (name.size() > 1 && name.endsWith('s')) {
> def singularName = name[0..-2]
> if (hasOption(singularName)) {
> name = singularName
> methodname += 's'
> }
> }
> if (name.size() == 1) name = name as char
> def result = InvokerHelper.getMetaClass(inner).invokeMethod(inner,
> methodname, name)
> if (null == result) result = inner.hasOption(name)
> if (result instanceof String[]) result = result.toList()
> return result
> }
> {code}
> to
> {code}
> def getProperty(String name) {
> def methodname = 'getOptionValue'
> if (name.size() > 1 && name.endsWith('s')) {
> def singularName = name[0..-2]
> if (hasOption(singularName)) {
> name = singularName
> methodname += 's'
> }
> }
> if (name.size() == 1) name = name as char
> def result = inner.getParsedOptionValue(name)
> if (null == result) result =
> InvokerHelper.getMetaClass(inner).invokeMethod(inner, methodname, name)
> if (null == result) result = inner.hasOption(name)
> if (result instanceof String[]) result = result.toList()
> return result
> }
> {code}
> should do what I mean, as getParsedOptionValue() returns null if no type is
> set or the type is unknown to the TypeHandler.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)