Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7495611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:14:05+00:00 2026-05-29T18:14:05+00:00

I am learning Groovy CliBuilder and I find it great, except, I don’t know

  • 0

I am learning Groovy CliBuilder and I find it great, except, I don’t know how to recognise wrong arguments. Consider the following example code:

def cli = new CliBuilder()
cli.s args: 1, longOpt: 'sdkdir', 'sdkdir usage info'
cli.h args: 0, longOpt: 'help', 'print usage information'
def opt = cli.parse(args)
if (!opt) {
    //how to be in this case? seems I can never reach here
    println "something went wrong, but I don't know what"
} else if (opt.h) {
    cli.usage()
} else (!opt.s) {
    println "missing required option -s, try with --help for more information"
} else {
    //do something
}

If I call my script with, for instance, -p, which is an invalid option nothing happens. Similarly, if I add arguments after the options they aren’t detected too.

How can I detect and signal the error?

Also, a small inconvenience is that in my example -s is a required parameter, so in theory, I could add required: true, in practice I can’t or it would be required also with -h, but I think testing it with an if is fine, unless there is a better way.

My real problem is about finding unwanted options and arguments. Any help appreciated, thank you.

UPDATE: Thanks @rodion for your input, I guess I will settle with good enough instead of perfect for the sake of simplicity. Here is what I came up with:

#!/usr/bin/groovy
def cli = new CliBuilder(usage: 'cliTest -s sdkdir {projectName}', 
                         header: 'Command line parameter parsing test in Groovy')
cli.s longOpt: 'sdkdir', args: 1, 'sdkdir usage info, REQUIRED'
cli.h longOpt: 'help', 'print usage information'
def opt = cli.parse(args)
def errMsg = "Invalid arguments.\nusage: ${cli.usage}\n" + 
        "Try `cliTest --help' for more information."
if (!opt) {
    //should never happen, since I don't have required parameters in CliBuilder
    println "error processing arguments\n"
} else if (opt.h) {
    cli.usage()
} else if (!opt.s) {
    println errMsg
} else if (opt.arguments().size() != 1) {
    println errMsg
} else {
    println "Creating project ${opt.arguments()[0]}, sdkdir ${opt.s.value}" 
}

This solution is good enough, but not perfect because it doesn’t tell you which parameter is wrong, but just tells you with a concise message or prints usage information. Here are some tests:

$ ./cliTest 
Invalid arguments.
usage: cliTest -s sdkdir {projectName}
Try `cliTest --help' for more information.

$ ./cliTest -a
Invalid arguments.
usage: cliTest -s sdkdir {projectName}
Try `cliTest --help' for more information.

$ ./cliTest -a -s ../sdkdir
Invalid arguments.
usage: cliTest -s sdkdir {projectName}
Try `cliTest --help' for more information.

$ ./cliTest -s ../sdkdir
Invalid arguments.
usage: cliTest -s sdkdir {projectName}
Try `cliTest --help' for more information.

$ ./cliTest -s ../sdkdir projectName
Creating project projectName, sdkdir ../sdkdir

$ ./cliTest -s ../sdkdir projectName wrong
Invalid arguments.
usage: cliTest -s sdkdir {projectName}
Try `cliTest --help' for more information.

$ ./cliTest -s ../sdkdir -a  projectName 
Invalid arguments.
usage: cliTest -s sdkdir {projectName}
Try `cliTest --help' for more information.

$ ./cliTest -s
error: Missing argument for option: s
usage: cliTest -s sdkdir {projectName}
Command line parameter parsing test in Groovy
 -h,--help           print usage information
 -s,--sdkdir <arg>   sdkdir usage info, REQUIRED
error processing arguments

For my purposes, I’m more than satisfied, but if anyone knows a better way, let me know.

Also, I figured out that the !opt case can happen when there is a required: true option and the argument is missing, but from my understanding it can never be used, since otherwise is not possible to have an help option alone.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T18:14:08+00:00Added an answer on May 29, 2026 at 6:14 pm

    This seems to be related to this issue. Apparently you can inspect opt after parsing (but if it’s null, then I guess you are left with nothing). Oh, and the issue is apparenly Not A Bug, hmm… wierd.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm learning groovy and I have a question concerning its metaprogramming facilities. From what
Just started learning Groovy, got the PragProg book Programming Groovy and had a problem
Learning Ruby. Needed to create a hash of arrays. This works... but I don't
Learning Clips, while I don't mind the syntax I'm finding it difficult to derive
Learning CasperJS Trying to understand why the following is not displaying my results in
Learning spine.js I completed both the tutorials no problem, seems like a great framework,
I'm learning to use Grails and have run into a situation I don't understand
Learning sed, I want to change the drive letter of the following input lines
I have been learning Groovy for a week, but I have a problem: I
I'm learning Groovy. I want an array of numbers from 0 to n with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.