[Japi-cvs] SF.net SVN: japi:[1373] libs/argparser/trunk
Status: Beta
Brought to you by:
christianhujer
From: <chr...@us...> - 2009-07-25 14:26:54
|
Revision: 1373 http://japi.svn.sourceforge.net/japi/?rev=1373&view=rev Author: christianhujer Date: 2009-07-25 14:26:45 +0000 (Sat, 25 Jul 2009) Log Message: ----------- Revert previous commit - StringConverter actually was there - through the services. Modified Paths: -------------- libs/argparser/trunk/libs-argparser.iml libs/argparser/trunk/src/doc/guide/start.xhtml libs/argparser/trunk/src/doc/start.xhtml libs/argparser/trunk/src/prj/net/sf/japi/io/args/OptionType.java libs/argparser/trunk/src/prj/net/sf/japi/io/args/converter/ConverterRegistry.java Modified: libs/argparser/trunk/libs-argparser.iml =================================================================== --- libs/argparser/trunk/libs-argparser.iml 2009-07-25 14:24:07 UTC (rev 1372) +++ libs/argparser/trunk/libs-argparser.iml 2009-07-25 14:26:45 UTC (rev 1373) @@ -27,6 +27,15 @@ <SOURCES /> </library> </orderEntry> + <orderEntry type="module-library"> + <library> + <CLASSES> + <root url="jar://$MODULE_DIR$/lib/annotations2.jar!/" /> + </CLASSES> + <JAVADOC /> + <SOURCES /> + </library> + </orderEntry> </component> <component name="copyright"> <Base> @@ -42,10 +51,10 @@ <option name="lenBefore" value="80" /> <option name="lenAfter" value="80" /> <option name="box" value="false" /> - <option name="filler" value=" " /> + <option name="filler" value="$TEMPLATE$" /> </value> </option> - <option name="notice" value="JAPI libs-argparser is a library for parsing command line arguments. Copyright (C) &#36;today.year Christian Hujer. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA" /> + <option name="notice" value="Copyright (c) &#36;today.year, Your Corporation. All Rights Reserved." /> <option name="keyword" value="Copyright" /> <option name="fileTypeOverride" value="4" /> <option name="relativeBefore" value="true" /> @@ -63,7 +72,7 @@ <option name="lenBefore" value="80" /> <option name="lenAfter" value="80" /> <option name="box" value="false" /> - <option name="filler" value=" " /> + <option name="filler" value="$TEMPLATE$" /> </value> </option> <option name="notice" value="Copyright (c) &#36;today.year, Your Corporation. All Rights Reserved." /> @@ -84,7 +93,7 @@ <option name="lenBefore" value="80" /> <option name="lenAfter" value="80" /> <option name="box" value="false" /> - <option name="filler" value=" " /> + <option name="filler" value="$TEMPLATE$" /> </value> </option> <option name="notice" value="Copyright (c) &#36;today.year, Your Corporation. All Rights Reserved." /> @@ -105,7 +114,7 @@ <option name="lenBefore" value="80" /> <option name="lenAfter" value="80" /> <option name="box" value="false" /> - <option name="filler" value=" " /> + <option name="filler" value="$TEMPLATE$" /> </value> </option> <option name="notice" value="Copyright (c) &#36;today.year, Your Corporation. All Rights Reserved." /> @@ -126,7 +135,7 @@ <option name="lenBefore" value="80" /> <option name="lenAfter" value="80" /> <option name="box" value="false" /> - <option name="filler" value=" " /> + <option name="filler" value="$TEMPLATE$" /> </value> </option> <option name="notice" value="Copyright (c) &#36;today.year, Your Corporation. All Rights Reserved." /> @@ -147,7 +156,7 @@ <option name="lenBefore" value="80" /> <option name="lenAfter" value="80" /> <option name="box" value="false" /> - <option name="filler" value=" " /> + <option name="filler" value="$TEMPLATE$" /> </value> </option> <option name="notice" value="Copyright (c) &#36;today.year, Your Corporation. All Rights Reserved." /> @@ -168,7 +177,7 @@ <option name="lenBefore" value="80" /> <option name="lenAfter" value="80" /> <option name="box" value="false" /> - <option name="filler" value=" " /> + <option name="filler" value="$TEMPLATE$" /> </value> </option> <option name="notice" value="Copyright (c) &#36;today.year, Your Corporation. All Rights Reserved." /> @@ -189,7 +198,7 @@ <option name="lenBefore" value="80" /> <option name="lenAfter" value="80" /> <option name="box" value="false" /> - <option name="filler" value=" " /> + <option name="filler" value="$TEMPLATE$" /> </value> </option> <option name="notice" value="Copyright (c) &#36;today.year, Your Corporation. All Rights Reserved." /> @@ -200,6 +209,27 @@ <option name="fileLocation" value="1" /> <option name="useAlternate" value="false" /> </LanguageOptions> + <LanguageOptions name="__TEMPLATE__"> + <option name="templateOptions"> + <value> + <option name="block" value="true" /> + <option name="separateBefore" value="false" /> + <option name="separateAfter" value="false" /> + <option name="prefixLines" value="true" /> + <option name="lenBefore" value="80" /> + <option name="lenAfter" value="80" /> + <option name="box" value="false" /> + <option name="filler" value="$TEMPLATE$" /> + </value> + </option> + <option name="notice" value="Copyright (c) &#36;today.year, Your Corporation. All Rights Reserved." /> + <option name="keyword" value="Copyright" /> + <option name="fileTypeOverride" value="4" /> + <option name="relativeBefore" value="true" /> + <option name="addBlankAfter" value="true" /> + <option name="fileLocation" value="1" /> + <option name="useAlternate" value="false" /> + </LanguageOptions> </component> </module> Modified: libs/argparser/trunk/src/doc/guide/start.xhtml =================================================================== --- libs/argparser/trunk/src/doc/guide/start.xhtml 2009-07-25 14:24:07 UTC (rev 1372) +++ libs/argparser/trunk/src/doc/guide/start.xhtml 2009-07-25 14:26:45 UTC (rev 1373) @@ -50,18 +50,20 @@ You create a class that implements the Command interface. Usually you won't implement that interface directly but extend one of the classes like BasicCommand. </p> - <pre>package mycommand; + <pre class="java listing">package mycommand; +import net.sf.japi.io.args.ArgParser; import net.sf.japi.io.args.BasicCommand; -public class MyCommand extends BasicCommand { +public class HelloCommand extends BasicCommand { public static void main(final String... args) { - ArgParser.simpleParseAndRun(new MyCommand(), args); + // Delegate command line processing to ArgParser. + ArgParser.simpleParseAndRun(new HelloCommand(), args); } public void run(final List<String> args) { - // Argument processing (not option processing) + System.out.println("Hello, world!"); } }</pre> <p> @@ -78,31 +80,98 @@ <p> Just invoke your program with --help to see this. </p> + <h2>Processing the remaining arguments</h2> + <p> + The <code>List<String></code> passed to the <code>run()</code> method contains all remaining non-option command line arguments. + Looping over that for further processing is simple: + </p> + <pre>package mycommand; + +import net.sf.japi.io.args.ArgParser; +import net.sf.japi.io.args.BasicCommand; + +public class GreetCommand extends BasicCommand { + + public static void main(final String... args) { + ArgParser.simpleParseAndRun(new GreetCommand(), args); + } + + public void run(final List<String> args) { + if (args.size() == 0) { + System.out.println("Hello, world!"); + } else { + for (final String arg : args) { + System.out.format("Hello, %s!%n", arg); + } + } + } +}</pre> <h2>Adding your own Options</h2> <p> Imagine your program can optionally behave recursively. - You want the option to be <code>-r</code> and <code>--recursive</code>. + You want the option to be <code>-r</code> in short form and <code>--recursive</code> in long form. </p> <pre>package mycommand; +import net.sf.japi.io.args.ArgParser; import net.sf.japi.io.args.BasicCommand; -public class MyCommand2 extends BasicCommand { +public class RecursiveCommand extends BasicCommand { private boolean recursive; public static void main(final String... args) { - ArgParser.simpleParseAndRun(new MyCommand(), args); + ArgParser.simpleParseAndRun(new RecursiveCommand(), args); } + // This declares an optional option. + // The option has one short name: r. + // The option has one long name: recursive. + // It can be triggered as -r, -recursive, --recursive, -W recursive. @Option({"r", "recursive"}) public void setRecursive() { recursive = true; } public void run(final List<String> args) { - // Argument processing (not option processing) + if (recursive) { + // ... + } else { + // ... + } } }</pre> + <h2>Adding Options with arguments</h2> + <p> + Options also can have arguments. + For this, the method for the option simply has to declare a parameter. + Imagine, your command supports an option named "color" which takes a String as an argument. + </p> + <pre>package mycommand; + +import net.sf.japi.io.args.ArgParser; +import net.sf.japi.io.args.BasicCommand; + +public class ColorCommand extends MyCommand { + + private String color; + + public static void main(final String... args) { + ArgParser.simpleParseAndRun(new GreetCommand(), args); + } + + @Option({"c", "color"}) + public void setColor(final String color) { + this.color = color; + } + + public void run(final List<String> args) { + // ... + } +}</pre> + <p> + The argument of the option method can be of any type for which ArgParser knows a conversion. + See the documentation of the class ConverterRegistry for a documentation of default supported types. + </p> </body> </html> Modified: libs/argparser/trunk/src/doc/start.xhtml =================================================================== --- libs/argparser/trunk/src/doc/start.xhtml 2009-07-25 14:24:07 UTC (rev 1372) +++ libs/argparser/trunk/src/doc/start.xhtml 2009-07-25 14:26:45 UTC (rev 1373) @@ -27,7 +27,7 @@ <body> <h1>JAPI Lib Argparser</h1> <p> - <em>JAPI Lib Argparser</em> is a library for parsing command line arguments. + <em>JAPI Lib Argparser</em> is subproject of JAPI which features a library for parsing command line arguments. </p> <h2>Supported features</h2> <p> @@ -52,7 +52,7 @@ <li> Direct Audience: Developers <ul> - <li>Makes command line argument parsing easy.</li> + <li>Makes Java command line argument parsing easy.</li> <li>Keeps you from reinventing the wheel, gets the job done for you.</li> </ul> </li> Modified: libs/argparser/trunk/src/prj/net/sf/japi/io/args/OptionType.java =================================================================== --- libs/argparser/trunk/src/prj/net/sf/japi/io/args/OptionType.java 2009-07-25 14:24:07 UTC (rev 1372) +++ libs/argparser/trunk/src/prj/net/sf/japi/io/args/OptionType.java 2009-07-25 14:26:45 UTC (rev 1373) @@ -25,12 +25,31 @@ /** * The type of an {@link Option}. + * It controls the behaviour of an {@link Option} when {@link ArgParser} parses a {@link Command}. + * <h4>Usage example</h4> + * <pre> + * public class MyCommand extends {@link BasicCommand} { + * public static void main(final String... args) { + * {@link ArgParser}.{@link ArgParser#simpleParseAndRun(Command, String[]) simpleParseAndRun}(new MyCommand(), args); + * } + * /** {@inheritDoc} */ + * @Option + * public void run(final List<String> args) { + * } + * } + * </pre> * @author <a href="mailto:ch...@ri...">Christian Hujer</a> * @since 0.2 */ public enum OptionType { /** + * Optional options may be omitted. + * This is the default and usually doesn't need to be changed. + */ + OPTIONAL, + + /** * Required options must always be specified prior to command execution. * Please use required options sparingly - if possible none at all. * Instead provide your program with reasonable defaults. @@ -38,12 +57,6 @@ REQUIRED, /** - * Optional options may be omitted. - * This is the default and usually doesn't need to be changed. - */ - OPTIONAL, - - /** * Terminal options terminate argument parsing, no matter what happens. * This is only for special options like --help. * Normally there should be no need for you to declare your own terminal options. Modified: libs/argparser/trunk/src/prj/net/sf/japi/io/args/converter/ConverterRegistry.java =================================================================== --- libs/argparser/trunk/src/prj/net/sf/japi/io/args/converter/ConverterRegistry.java 2009-07-25 14:24:07 UTC (rev 1372) +++ libs/argparser/trunk/src/prj/net/sf/japi/io/args/converter/ConverterRegistry.java 2009-07-25 14:26:45 UTC (rev 1373) @@ -64,6 +64,7 @@ private static ConverterRegistry createSingletonInstance() { final ConverterRegistry instance = new ConverterRegistry(); instance.registerDefaultConverters(); + // Add more from META-INF/services/net.sf.japi.io.args.converter.Converter for (final Iterator<Converter> converters = ServiceLoader.load(Converter.class).iterator(); converters.hasNext();) { instance.register(converters.next()); } @@ -72,7 +73,7 @@ /** Registers the default converters with this ConverterRegistry. */ public void registerDefaultConverters() { - register(new StringConverter()); + // To see what's really there, also look at META-INF/services/net.sf.japi.io.args.converter.Converter register(new BooleanConverter(boolean.class)); register(new BooleanConverter(Boolean.class)); register(new CharConverter(char.class)); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |