Displaying the entered command-line options as parsed by clibuilder

Merlin Beedell <[email protected]>
Newsgroups gmane.comp.lang.groovy.user
Message-ID <LO6P123MB719103A1F6FDF6777C4F21B8B9732@LO6P123MB7191.GBRP123.PROD.OUTLOOK.COM>
I thought this might be useful... I wanted an easy way to print the parameters passed to groovy CliBuilder.
Unlike the usage() method that prints the 'how to use' (header/parameter options/footer), I just wanted to print the final parsed value set.
(Perhaps something like this could be incorporated into CliBuilder's OptionAccessor - a pretty print method?)
It turns out to be a one liner [for basic single args] and 1 more for additional args.  For example...

def cli = new CliBuilder(usage:'''Cryoserver Export to PST''', footer:'''
V1.0 MGB Jul 2020.''')
cli.i(longOpt:'input', args:1, required: true, argName:'filepath', type:File, 'a directory containing Cryoserver Export zip files')
cli.o(longOpt:'output', args:1, required: true, argName:'filepath', type:File, 'output into this directory')
cli.d(longOpt:'depth',args:1, required: false, argName:'number', type:int, defaultValue: '1', 'levels of subdirectories to traverse. Default 1.')
cli.m(longOpt:'max',args:1, required: false, argName:'number', type:int, defaultValue: '50000', 'Max num mails per pst. Default 50000.')
cli.t(longOpt:'threads',args:1, required: false, argName:'number', type:int, defaultValue: '4', 'number of threads. Default 4.')
cli.w(longOpt:'wrapped', args:0, required: false, defaultValue:false, 'Include Wrapper? Default = false (strip any wrapper)')
cli.x(longOpt:'filefilter', args:1, required: false, argName:'regex', defaultValue: fileMatch, "a regex to select required export files. default = $fileMatch")
cli.'?'(longOpt:'help', 'help')

def opts = cli.parse(['-i','b','-o','c', 'plus', 'more'])
if (!opts) { //if invalid opts then it prints usage by default
  return
}
cli.options.options.each {println "-$it.option(${it.longOpt}) = ${opts[(it.option)]}"}
if (opts.arguments()) {println opts.arguments()}

prints (note - args are in the same order as defined):
-i(input) = b
-o(output) = c
-d(depth) = 1
-m(max) = 50000
-t(threads) = 4
-w(wrapped) = false
-x(filefilter) = .*
-?(help) = false
  [Plus, more]

Merlin Beedell
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.