Skip to content

Instantly share code, notes, and snippets.

@jpertino
Created December 30, 2010 14:32

Revisions

  1. jpertino created this gist Dec 30, 2010.
    32 changes: 32 additions & 0 deletions cliBuilderExample.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    def cli = new CliBuilder().with {
    usage = 'program [options] <arguments>'
    header = 'Options:'
    footer = '-' * width

    s 'simplest boolean option'

    b longOpt: 'both', 'boolean option with both longop and shortop'

    _ longOpt: 'no-shortop-1', 'boolean option without short version 1'
    _ longOpt: 'no-shortop-2', 'boolean option without short version 2'

    n args:1, argName:'thing', 'simple argument option'
    r args:1, argName:'thing', 'required argument option', required: true

    u args:2, 'key value argument option, no clue'
    v args:2, argName:'property=value', valueSeparator:'=', 'key=value argument option'

    parse(args) ?: System.exit(1)
    }

    // -b --no-shortop-1 -r req -u vvv www -v xxx=yyy qwer asdf "1234 5678" zxcv cvbn

    println 'Arguments: ' << cli.arguments()
    println '-s: ' << cli.s
    println '-b: ' << cli.b
    println '--no-shortop-1: ' << cli.'no-shortop-1'
    println '--no-shortop-2: ' << cli.'no-shortop-2'
    println '-n: ' << cli.n
    println '-r: ' << cli.r
    println '-u: ' << cli.us
    println '-v: ' << cli.vs