You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Merlin Beedell <MB...@cryoserver.com> on 2024/01/16 15:42:29 UTC

Displaying the entered command-line options as parsed by clibuilder

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


RE: Displaying the entered command-line options as parsed by clibuilder

Posted by Merlin Beedell <MB...@cryoserver.com>.
And if anyone is interested to print the cliBuilder usage in the order that you added them, try

def cli = new CliBuilder(usage:'Blaa')
cli.x(..)
cli.a(..)
cli.formatter.setOptionComparator({a,b -> 1})

cli.usage() will now print in the order added (x then a).

Merlin Beedell
From: Merlin Beedell <MB...@cryoserver.com>
Sent: Tuesday, January 16, 2024 3:42 PM
To: users@groovy.apache.org
Subject: Displaying the entered command-line options as parsed by clibuilder

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


Re: Displaying the entered command-line options as parsed by clibuilder

Posted by MG <mg...@arscreat.com>.
Hi Merlin,

thanks for sharing G-)

Cheers,
mg

On 16/01/2024 16:42, Merlin Beedell wrote:
>
> 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
>