You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (JIRA)" <ji...@apache.org> on 2017/05/02 02:04:16 UTC
[jira] [Closed] (GROOVY-6537) Support getParsedOptionValue() in
OptionAccessor.getProperty()
[ 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)