Editing
CommandLineOptions tutorial
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==So here's the code== <pre> package com.acme.clo_demo; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.regex.Pattern; import de.unkrig.commons.text.pattern.PatternUtil; import de.unkrig.commons.util.CommandLineOptions; import de.unkrig.commons.util.annotation.CommandLineOption; /** * A (rudimentary) re-implementation of the well-known DIFF command line utility. */ public class Diff { /** * Prints the differences between files. * <h2>Usage:</h2> * <dl> * <dt>diff [ <var>option</var> ] ... <var>file1</var> <var>file2</var></dt> * <dd>Prints the differences between <var>file1</var> and <var>file2</var>.</dd> * <dt>diff [ <var>option</var> ] ... <var>dir1</var> <var>dir2</var></dt> * <dd>Prints the differences between the files under <var>dir1</var> and <var>dir2</var>.</dd> * </dl> * <h2>Options:</h2> * {@command-line-options} */ public static void main(String[] args) { Diff diff = new Diff(); args = CommandLineOptions.parse(args, diff); // Now process args[idx]... and do the "real work". } private boolean brief = false; private boolean reportIdenticalFiles = false; private int context = 3; /** * Print usage help and exit. */ @CommandLineOption public void help() throws IOException { CommandLineOptions.printResource(Diff.class, "main(String[]).txt", null, System.out); System.exit(0); } /** * Don't report the differences, only which files have changed. */ @CommandLineOption(name = { "-q", "--brief" }) public void setBrief() { this.brief = true; } /** * Also report about files that did NOT change. */ @CommandLineOption(name = { "-s", "--report-identical-files"}) public void setReportIdenticalFiles() { this.reportIdenticalFiles = true; } /** * Report differences in "context diff" format, with <var>number-of-lines</var> lines of context. */ @CommandLineOption(name = { "-C", "--Context" }) public void setContext(int numberOfLines) { this.context = numberOfLines; } } </pre> When you run it with the "<tt>--help</tt>" option, you get: Prints the differences between files. Usage: ====== diff [ <option> ] ... <file1> <file2> Prints the differences between <file1> and <file2>. diff [ <option> ] ... <dir1> <dir2> Prints the differences between the files under <dir1> and <dir2>. Options: ======== --help Print usage help and exit. -q --brief Don't report the differences, only which files have changed. -s --report-identical-files Also report about files that did NOT change. -C <number-of-lines> --context <number-of-lines> Report differences in "context diff" format, with <number-of-lines> lines of context. And voila - you have zero redundancy, and HTML documentation for printing and/or online presentation for free!
Summary:
Please note that all contributions to unkrig.de may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Unkrig.de:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help
Tools
What links here
Related changes
Special pages
Page information