groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Paul King (JIRA)" <j...@apache.org>
Subject [jira] [Closed] (GROOVY-6537) Support getParsedOptionValue() in OptionAccessor.getProperty()
Date Tue, 02 May 2017 02:04:16 GMT

     [ https://issues.apache.org/jira/browse/GROOVY-6537?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]

Paul King closed GROOVY-6537.
-----------------------------

> 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-alpha-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.15#6346)

Mime
View raw message