From: <chr...@us...> - 2006-05-27 22:20:48
|
Revision: 121 Author: christianhujer Date: 2006-05-27 15:20:42 -0700 (Sat, 27 May 2006) ViewCVS: http://svn.sourceforge.net/japi/?rev=121&view=rev Log Message: ----------- Improved Grep example. Modified Paths: -------------- trunk/src/doc/guide/io/src/GrepJAPI.java Modified: trunk/src/doc/guide/io/src/GrepJAPI.java =================================================================== --- trunk/src/doc/guide/io/src/GrepJAPI.java 2006-05-27 22:20:16 UTC (rev 120) +++ trunk/src/doc/guide/io/src/GrepJAPI.java 2006-05-27 22:20:42 UTC (rev 121) @@ -12,34 +12,51 @@ */ public class GrepJAPI implements Command { + /** Whether to display the total line number. */ + private boolean totalLine; + /** Main program. * @param args command line arguments */ public static void main(final String... args) { - final GrepJAPI grepJAPI = new GrepJAPI(); - ArgParser.parse(grepJAPI, args); + ArgParser.parse(new GrepJAPI(), args); } /** {@inheritDoc} */ @SuppressWarnings({"InstanceMethodNamingConvention"}) public void run(final List<String> args) { + int totalLines = 0; final String regex = args.remove(0); final Pattern pattern = Pattern.compile(regex); final Matcher matcher = pattern.matcher(""); for (final String line : new ARGV(args.toArray(new String[args.size()]))) { + totalLines++; if (matcher.reset(line).find()) { - System.out.println(line); + if (totalLine) { + System.out.format("%d:%s%n", totalLines, line); + } else { + System.out.format("%s%n", line); + } } } } + /** Print help and quit. */ @StopOption @Option({"h", "help"}) public void help() { - System.out.println("help"); + System.out.println("Usage: java GrepJAPI regex [file...]"); } + /** Print version and quit. */ @StopOption @Option({"V", "version"}) public void version() { - System.out.println("The version."); + System.out.println("GrepJAPI V 0.1"); } + + /** Set displaying the total line number. */ + @Option({"l", "total-line"}) + public void totalLine() { + totalLine = true; + } + } // class GrepJAPI This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |