groovy
  1. groovy
  2. GROOVY-3272

Passing invalid arguments to CliBuilder.parse does not result in any informative error msg or exception.

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Minor Minor
    • Resolution: Not A Bug
    • Affects Version/s: 1.6-beta-2
    • Fix Version/s: None
    • Labels:
      None
    • Environment:
      Windows vista 64 bit.
    • Testcase included:
      yes
    • Number of attachments :
      0

      Description

      When i pass garbage arguments as part of my command line i get no indication that anything is amiss...

      I think it would be better to have a mode in which command line processing is 'strict' and anything that is not specified as a valid option triggers some kind of informative err msg or exception...

      If you agree that this suggestion is worthwhile and you want help implementing the feature pls drop you a line and i can work on a patch...
      it seems like it should be pretty straight forward.

      thanks...
      /chris

      Here is a test case that demonstrates the issue >>

      //
      // Generated from archetype; please customize.
      //

      package com.lackey

      import groovy.util.GroovyTestCase

      /**

      • Tests for the {@link Example}

        class.
        */
        class NewTest extends GroovyTestCase
        {
        protected static CliBuilder getOptionParser(PrintWriter print_writer)

        { def cli = new CliBuilder([writer: print_writer]); cli.r( argName: 'dir', longOpt: 'rootDirectory', args: 1, required: false, 'root directory from which to start the scan ' + '- Default: root (/) if not specified', ); return cli; }

      protected static OptionAccessor getParsedCommandLineOptions(
      String[] args,
      PrintWriter pw= new PrintWriter(System.out)) {
      // try/catch so we can gracefully handle NPEs in commons command line parsing library
      try

      { final CliBuilder cli = getOptionParser() return cli.parse(args) }

      catch (Exception e)

      { return null; }

      ;
      }

      void testBadArgHandling()

      { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintWriter pwriter = new PrintWriter(baos); String[] bad_args1 = ["blah", "foo"] as String[] //String[] bad_args1 = ["-r", "-u"] as String[] OptionAccessor opts = FindFile.getParsedCommandLineOptions( bad_args1, pwriter); println "Note that no errors are printed.. no exceptions are thrown" println "there is nothing to inform the user that he/she is using invalid arguments " print "opts are = " + opts.toString() print "parse result = " + baos.toString() }

      }

        Activity

        Hide
        chris bedford added a comment -

        woops .. i meant pls drop me a line...(not you)...
        cb

        Show
        chris bedford added a comment - woops .. i meant pls drop me a line...(not you)... cb
        Hide
        chris bedford added a comment -

        Now I realize that this is actually not a bug...

        It seems you can call the OptionAccessor.arguments() after you finish parsing, and if there is anything left over you know that there are (possibly, depending on your case) some garbage args.. .and you can take action at that point.

        This wasn't clear from my reading of the groovy in action book or any of the docs.. maybe it warrants a mention ?

        thanks
        -chris

        Show
        chris bedford added a comment - Now I realize that this is actually not a bug... It seems you can call the OptionAccessor.arguments() after you finish parsing, and if there is anything left over you know that there are (possibly, depending on your case) some garbage args.. .and you can take action at that point. This wasn't clear from my reading of the groovy in action book or any of the docs.. maybe it warrants a mention ? thanks -chris

          People

          • Assignee:
            Unassigned
            Reporter:
            chris bedford
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: