2011-04-27 5 views
6

Groovy CliBuilder를 사용하여 명령 줄 옵션을 구문 분석하려고합니다. 짧은 옵션없이 여러 개의 긴 옵션을 사용하려고합니다. 나는 그것이 짧은 옵션 이름을 무시하고 단지 긴 옵션 이름을 사용할 때이 옵션에 "_"값을 여러 개를 사용할 수 있어야 끝내 문서에 따르면Groovy CliBuilder : 마지막 LongOpt 만 사용됩니다.

def cli = new CliBuilder(usage: 'Generate.groovy [options]') 
    cli.with { 
     h longOpt: "help", "Usage information" 
     r longOpt: "root", args: 1, type: GString, "Root directory for code generation" 
     x args: 1, type: GString, "Type of processor (all, schema, beans, docs)" 
     _ longOpt: "dir-beans", args: 1, argName: "directory", type: GString, "Custom location for grails bean classes" 
     _ longOpt: "dir-orm", args: 1, argName: "directory", type: GString, "Custom location for grails domain classes" 
    } 
    options = cli.parse(args) 

    println "BEANS=${options.'dir-beans'}" 
    println "ORM=${options.'dir-orm'}" 

    if (options.h || options == null) { 
     cli.usage() 
     System.exit(0) 
    } 

: 나는 다음과 같은 프로세서가 . Groovy의 문서에 따르면

Another example showing long options (partial emulation of arg 
processing for 'curl' command line): 
def cli = new CliBuilder(usage:'curl [options] <url>') 
cli._(longOpt:'basic', 'Use HTTP Basic Authentication') 
cli.d(longOpt:'data', args:1, argName:'data', 'HTTP POST data') 
cli.G(longOpt:'get', 'Send the -d data with a HTTP GET') 
cli.q('If used as the first parameter disables .curlrc') 
cli._(longOpt:'url', args:1, argName:'URL', 'Set URL to work with') 

Which has the following usage message: 

usage: curl [options] <url> 
    --basic   Use HTTP Basic Authentication 
    -d,--data <data> HTTP POST data 
    -G,--get   Send the -d data with a HTTP GET 
    -q     If used as the first parameter disables .curlrc 
    --url <URL>  Set URL to work with 
This example shows a common convention. When mixing short and long 

이름을 짧은 이름은 종종 크기 일 개 문자입니다. 인수가있는 한 문자 옵션에는 옵션과 인수 사이에 공백이 필요하지 않습니다. -Ddebug = true. 예제에서는 짧은 옵션을 적용 할 수없는 경우 '_'사용을 보여줍니다.

또한 '_'이 여러 번 사용되었습니다. 다른 shortOpt 또는 longOpt가 반복되는 경우 지원되지만 이면 동작은 정의되지 않습니다.

http://groovy.codehaus.org/gapi/groovy/util/CliBuilder.html

나는이 "_"그것은 단지 목록의 마지막 하나를 받아 사용

(마지막 하나가 발생). 내가 잘못된 것을하고 있습니까, 아니면이 문제를 해결할 수있는 방법이 있습니까?

감사합니다.

답변

2

나는 원래의 코드가 올바르게 작동한다는 것을 알게되었습니다. 작동하지 않는 것은 with 인클로저에 내장 된 모든 옵션을 사용하고 자세한 사용법을 인쇄하는 기능입니다. 사용을 인쇄 CliBuilder에 내장 된 함수 호출은 다음과 같습니다

usage: Generate.groovy [options] 
    --dir-orm <directory> Custom location for grails domain classes 
-h,--help     Usage information 
-r,--root     Root directory for code generation 
-x       Type of processor (all, schema, beans, docs) 

이 사용 라인은 내가 옵션을 누락처럼 보이게 :

cli.usage()

원래 위의 코드는 다음과 같은 사용 행을 인쇄 . 이 사용 함수 호출과는 별도로 각 개별 항목을 인쇄하지 않는 실수를했습니다. 이것이 마치 with 인클로저의 마지막 _ 항목에만 신경을 썼던 것처럼 보입니다. 나는 값을 전달하는 것을 증명하기 위해이 코드를 추가 : 나는 또한 당신이 긴 옵션 사이 =을 사용해야한다는 것을 발견

println "BEANS=${options.'dir-beans'}" 
    println "ORM=${options.'dir-orm'}" 

을 그리고 값이다하거나 (--long-option=some_value)

3

당신이 의미하는 것이 확실하지 않은 것은 마지막 것만 받아들입니다. 그러나 이것은 작동해야합니다 ...

def cli = new CliBuilder().with { 
    x 'something', args:1 
    _ 'something', args:1, longOpt:'dir-beans' 
    _ 'something', args:1, longOpt:'dir-orm' 
    parse "-x param --dir-beans beans --dir-orm orm".split(' ') 
} 
assert cli.x == 'param' 
assert cli.'dir-beans' == 'beans' 
assert cli.'dir-orm' == 'orm' 
+0

제대로 명령 줄 옵션을 구문 분석하지 않습니다 fyi,이 코드를 시도하면 두 번째 예외가 실패합니다. 구문 분석 함수 호출에서 긴 인수 사이에'='를 추가해야했습니다 :'parse "-x param --dir-beans = beans --dir-orm = orm".split ('')'. 나는 인수가 등호를 요구한다고 생각한다. – jmq

+0

@jmquigley 그 문제가 없습니다. 아마도 내가 (1.8에있어) 몇 가지 오래된 오류가있을 수 있으므로 사용중인 버전을 묻기 시작 했어야했다. – jpertino

관련 문제