com.jkristian.cli
Class UnixCommand

java.lang.Object
  extended by com.jkristian.cli.UnixCommand

public class UnixCommand
extends java.lang.Object

A utility program in the Unix filter style. Command line parameters are options (each beginning with '-') and the names of files that the program reads. When there are no file name parameters or a parameter '-', the program reads from System.in and writes to System.out.

This class doesn't actually do anything; it merely provides generic 'plumbing' to parse the command line and access files.


Nested Class Summary
protected static class UnixCommand.SteerableInputStream
           
protected static class UnixCommand.SteerableOutputStream
           
 
Field Summary
protected  java.lang.String backupFileSuffix
           
protected  java.lang.String charset
           
protected  java.lang.Class command
           
protected  java.io.Writer errorWriter
           
protected  java.util.List files
           
protected  LineReader inputLineReader
           
protected  UnixCommand.SteerableInputStream inputStream
           
protected  UnixCommand.SteerableOutputStream outputStream
           
protected  java.io.Writer outputWriter
           
static java.lang.String POSIXLY_CORRECT
           
static java.io.File STDIN
          The name of the standard input stream (as a command line parameter).
 
Constructor Summary
UnixCommand(java.lang.Class command, java.lang.String options, java.lang.String[] argv)
           
 
Method Summary
 byte[] encode(java.lang.String s)
           
 void exit(int status)
           
 void flush()
           
 java.lang.Class getCommand()
           
 java.io.Writer getErrorWriter()
           
 java.lang.StringBuffer getHelp()
           
 LineReader getInputLineReader()
           
 java.io.PushbackReader getInputReader()
           
 java.io.OutputStream getLineOutputStream()
           
 java.util.Locale getLocale()
           
protected  java.lang.String getMessage(java.lang.String name)
           
 java.lang.String getOption(java.lang.String name)
           
 java.util.Properties getOptions()
           
 java.io.Writer getOutputWriter()
           
 java.lang.String getResource(java.lang.String name)
           
 java.util.ResourceBundle getResourceBundle()
           
protected  java.util.ResourceBundle getResourceBundle(java.lang.Class base)
           
 java.lang.String getUsage()
           
static org.apache.commons.cli.CommandLine GnuParserGetopt(java.lang.String[] argv, java.lang.String options)
          Parse options in a style similar to GNU getopt() .
 boolean hasNext()
           
 boolean hasOption(java.lang.String name)
           
protected  java.io.OutputStream lineSeparator(java.io.OutputStream raw)
           
 java.io.File next()
           
protected static void replaceAll(java.lang.StringBuffer s, java.lang.String find, java.lang.String replace)
           
 void setCharset(java.lang.String charset)
           
 void setEditInPlace(java.lang.String suffix)
           
 void setLocale(java.util.Locale locale)
           
 void writeError(java.lang.Object o)
           
 void writeError(java.lang.String s)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

STDIN

public static final java.io.File STDIN
The name of the standard input stream (as a command line parameter).


command

protected java.lang.Class command

charset

protected java.lang.String charset

backupFileSuffix

protected java.lang.String backupFileSuffix

files

protected java.util.List files

inputStream

protected final UnixCommand.SteerableInputStream inputStream

inputLineReader

protected LineReader inputLineReader

outputStream

protected final UnixCommand.SteerableOutputStream outputStream

outputWriter

protected java.io.Writer outputWriter

errorWriter

protected java.io.Writer errorWriter

POSIXLY_CORRECT

public static final java.lang.String POSIXLY_CORRECT
See Also:
Constant Field Values
Constructor Detail

UnixCommand

public UnixCommand(java.lang.Class command,
                   java.lang.String options,
                   java.lang.String[] argv)
Parameters:
command - the class that contains the main method of this command
options - describes the supported options as to GNU getopt(); for example "e:hv"
Method Detail

GnuParserGetopt

public static org.apache.commons.cli.CommandLine GnuParserGetopt(java.lang.String[] argv,
                                                                 java.lang.String options)
                                                          throws org.apache.commons.cli.ParseException
Parse options in a style similar to GNU getopt() . Stop parsing options at the first non-option if options begins with '+' or the System property "gnu.posixly_correct" is set; otherwise parse options anywhere in argv.

An option's argument may appear in argv immediately after the option letter ("-oarg") or may be separated from the option letter ("-o arg"). In particular "-o arg" means the option 'o' with the argument "arg", regardless of whether 'o' requires an argument or takes an optional argument (which is different from the behavior of GNU getopt). An option that takes an optional argument may be followed by another option; for example "-o -p" means the option 'o' with no argument and the option 'p', if 'o' takes an optional argument.

Parameters:
argv - command line parameters (not including this program's own name)
options - contains all valid option characters. In this string, an option character may be followed by a colon ":" to indicate that it requires an argument, or two colons "::" to indicate that it takes an optional argument (the latter is a GNU extension).
Throws:
org.apache.commons.cli.ParseException

getCommand

public java.lang.Class getCommand()

getOptions

public java.util.Properties getOptions()

getOption

public java.lang.String getOption(java.lang.String name)

hasOption

public boolean hasOption(java.lang.String name)

getLocale

public java.util.Locale getLocale()

setLocale

public void setLocale(java.util.Locale locale)

getResourceBundle

public java.util.ResourceBundle getResourceBundle()

getResource

public java.lang.String getResource(java.lang.String name)

getUsage

public java.lang.String getUsage()

getHelp

public java.lang.StringBuffer getHelp()

setCharset

public void setCharset(java.lang.String charset)
                throws java.io.UnsupportedEncodingException
Throws:
java.io.UnsupportedEncodingException

encode

public byte[] encode(java.lang.String s)

setEditInPlace

public void setEditInPlace(java.lang.String suffix)
Parameters:
suffix - The string that is appended to an input file name, to get the 'backup' file name; or null to disable editing in place.

hasNext

public boolean hasNext()

next

public java.io.File next()
                  throws java.io.IOException
Throws:
java.io.IOException

flush

public void flush()
           throws java.io.IOException
Throws:
java.io.IOException

getInputReader

public java.io.PushbackReader getInputReader()
                                      throws java.io.IOException
Throws:
java.io.IOException

getInputLineReader

public LineReader getInputLineReader()
                              throws java.io.IOException
Throws:
java.io.IOException

getLineOutputStream

public java.io.OutputStream getLineOutputStream()

getOutputWriter

public java.io.Writer getOutputWriter()
                               throws java.io.IOException
Throws:
java.io.IOException

getErrorWriter

public java.io.Writer getErrorWriter()
                              throws java.io.IOException
Throws:
java.io.IOException

writeError

public void writeError(java.lang.String s)
                throws java.io.IOException
Throws:
java.io.IOException

writeError

public void writeError(java.lang.Object o)
                throws java.io.IOException
Throws:
java.io.IOException

exit

public void exit(int status)

getMessage

protected java.lang.String getMessage(java.lang.String name)

getResourceBundle

protected java.util.ResourceBundle getResourceBundle(java.lang.Class base)

replaceAll

protected static void replaceAll(java.lang.StringBuffer s,
                                 java.lang.String find,
                                 java.lang.String replace)

lineSeparator

protected java.io.OutputStream lineSeparator(java.io.OutputStream raw)