com.ptc.windchill.upgrade.tool
Class ArgumentProcessor

java.lang.Object
  extended bycom.ptc.windchill.upgrade.tool.ArgumentProcessor

public final class ArgumentProcessor
extends Object

Loads all the menu options it receives from another *.class file. When a user makes a call with arguments the ArgumentProcessor checks if the argument matches an option that was previously loaded. The option is then checked for its type: Boolean.class, String.class, or Integer.class.

If the option is of type java.lang.Boolean, no argument needs to follow it. If the argument is of type java.lang.String or java.lang.Integer an argument needs to follow the option.

The String option should be followed by a string and an Integer option should be followed by an argument that is a string but can successfully be converted into an integer.


Example Usage

To load an option that does not need an argument, it can be done in two ways:

        ArgumentProcessor ap = new ArgumentProcessor();
 
      First option:   ap.addBooleanOption("help");
      Second option:  ap.addOption("count", Integer.class);
 
To check if the argument provided is an option:
  ArgumentProcessor.ProcessedArguments processed_arguments = ap.process(args);
 
To verify there was a match:
    String[] opt_names = processed_args.getSelectedOptionNames();
 
If the argument list originally looked like {"-h", "-count", "5", "abc"}, then:
    processed_arguments.getArguments() => {"abc"}
    processed_arguments.getSelectedOptionNames() => {"h", "count"} NOTE: Order is not guaranteed
    processed_arguments.isOptionSet("h") => true
    processed_arguments.getOption("count") => 5 as a java.lang.Integer
    processed_arguments.isOptionSet("blue") => false
    processed_arguments.getOption("blue") => null

 


Nested Class Summary
static class ArgumentProcessor.ProcessedArguments
          Inner class that stores the arguments, under the correct category.
 
Field Summary
private  HashMap optionDefinitions_
           
 
Constructor Summary
ArgumentProcessor()
          Create an instance of ArgumentProcessor without using arguments
 
Method Summary
 void addBooleanOption(String option_name)
          Adds an argument that should be treated as a boolean.
 void addIntegerOption(String option_name)
          Adds an argument that should be treated as an integer.
 void addOption(String option_name, Class option_type)
          This method adds the different options to be loaded into the options list.
 void addStringOption(String option_name)
          Adds an argument that should be treated as a string.
private  String getOptionNameFromArg(String arg)
           
private  Class getOptionType(String opt_name)
          Check if the command-line argument has a Class type that matches it.
private  boolean isOption(String arg)
          Checks if an argument starts with a "-" or not.
 ArgumentProcessor.ProcessedArguments process(String[] args)
          Checks if the argument(s) specified are options.
 void removeOption(String option_name)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

optionDefinitions_

private HashMap optionDefinitions_
Constructor Detail

ArgumentProcessor

public ArgumentProcessor()
Create an instance of ArgumentProcessor without using arguments

Method Detail

addBooleanOption

public void addBooleanOption(String option_name)
Adds an argument that should be treated as a boolean. If the option is specifed in the argument list, the option value will be Boolean.TRUE and ProcessedArguments.isOptionSet()will report true. Otherwise, it will be null and ProcessedArguments.isOptionSet() will report false


addIntegerOption

public void addIntegerOption(String option_name)
Adds an argument that should be treated as an integer. If the option is specified, it must be followed by a string that can be successfully be converted to an integer via the java.lang.Integer class. If the option is specified in the argument list, the ProcessedArguments.isOptionSet()will report true. Otherwise, it will be null and ProcessedArguments.isOptionSet() will report false.


addStringOption

public void addStringOption(String option_name)
Adds an argument that should be treated as a string. If the option is specified, it must be followed by a string. If the option is specified in the argument list, the ProcessedArguments.isOptionSet()will report true. Otherwise, it will be null and ProcessedArguments.isOptionSet() will report false.


addOption

public void addOption(String option_name,
                      Class option_type)
This method adds the different options to be loaded into the options list.

Parameters:
option_type - Class
Throws:
IllegalArgumentException - if the Class type does not match Boolean.class, String.class, or Integer.class

removeOption

public void removeOption(String option_name)

process

public ArgumentProcessor.ProcessedArguments process(String[] args)
                                             throws Exception
Checks if the argument(s) specified are options. If there is a match between the option and argument list, the match is classified as an option. If the argument does not match the option list, the argument provided is added to the list of arguments not to be processed. If the option is a Boolean option, it does not need to have an argument following it. If the option is a String or an Intger option, it will check if it is followed by another argument that is acceptable for it to execute. Otherwise, an exception will be thrown.

Throws:
Exception

isOption

private boolean isOption(String arg)
Checks if an argument starts with a "-" or not. If it starts with a dash and the command-line argument matches one that is in the list, it will continue to execute the argument.


getOptionNameFromArg

private String getOptionNameFromArg(String arg)

getOptionType

private Class getOptionType(String opt_name)
Check if the command-line argument has a Class type that matches it.

Returns:
Class or null