2017-05-24 9 views
0

Groovysh 문제가 있습니다. 여기서는 루프 컨텍스트 또는 함수 내부에서 goovysh 명령을 사용할 수 없습니다. 명령은 런타임 대신 구문 분석시 평가됩니다.Groovysh는 루프 내에서 사용자 정의 명령을 사용합니다.

이 문제를 해결할 수있는 마술 구문이 있습니까?

import org.codehaus.groovy.tools.shell.CommandSupport 
import org.codehaus.groovy.tools.shell.Groovysh 

class Rand extends CommandSupport { 
    private Random random = new Random() 

    protected Rand(final Groovysh shell) { 
     super(shell, 'rand', 'r') 
    } 

    public Integer execute(List args) { 
     random.nextInt() 
    } 

} 

:register Rand 

(1..3).each { 
    println "number ${it}" 
    rand 
    foo = _ 
    println "Random number is ${foo}" 
} 
당신은 임의의 숫자가 변경되지 않으며, 당신은 내가 콘솔에 코드를 붙여 넣을 때 평가 된 것을 볼 수 있습니다 것을 볼 실행

하지만 이전 : 여기

이의 예입니다 직접 쉘 또는 뭔가를 참조하는 다른 구문을 통해 사용자 정의 명령을 참조 할 수있는 방법이 있다는 것을

Groovy Shell (2.4.11, JVM: 1.8.0_51) 
Type ':help' or ':h' for help. 
----------------------------------------------------------------------------------------------------------------------- 
groovy:000> import org.codehaus.groovy.tools.shell.CommandSupport 
===> org.codehaus.groovy.tools.shell.CommandSupport 
groovy:000> import org.codehaus.groovy.tools.shell.Groovysh 
===> org.codehaus.groovy.tools.shell.CommandSupport, org.codehaus.groovy.tools.shell.Groovysh 
groovy:000> 
groovy:000> class Rand extends CommandSupport { 
groovy:001>  private Random random = new Random() 
groovy:002> 
groovy:002>  protected Rand(final Groovysh shell) { 
groovy:003>   super(shell, 'rand', 'r') 
groovy:004>  } 
groovy:005> 
groovy:005>  public Integer execute(List args) { 
groovy:006>   random.nextInt() 
groovy:007>  } 
groovy:008> 
groovy:008> } 
===> true 
groovy:000> 
groovy:000> :register Rand 
===> true 
groovy:000> 
groovy:000> (1..3).each { 
groovy:001>  println "number ${it}" 
groovy:002>  rand 
===> -1321819102 
groovy:002>  foo = _ 
groovy:003>  println "Random number is ${foo}" 
groovy:004> } 
number 1 
Random number is -1321819102 
number 2 
Random number is -1321819102 
number 3 
Random number is -1321819102 
===> [1, 2, 3] 
groovy:000> 

내가 바라고 있어요 : 루프를 통해 갔다.

답변

0

좋아, 나는 단지 약간의 해킹 해법을 생각해 냈다. 이 작업을 수행하는 또 다른 방법이 있나요

Groovy Shell (2.4.11, JVM: 1.8.0_51) 
Type ':help' or ':h' for help. 
----------------------------------------------------------------------------------------------------------------------- 
groovy:000> import org.codehaus.groovy.tools.shell.CommandSupport 
===> org.codehaus.groovy.tools.shell.CommandSupport 
groovy:000> import org.codehaus.groovy.tools.shell.Groovysh 
===> org.codehaus.groovy.tools.shell.CommandSupport, org.codehaus.groovy.tools.shell.Groovysh 
groovy:000> 
groovy:000> class Rand extends CommandSupport { 
groovy:001>  private Random random = new Random() 
groovy:002> 
groovy:002>  protected Rand(final Groovysh shell) { 
groovy:003>   super(shell, 'rand', 'r') 
groovy:004>  } 
groovy:005> 
groovy:005>  public Integer execute(List args) { 
groovy:006>   random.nextInt() 
groovy:007>  } 
groovy:008> 
groovy:008> } 
===> true 
groovy:000> 
groovy:000> :register Rand 
===> true 
groovy:000> 
groovy:000> class Shell extends CommandSupport { 
groovy:001> 
groovy:001>  private Groovysh shellint 
groovy:002> 
groovy:002>  protected Shell(final Groovysh shell) { 
groovy:003>   super(shell, 'shell', 's') 
groovy:004>   shellint = shell 
groovy:005>  } 
groovy:006> 
groovy:006>  public Groovysh execute(List args) { 
groovy:007>   shellint 
groovy:008>  } 
groovy:009> 
groovy:009> } 
===> true 
groovy:000> 
groovy:000> :register Shell 
===> true 
groovy:000> 
groovy:000> shell 
===> [email protected] 
groovy:000> myshell = _ 
===> [email protected] 
groovy:000> 
groovy:000> (1..3).each { 
groovy:001>  println "number ${it}" 
groovy:002>  foo = myshell.execute("rand") 
groovy:003>  println "Random number is ${foo}" 
groovy:004> } 
number 1 
===> -666149132 
Random number is -666149132 
number 2 
===> -1675600826 
Random number is -1675600826 
number 3 
===> 412144734 
Random number is 412144734 
===> [1, 2, 3] 

:

import org.codehaus.groovy.tools.shell.CommandSupport 
import org.codehaus.groovy.tools.shell.Groovysh 

class Rand extends CommandSupport { 
    private Random random = new Random() 

    protected Rand(final Groovysh shell) { 
     super(shell, 'rand', 'r') 
    } 

    public Integer execute(List args) { 
     random.nextInt() 
    } 

} 

:register Rand 

class Shell extends CommandSupport { 

    private Groovysh shellint 

    protected Shell(final Groovysh shell) { 
     super(shell, 'shell', 's') 
     shellint = shell 
    } 

    public Groovysh execute(List args) { 
     shellint 
    } 

} 

:register Shell 

shell 
myshell = _ 

(1..3).each { 
    println "number ${it}" 
    foo = myshell.execute("rand") 
    println "Random number is ${foo}" 
} 

을의 출력과 다음 Groovysh 인스턴스의 보류를 얻기 내가 기분 때 평가 수 있음을 의미? 나는이 필요로하는 맥락에서 groovysh 내가 다시에서 :register 명령을 추가 할 groovysh jar 파일을 변형 한

제거 :register와 사용자 정의 하나이고, 나는 위의 솔루션을 사용할 수 있습니다. 나는 이것을 https://github.com/groovy/groovy-core/blob/master/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/Groovysh.groovy을보고, org/codehaus/groovy/tools/shell/commands.xml에 명령 목록이 포함되어 있음을보고, 목록에 <command>org.codehaus.groovy.tools.shell.commands.RegisterCommand</command>을 추가했습니다.