UNIX getopt() system call : UNIX Win32 « Development Class « Java






UNIX getopt() system call

   

import java.util.HashMap;

/**
 * Getopts is similar to the UN*X getopt() system call. It parses an array of
 * Strings (usually the command line), looking for specified option flags and
 * values.
 * <p>
 * An instance of Getopts parses the whole args list at once, and stores the
 * option flags and values that it finds.
 *
 * @author Jim Menard,
 * <a href="mailto:jimm@io.com">jimm@io.com</a>
 */
public class Getopts {
String[] argv;
HashMap options = new HashMap();
boolean errorFlag = false;

/**
 * This constructor takes a list of legal options and a list of (usually
 * command line) arguments. Each option in optionListString may be followed
 * by a ':' to signify that option takes an argument.
 *
 * @param optionListString option chars with optional ':' specifying arg.
 * For example, "ab:c" specifies three options, a, b, and c. Option b takes
 * a (required) argument.
 * @param args array of command line arguments
 */
public Getopts(String optionListString, String[] args) {
    String optChoices = optionListString;

    for (int index = 0; index < args.length; ++index) {
  String arg = args[index];
  if (arg.startsWith("-")) {
      char optionChar = arg.charAt(1);
      int optionLoc = optChoices.indexOf(optionChar);
      if (optionLoc == -1)
    errorFlag = true;
      else {
    // Look for argument, if any
    boolean hasArgument =
        optChoices.length() > optionLoc + 1 &&
        optChoices.charAt(optionLoc + 1) == ':';
    if (hasArgument) {
        String optarg = arg.substring(2);
        if (optarg.equals("")) {
      ++index;
      try {
          optarg = args[index];
      }
      catch (Exception e) { // Catch ArrayOutOfBounds
          optarg = "";
          errorFlag = true;
      }
        }
        options.put(new Character(optionChar), optarg);
    }
    else {
        // No arg, store empty string
        options.put(new Character(optionChar), "");
    }
      }
  }
  else {      // End of options. Store rest of args
      argv = new String[args.length - index];
      int offset = index;
      while (index < args.length) {
    argv[index - offset] = args[index];
    ++index;
      }
      break;
  }
    }
}

/**
 * 
 * Return true if there was an error while parsing the command line.
 */
public boolean error() {
    return errorFlag;
}

/**
 * Returns existence of an option.
 *
 * @return true of option 'c' exists, else return false.
 * @param c any character
 */
public boolean hasOption(char c) {
    if (options == null)
  return false;
    return options.containsKey(new Character(c));
}

/**
 * Return an option or, if missing, the empty string.
 *
 * @return option string, or "" if error or option has no argument
 * @param c the option whose value is returned
 */
public String option(char c) {
    return option(c, "");
}

/**
 * Return an option or, if missing, a default value.
 *
 * @return option string, or defaultValue if error or option has no argument
 * @param c the option whose value is returned
 * @param defaultValue the value to return if there is no such option
 */
public String option(char c, String defaultValue) {
    if (options == null)
  return defaultValue;

    String s;
    try {
  Object o = options.get(new Character(c));
  if (o == null || !(o instanceof String))
      s = defaultValue;
  else
      s = (String)o;
    }
    catch (Exception e) {
  s = defaultValue;
    }
    return s;
}

/**
 * Return the remaining command-line arguments.
 *
 * @return an array of Strings
 * @see #argc
 * @see #argv
 */
public String[] args() {
    return argv;
}

/**
 * Return the number of non-option args.
 */
public int argc() {
    if (argv == null)
  return 0;
    return argv.length;
}
/**
 * Return a command line argument or "" if <var>argv</var> is
 * <code>null</code>. Index starts at 0.
 *
 * @param index which argument to return
 * @return the index'th arg or "" if <var>argv</var> is <code>null</code>
 */
public String argv(int index) {
    if (argv == null)
  return "";

    return argv[index];
}
}

   
    
    
  








Related examples in the same category

1.Java 1.5 (5.0) Changes to the API: ProcessBuilder.
2.How to execute a program from within Java
3.How to execute an external program How to execute an external program
4.Show how to use exec to pass complex args
5.ExecDemo shows how to execute an external program 2
6.ExecDemo shows how to execute an external program
7.Execute an external program read its output, and print its exit status
8.Create some temp files, ls them, and rm them
9.ExecDemoHelp shows how to use the Win32 start command
10.ExecDemo shows how to execute an external program and read its output
11.ExecDemo shows how to execute an external program and read its output 3
12.Unix Crypt
13.Handles program arguments like Unix getopt()
14.Helper method to execute shell command
15.dealing with Excel dates