# This patch file was generated by NetBeans IDE # Following Index: paths are relative to: /home/jarda/netbeans-src/core/sendopts # This patch can be applied using context Tools: Patch action on respective folder. # It uses platform neutral UTF-8 encoding and \n newlines. # Above lines and this line are ignored by the patching process. Index: src/org/netbeans/modules/sendopts/OptionImpl.java --- /home/jarda/netbeans-src/core/sendopts/src/org/netbeans/modules/sendopts/OptionImpl.java Base (1.1) +++ /home/jarda/netbeans-src/core/sendopts/src/org/netbeans/modules/sendopts/OptionImpl.java Locally Modified (Based On 1.1) @@ -281,7 +281,32 @@ public OptionImpl findNotUsedOption(Set used) { return appear == null && used.contains(this) ? this : null; } + public static OptionImpl createAlways(Option o) { + class Always extends OptionImpl { + public Always(Option o, OptionProcessor p, int t) { + super(o, p, 5); + } + public void process(String[] additionalArgs, Map optionsAndTheirArgs) throws CommandException { + optionsAndTheirArgs.put(option, NO_VALUE); + } + public void associateValue(String value) { + throw new IllegalStateException(); + } + + public Appearance checkConsistent(Set presentOptions) { + return Appearance.YES; + } + + protected OptionImpl handleAdd(Collection allOptions) { + Always n = doClone(this); + allOptions.add(n); + return n; + } + } + return new Always(o, null, 0); + } + /** Creates an impl that delegates to given option and NoArgumentProcessor processor. */ public static OptionImpl createNoArg(Option o) { Index: src/org/netbeans/spi/sendopts/Option.java --- /home/jarda/netbeans-src/core/sendopts/src/org/netbeans/spi/sendopts/Option.java Base (1.1) +++ /home/jarda/netbeans-src/core/sendopts/src/org/netbeans/spi/sendopts/Option.java Locally Modified (Based On 1.1) @@ -70,6 +70,7 @@ case 2: this.impl = OptionImpl.createOneArg(this, true); break; case 3: this.impl = OptionImpl.createAdd(this, false); break; case 4: this.impl = OptionImpl.createAdd(this, true); break; + case 5: this.impl = OptionImpl.createAlways(this); break; default: throw new IllegalArgumentException("Type: " + type); // NOI18N } this.keys = EMPTY; @@ -292,6 +293,24 @@ return new Option(NO_SHORT_NAME, null, 4); } + /** Creates an option that is always present. This can be useful for + * processors that want to be notified everytime the command line + * is successfuly parsed. + * + * Option always = Option.always(); + * and inside of the {@link OptionProcessor} declaring this + * option use:
+     *   public void process(Env env, Map<Option,String[]> values) throws CommandException {
+     *     assert values.contains(always);
+     *   }
+ * + * @return the option that always matches correct command line + * @since 2.1 + */ + public static Option always() { + return new Option(NO_SHORT_NAME, null, 5); + } + /** Associates a name with given option. By default * the option display name is generated by the infrastructure from the * short and long name plus generic description of options arguments, this Index: test/unit/src/org/netbeans/api/sendopts/AlwaysOptionTest.java --- /home/jarda/netbeans-src/core/sendopts/test/unit/src/org/netbeans/api/sendopts/AlwaysOptionTest.java No Base Revision +++ /home/jarda/netbeans-src/core/sendopts/test/unit/src/org/netbeans/api/sendopts/AlwaysOptionTest.java Locally New @@ -0,0 +1,149 @@ +/* + * The contents of this file are subject to the terms of the Common Development + * and Distribution License (the License). You may not use this file except in + * compliance with the License. + * + * You can obtain a copy of the License at http://www.netbeans.org/cddl.html + * or http://www.netbeans.org/cddl.txt. + * + * When distributing Covered Code, include this CDDL Header Notice in each file + * and include the License file at http://www.netbeans.org/cddl.txt. + * If applicable, add the following below the CDDL Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun + * Microsystems, Inc. All Rights Reserved. + */ +package org.netbeans.api.sendopts; + +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import junit.framework.TestCase; +import org.netbeans.spi.sendopts.Env; +import org.netbeans.spi.sendopts.Option; + +/** The basic test to check semantics of always option. + * + * @author Jaroslav Tulach + */ +public class AlwaysOptionTest extends TestCase { + private CommandLine l; + private NoArgProc proc = new NoArgProc(); + private Option help; + private NoArgProc okProc = new NoArgProc(); + private Option ok; + + private Always always = new Always(); + private Option all; + + public AlwaysOptionTest(String s) { + super(s); + } + + protected void tearDown() throws Exception { + + super.tearDown(); + } + + protected void setUp() throws Exception { + + help = Option.withoutArgument('h', "help"); + Provider.clearAll(); + Provider.add(proc, help); + ok = Option.withoutArgument('o', "ok"); + Provider.add(okProc, ok); + + all = Option.always(); + Provider.add(always, all); + + l = CommandLine.getDefault(); + } + + public void testSingleNoArgOptionIsRecognized() throws Exception { + l.process(new String[] { "-h" }); + assertEquals("Processor found", help, proc.option); + assertEquals("Always", all, always.option); + } + + public void testLongOptionRecognized() throws Exception { + l.process(new String[] { "--help" }); + assertEquals("Processor found for long name", help, proc.option); + assertEquals("Always", all, always.option); + } + + public void testTwoOptionsRecognized() throws Exception { + l.process(new String[] { "-ho" }); + assertEquals("Processor for help", help, proc.option); + assertEquals("Processor for ok", ok, okProc.option); + assertEquals("Always", all, always.option); + } + + public void testAbrevatedNameRecognized() throws Exception { + l.process(new String[] { "--he" }); + assertEquals("Processor found for abbrevated name", help, proc.option); + assertEquals("Always", all, always.option); + + proc.option = null; + always.option = null; + l.process(new String[] { "--he" }); + assertEquals("Processor found for abbrevated name again", help, proc.option); + assertEquals("Always", all, always.option); + } + + + public void testIncorrectOptionIdentified() throws Exception { + try { + l.process(new String[] { "--hell" }); + fail("This option does not exists"); + } catch (CommandException ex) { + // ok + } + assertNull("No processor called", proc.option); + assertEquals("Always not called in case of failure", null, always.option); + } + + public void testNoProcessorCalledWhenOneOptionIsNotKnown() throws Exception { + try { + l.process(new String[] { "-h", "--hell" }); + fail("One option does not exists"); + } catch (CommandException ex) { + // ok + } + assertNull("No processor called", proc.option); + assertEquals("Always not called in case of failure", null, always.option); + } + + static final class NoArgProc implements Processor { + Option option; + + public void process(Env env, Map values) throws CommandException { + assertNull("Not processed yet", option); + assertEquals(1, values.size()); + option = values.keySet().iterator().next(); + assertNotNull("An option is provided", option); + + String[] args = values.get(option); + assertNotNull("Values is always [0]", args); + assertEquals("Values is always [0]", 0, args.length); + } + } + static final class Always implements Processor { + Option option; + + public void process(Env env, Map values) throws CommandException { + assertNull("Not processed yet", option); + assertEquals(1, values.size()); + option = values.keySet().iterator().next(); + assertNotNull("An option is provided", option); + + String[] args = values.get(option); + assertNotNull("Values is always [0]", args); + assertEquals("Values is always [0]", 0, args.length); + } + } +} Index: apichanges.xml --- /home/jarda/netbeans-src/core/sendopts/apichanges.xml Base (1.1) +++ /home/jarda/netbeans-src/core/sendopts/apichanges.xml Locally Modified (Based On 1.1) @@ -26,6 +26,20 @@ SendOpts SPI + + An option that is always on + + + + + + Those processors that wish to be informed about every command line + processing, can register themselves as providers of + always + option. This one is automatically present in each successfully parsed + command line. + + Completelly rewritten Index: manifest.mf --- /home/jarda/netbeans-src/core/sendopts/manifest.mf Base (1.1) +++ /home/jarda/netbeans-src/core/sendopts/manifest.mf Locally Modified (Based On 1.1) @@ -1,3 +1,3 @@ OpenIDE-Module: org.netbeans.modules.sendopts/2 OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/sendopts/Bundle.properties -OpenIDE-Module-Specification-Version: 2.0 +OpenIDE-Module-Specification-Version: 2.1