Package org.kohsuke.args4j

Examples of org.kohsuke.args4j.CmdLineParser


    public void testParseWithOneParam() throws Exception {

        TestBean bean = new TestBean();

        CmdLineParser parser = new CmdLineParser(bean);
        parser.parseArgument("test1", "test2", "-opt", "test3");

        Assert.assertEquals(Arrays.asList("test3"), Arrays.asList(bean.stringArray));
        Assert.assertEquals(Arrays.asList("test1", "test2"), Arrays.asList(bean.rest));
    }
View Full Code Here


   
    public void testParseWithTwoParams() throws Exception {

        TestBean bean = new TestBean();

        CmdLineParser parser = new CmdLineParser(bean);
        parser.parseArgument("test1", "test2", "-opt", "test3", "-opt", "test4");

        Assert.assertEquals(Arrays.asList("test3", "test4"), Arrays.asList(bean.stringArray));
        Assert.assertEquals(Arrays.asList("test1", "test2"), Arrays.asList(bean.rest));
    }
View Full Code Here

   public void testParseWithNoDefault() throws Exception {

        TestBean bean = new TestBean();
        bean.stringArray = null; // remove
       
        CmdLineParser parser = new CmdLineParser(bean);
        parser.parseArgument("test1", "test2", "-opt", "test3", "-opt", "test4");

        Assert.assertEquals(Arrays.asList("test3", "test4"), Arrays.asList(bean.stringArray));
        Assert.assertEquals(Arrays.asList("test1", "test2"), Arrays.asList(bean.rest));
    }   
View Full Code Here

    @Option(name="-b")
    int y;

    public void test1() throws Exception {
        CmdLineParser p = new CmdLineParser(this);
        p.parseArgument("-a","3","-b","2");
        assertEquals(x,3);
        assertEquals(y,2);

        p = new CmdLineParser(this);
        try {
            p.parseArgument("-b","2");
            fail();
        } catch(CmdLineException e) {
            System.out.println(e.getMessage());
            assertTrue(e.getMessage().contains("-a"));
        }
View Full Code Here

        throw new CmdLineException(owner, Messages.ILLEGAL_OPERAND, option.toString(), subCmd);
    }

    protected Object subCommand(SubCommand c, final Parameters params) throws CmdLineException {
        Object subCmd = instantiate(c);
        CmdLineParser p = configureParser(subCmd,c);
        p.parseArgument(new AbstractList<String>() {
            @Override
            public String get(int index) {
                try {
                    return params.getParameter(index+1);
                } catch (CmdLineException e) {
View Full Code Here

        });
        return subCmd;
    }

    protected CmdLineParser configureParser(Object subCmd, SubCommand c) {
        return new CmdLineParser(subCmd);
    }
View Full Code Here

            //no-op
        }
    }

    public void parseArgs(String[] args) throws IOException {
        CmdLineParser parser = new CmdLineParser(this);
        try {
            parser.parseArgument(args);
        } catch( CmdLineException e ) {
            int start = e.getMessage().indexOf('"')+1;
            int end   = e.getMessage().lastIndexOf('"');
            String wrongArgument = e.getMessage().substring(start, end);
            System.err.println("Unknown argument: " + wrongArgument);
            System.err.println("ant [options] [target [target2 [target3] ...]]");
            parser.printUsage(System.err);
            System.err.println();
            throw new IOException();
        }
    }
View Full Code Here

    public static void main(String[] args) throws IOException {
        new SampleMain().doMain(args);
    }

    public void doMain(String[] args) throws IOException {
        CmdLineParser parser = new CmdLineParser(this);
       
        // if you have a wider console, you could increase the value;
        // here 80 is also the default
        parser.setUsageWidth(80);

        try {
            // parse the arguments.
            parser.parseArgument(args);

            // you can parse additional arguments if you want.
            // parser.parseArgument("more","args");

            // after parsing arguments, you should check
            // if enough arguments are given.
            if( arguments.isEmpty() )
                throw new CmdLineException(parser,"No argument is given");

        } catch( CmdLineException e ) {
            // if there's a problem in the command line,
            // you'll get this exception. this will report
            // an error message.
            System.err.println(e.getMessage());
            System.err.println("java SampleMain [options...] arguments...");
            // print the list of available options
            parser.printUsage(System.err);
            System.err.println();

            // print option sample. This is useful some time
            System.err.println("  Example: java SampleMain"+parser.printExample(ALL));

            return;
        }

        // this will redirect the output to the specified output
View Full Code Here

  /**
   * Parses options from args. Mutates this object. Prints help and exits if it should.
   */
  public static void parseOptions(MoeOptions options, List<String> args) {
    CmdLineParser parser = new CmdLineParser(options);
    boolean parseError = false;
    try {
      parser.parseArgument(processArgs(args).toArray(new String[] {}));
    } catch (CmdLineException e) {
      logger.log(Level.SEVERE, "Failure", e);
      parseError = true;
    }

    if (options.shouldDisplayHelp() || parseError) {
      parser.printUsage(System.err);
      System.exit(parseError ? 1 : 0);
    }
  }
View Full Code Here

      // DirectiveFactory.makeDirective has failed and reported the error.
      System.exit(1);
    }

    MoeOptions flags = d.getFlags();
    CmdLineParser parser = new CmdLineParser(flags);
    boolean parseError = false;
    try {
      List<String> nextArgs = Lists.newArrayList(args);
      nextArgs.remove(0); // Remove the directive.
      parser.parseArgument(
          processArgs(nextArgs).toArray(new String[] {}));
    } catch (CmdLineException e) {
      logger.log(Level.SEVERE, "Failure", e);
      parseError = true;
    }

    if (flags.shouldDisplayHelp() || parseError) {
      parser.printUsage(System.err);
      System.exit(parseError ? 1 : 0);
    }

    try {
      int result = d.perform();
View Full Code Here

TOP

Related Classes of org.kohsuke.args4j.CmdLineParser

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.