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!
===Processing command line options is easy, so why do I need a helper?=== To illustrate the reason being, let's develop a command line application to show the typical problems and how <code>CommandLineOptions</code> can help you: <pre> package com.acme.clo_demo; public class Diff { public static void main(String[] args) { boolean brief = false; boolean reportIdenticalFiles = false; int context = 3; int idx = 0; while (idx < args.length) { String arg = args[idx]; if (!arg.startsWith("-")) break; idx++; if ("-q".equals(arg) || "--brief".equals(arg)) { brief = true; } else if ("-s".equals(arg) || "--report-identical-files".equals(arg)) { reportIdenticalFiles = true; } else if ("-C".equals(arg) || "--Context".equals(arg)) { context = Integer.parseInt(args[idx++]); } else { System.err.println("Unrecognized command line option \"" + arg + "\""); } } // Now process args[idx]... and do the "real work". } } </pre> This implementation supports both "short" and "long" options, and detects invalid options. However there is room for improvement: * You cannot have "compact" short options, e.g. "<tt>-qs</tt>" for "<tt>-q -s</tt>". * You get an <code>ArrayIndexOutOfBoundsException</code> when "<tt>-C</tt>" is not followed by an argument. * You get a <code>NumberFormatException</code> when the argument of "<tt>-C</tt>" cannot be converted to an integer. * If the first argument after the options starts with a hyphen, it is identified as an invalid option. * The code is not documented. * The only support the user gets when it uses the tool is the "Unsupported command line option..." error message. So here's the second version that overcomes these deficits: <pre> package com.acme.clo_demo; /** * A (rudimentary) re-implementation of the well-known DIFF command line utility. */ public class Diff { /** * Parses the command line options and arguments, then prints the differences between two files or two * directories. * <p> * Supports the following command line options: * </p> * <dl> * <dt>--help</dt> * <dd>Print usage help and exit</dd> * <dt>-q, --brief</dt> * <dd>Don't report the differences, only which files have changed</dd> * <dt>-s, --report-identical-files</dt> * <dd>Also report about files that did NOT change</dd> * <dt>-C <var>NUM</var>, --context <var>NUM</var></dt> * <dd>Report differences in "context diff" format, with <var>NUM</var> lines of context</dd> * </dl> */ public static void main(String[] args) { boolean brief = false; boolean reportIdenticalFiles = false; int context = 3; int idx = 0; while (idx < args.length) { String arg = args[idx]; if (!arg.startsWith("-")) break; idx++; if ("--".equals(arg)) { break; } else if ("--help".equals(arg)) { System.out.println("Prints the differences between files."); System.out.println(); System.out.println("Usage:"); System.out.println(" diff [ <option> ] ... <file1> <file2>"); System.out.println(" Prints the differences between <file1> and <file2>."); System.out.println(" diff [ <option> ] ... <dir1> <dir2>"); System.out.println(" Prints the differences between the files under <dir1> and <dir2>."); System.out.println(); System.out.println("Supports the following command line options:"); System.out.println(" --help</dt>"); System.out.println(" Print usage help and exit"); System.out.println(" -q, --brief"); System.out.println(" Don't report the differences, only which files have changed"); System.out.println(" -s, --report-identical-files"); System.out.println(" Also report about files that did NOT change"); System.out.println(" -C <NUM>, --context <NUM>"); System.out.println(" Report differences in \"context diff\" format, with <NUM> lines of"); System.out.println(" context"); System.exit(0); } else if ("-q".equals(arg) || "--brief".equals(arg)) { brief = true; } else if ("-s".equals(arg) || "--report-identical-files".equals(arg)) { reportIdenticalFiles = true; } else if ("-C".equals(arg) || "--Context".equals(arg)) { if (idx == args.length) { System.err.println("Count missing after \"-C\" or \"--context\"."); System.exit(1); } try { context = Integer.parseInt(args[idx++]); } catch (NumberFormatException nfe) { System.err.println("Cannot convert \"" + args[idx - 1] + "\" to an integer"); System.exit(1); } } else { System.err.println("Unrecognized command line option \"" + arg + "\"; try \"--help\"."); } } // Now process args[idx]... and do the "real work". } } </pre> Well, that's a lot of boilerplate code! And it's just this trivial example... Also, people may want to read about the tool without installing or running it, so you fire up the word processor, or the HTML editor, or the Wiki... But after all, you have written and documented a professional command line tool, congratulations! But wait, now there is number of ''new'' problems: * The code for processing the command line options is ''huge'', and most of it deals with trivial error conditions. * There is a lot of redundancy in the command line options: There's the CLO ''implementation'', and the ''description'' appears in three places: The JAVADOC of the <code>main()</code> method, the <code>System.out.println()</code> for the "<tt>--help</tt>" option, and in the online/paper documentation. What a maintenance nightmare!
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