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 8324369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:02:41+00:00 2026-06-09T00:02:41+00:00

I have the following: final String scriptName = new File(getClass().protectionDomain.codeSource.location.path).getName() final cli = new

  • 0

I have the following:

final String scriptName = new File(getClass().protectionDomain.codeSource.location.path).getName()
final cli = new CliBuilder(
        stopAtNonOption: false,
        usage: "${scriptName} ARGUMENTS",
        width: 80)
cli.with {
    _ longOpt: "known-option-0", args: 1, argName: "KNOWN_OPTION_0", "Known option."
    _ longOpt: "known-option-1", args: 1, argName: "KNOWN_OPTION_1", "Known option."
}

I would like to be able to call it with:

options = cli.parse(['--known-option-0=0', '--unknown-option=aoeu', '--known-option-1=1'])

such that there’s no parsing error, without having to add –unknown-option to the list of accepted options, and such that both known-option-0 and known-option-1 are set.

If I set:

        stopAtNonOption: true,

no error is emitted, but:

options.'known-option-0' == 0
options.'known-option-1' == false

How can I get all known options to be parsed and ignore unknown options?

  • 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-06-09T00:02:43+00:00Added an answer on June 9, 2026 at 12:02 am
    class CliParser {
        final CliBuilder cli
        final List<Cli> options_list = []
        final List<String> arguments_list = []
    
        CliParser(CliBuilder cli) {
            this.cli = cli
        }
    
        Collection getOptions() {
            cli.options.getOptions()
        }
    
        Cli parse(List<String> args) {
            while (args.size() != 0) {
                groovy.util.OptionAccessor options = cli.parse(args)
                options_list.add(options)
    
                List<String> arguments = options.arguments()
                arguments_list.add(arguments[0])
    
                args = (arguments.size() > 1) ? arguments[1..-1] : []
            }
    
            return new Cli(options: options_list, arguments: arguments_list)
        }
    }
    
    class Cli {
        List<groovy.util.OptionAccessor> options
        List<String> arguments
    
        List<String> arguments() {
            arguments
        }
    
        List<Option> getOptions() {
            options.collect {
                it.getOptions()
            }
        }
    
        Object getProperty(String name) {
            final optionAccessor = options
                    .find {
                        it.hasOption(name)
                    }
    
            optionAccessor?.getProperty(name)
        }
    }
    
    final String scriptName = new File(getClass().protectionDomain.codeSource.location.path).getName()
    final cliBuilder = new CliBuilder(
            stopAtNonOption: false,
            usage: "${scriptName} ARGUMENTS",
            width: 80)
    cliBuilder.with {
        _ longOpt: "known-option-0", args: 1, argName: "KNOWN_OPTION_0", "Known option."
        _ longOpt: "known-option-1", args: 1, argName: "KNOWN_OPTION_1", "Known option."
    }
    
    final cliParser = new CliParser(cliBuilder)
    final cli = cliParser.parse(args)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: public void post(String message) { final String mess =
I have the following code: private static final Set<String> allowedParameters; static { Set<String> tmpSet
I have the following Java code: final Future future = exeService.submit( new Runnable() {
I have the following code public class Test{ private static final String key =
Lets say i have the following scenario: public class A { public String createString(final
Hey. I have the following code: final String text = (String) lt.getItemAtPosition(position); db.removeCategory(text); What
I have the following string: public static final String UPDATESONG = update songs SET
I have this following test code: public static final String[] list = { apple,ball,cat,dog,egg,fan,girl,hat,igloo,jerk
I have the following shader taken from the android SDK sampele: final String vertexShader
I have a line of code similar to the following: final String food =

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.