[Japi-cvs] SF.net SVN: japi: [195] libs/argparser/trunk/src
Status: Beta
Brought to you by:
christianhujer
From: <chr...@us...> - 2006-11-06 22:30:01
|
Revision: 195 http://svn.sourceforge.net/japi/?rev=195&view=rev Author: christianhujer Date: 2006-11-06 14:29:50 -0800 (Mon, 06 Nov 2006) Log Message: ----------- Added some examples. Added Paths: ----------- libs/argparser/trunk/src/doc/ libs/argparser/trunk/src/doc/examples/ libs/argparser/trunk/src/doc/examples/Cat.java libs/argparser/trunk/src/doc/examples/Head.java libs/argparser/trunk/src/doc/examples/Tail.java Added: libs/argparser/trunk/src/doc/examples/Cat.java =================================================================== --- libs/argparser/trunk/src/doc/examples/Cat.java (rev 0) +++ libs/argparser/trunk/src/doc/examples/Cat.java 2006-11-06 22:29:50 UTC (rev 195) @@ -0,0 +1,67 @@ +package examples; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.List; +import net.sf.japi.io.args.ArgParser; +import net.sf.japi.io.args.BasicCommand; +import net.sf.japi.io.args.MissingArgumentException; +import net.sf.japi.io.args.RequiredOptionsMissingException; +import net.sf.japi.io.args.TerminalException; +import net.sf.japi.io.args.UnknownOptionException; +import org.jetbrains.annotations.NotNull; + +/** + * Java implementation of the UNIX command <q>cat</q> to demonstrate how to use the argparser library. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + */ +public class Cat extends BasicCommand { + + /** {@inheritDoc} */ + @SuppressWarnings({"InstanceMethodNamingConvention"}) + public int run(@NotNull final List<String> args) throws Exception { + int returnCode = 0; + if (args.size() == 0) { + copy(System.in, System.out); + } else { + for (final String arg : args) { + try { + final InputStream in = new FileInputStream(arg); + try { + copy(in, System.out); + } finally { + in.close(); + } + } catch (final IOException e) { + returnCode = 1; + System.err.println(e); + } + } + } + return returnCode; + } + + /** + * Copies data from one input stream to another. + * @param in InputStream to read from. + * @param out InputStream to write to. + * @throws IOException in case of I/O problems. + */ + private void copy(final InputStream in, final OutputStream out) throws IOException { + final byte[] buf = new byte[4096]; + for (int bytesRead; (bytesRead = in.read(buf)) != -1;) { + out.write(buf, 0, bytesRead); + } + } + + /** + * Main method. + * @param args Command line arguments + */ + public static void main(final String... args) throws RequiredOptionsMissingException, MissingArgumentException, TerminalException, UnknownOptionException { + ArgParser.parseAndRun(new Head(), args); + } + +} // class Cat Property changes on: libs/argparser/trunk/src/doc/examples/Cat.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF Added: libs/argparser/trunk/src/doc/examples/Head.java =================================================================== --- libs/argparser/trunk/src/doc/examples/Head.java (rev 0) +++ libs/argparser/trunk/src/doc/examples/Head.java 2006-11-06 22:29:50 UTC (rev 195) @@ -0,0 +1,135 @@ +package examples; + +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.List; +import net.sf.japi.io.args.ArgParser; +import net.sf.japi.io.args.BasicCommand; +import net.sf.japi.io.args.MissingArgumentException; +import net.sf.japi.io.args.Option; +import net.sf.japi.io.args.RequiredOptionsMissingException; +import net.sf.japi.io.args.TerminalException; +import net.sf.japi.io.args.UnknownOptionException; +import org.jetbrains.annotations.NotNull; + +/** + * Java implementation of the UNIX command <q>head</q> to demonstrate how to use the argparser library. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + */ +public class Head extends BasicCommand { + + /** The number of items to print. */ + private int numItems = 10; + + /** + * The kind of items to print. + * <code>false</code>: print lines. + * <code>true</code>: print bytes. + */ + private boolean printBytes; + + /** + * Quiety / Verbosity. + * 0 = never print filenames. + * 1 = print filenames if more than one file. + * 2 = always print filenames. + */ + private int verbose = 1; + + /** + * Sets the number of lines to print. + * @param lines number of lines to print + */ + @Option(names = {"n", "lines"}) + public void setLines(final String lines) { + numItems = Integer.parseInt(lines); + printBytes = false; + } + + /** + * Sets the number of bytes to print. + * @param bytes number of bytes to print. + */ + @Option(names = {"c", "bytes"}) + public void setBytes(final String bytes) { + numItems = Integer.parseInt(bytes); + printBytes = true; + } + + /** + * Sets the command to be quiet. + */ + @Option(names = {"q", "quiet", "silent"}) + public void setQuiet() { + verbose = 0; + } + + /** + * Sets the command to be verbose. + */ + @Option(names = {"v", "verbose"}) + public void setVerbose() { + verbose = 2; + } + + /** + * {@inheritDoc} + */ + public int run(@NotNull final List<String> args) throws IOException { + int returnCode = 0; + if (args.size() == 0) { + if (verbose == 2) { + System.out.println("==> STDIN <=="); + } + copyItems(System.in); + } else { + for (final String arg : args) { + if (verbose == 2 || verbose == 1 && args.size() > 1) { + System.out.println("==> " + arg + " <=="); + } + try { + final InputStream in = new FileInputStream(arg); + try { + copyItems(in); + } finally { + in.close(); + } + } catch (final IOException e) { + System.err.println(e); + returnCode = 1; + } + } + } + return returnCode; + } + + /** + * Copies the configured number of items from the specified InputStream to System.out. + * @param in InputStream to run on + */ + private void copyItems(final InputStream in) throws IOException { + if (printBytes) { + for (int i = 0, b; i < numItems && (b = in.read()) != -1; i++) { + System.out.write(b); + } + } else { + BufferedReader lin = new BufferedReader(new InputStreamReader(in)); + String line; + for (int i = 0; i < numItems && (line = lin.readLine()) != null; i++) { + System.out.println(line); + } + } + } + + /** + * Main method. + * @param args Command line arguments + */ + public static void main(final String... args) throws RequiredOptionsMissingException, MissingArgumentException, TerminalException, UnknownOptionException { + ArgParser.parseAndRun(new Head(), args); + } + +} // class Head Property changes on: libs/argparser/trunk/src/doc/examples/Head.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF Added: libs/argparser/trunk/src/doc/examples/Tail.java =================================================================== --- libs/argparser/trunk/src/doc/examples/Tail.java (rev 0) +++ libs/argparser/trunk/src/doc/examples/Tail.java 2006-11-06 22:29:50 UTC (rev 195) @@ -0,0 +1,143 @@ +package examples; + +import java.io.BufferedInputStream; +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.List; +import net.sf.japi.io.args.ArgParser; +import net.sf.japi.io.args.BasicCommand; +import net.sf.japi.io.args.MissingArgumentException; +import net.sf.japi.io.args.Option; +import net.sf.japi.io.args.RequiredOptionsMissingException; +import net.sf.japi.io.args.TerminalException; +import net.sf.japi.io.args.UnknownOptionException; +import org.jetbrains.annotations.NotNull; + +/** + * Java implementation of the UNIX command <q>tail</q> to demonstrate how to use the argparser library. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + */ +public class Tail extends BasicCommand { + + /** The number of items to print. */ + private int numItems = 10; + + /** + * The kind of items to print. + * <code>false</code>: print lines. + * <code>true</code>: print bytes. + */ + private boolean printBytes; + + /** + * Quiety / Verbosity. + * 0 = never print filenames. + * 1 = print filenames if more than one file. + * 2 = always print filenames. + */ + private int verbose = 1; + + /** + * Sets the number of lines to print. + * @param lines number of lines to print + */ + @Option(names = {"n", "lines"}) + public void setLines(final String lines) { + numItems = Integer.parseInt(lines); + printBytes = false; + } + + /** + * Sets the number of bytes to print. + * @param bytes number of bytes to print. + */ + @Option(names = {"c", "bytes"}) + public void setBytes(final String bytes) { + numItems = Integer.parseInt(bytes); + printBytes = true; + } + + /** + * Sets the command to be quiet. + */ + @Option(names = {"q", "quiet", "silent"}) + public void setQuiet() { + verbose = 0; + } + + /** + * Sets the command to be verbose. + */ + @Option(names = {"v", "verbose"}) + public void setVerbose() { + verbose = 2; + } + + /** {@inheritDoc} */ + @SuppressWarnings({"InstanceMethodNamingConvention"}) + public int run(@NotNull List<String> args) throws Exception { + int returnCode = 0; + for (final String arg : args) { + try { + final InputStream in = new BufferedInputStream(new FileInputStream(arg)); + try { + copy(in); + } finally { + in.close(); + } + } catch (final IOException e) { + returnCode = 1; + System.err.println(e); + } + } + return returnCode; + } + + private void copy(final InputStream in) throws IOException { + if (printBytes) { + copyBytes(in); + } else { + copyLines(in); + } + } + private void copyBytes(final InputStream in) throws IOException { + final InputStream lin = in instanceof BufferedInputStream ? in : new BufferedInputStream(in); + final byte[] buf = new byte[numItems]; + int bytesRead; + int loop = 0; + while ((bytesRead = lin.read(buf)) != -1) { + loop++; + } + if (loop >= 2) { + System.out.write(buf, bytesRead, buf.length - bytesRead); + } + System.out.write(buf, 0, bytesRead); + } + + private void copyLines(final InputStream in) throws IOException { + final String[] buf = new String[numItems]; + final BufferedReader lin = new BufferedReader(new InputStreamReader(in)); + int num = 0; + while ((buf[num++ % numItems] = lin.readLine()) != null); + if (num >= numItems) { + for (int i = num % numItems; i < numItems; i++) { + System.out.println(buf[i]); + } + } + for (int i = 0; i < num % numItems; i++) { + System.out.println(buf[i]); + } + } + + /** + * Main method. + * @param args Command line arguments + */ + public static void main(final String... args) throws RequiredOptionsMissingException, MissingArgumentException, TerminalException, UnknownOptionException { + ArgParser.parseAndRun(new Head(), args); + } + +} // class Tail Property changes on: libs/argparser/trunk/src/doc/examples/Tail.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |