Source of CallArgsProgram.java


  2: import java.util.Scanner;
  3: import java.util.Arrays;

  5: /**
  6:  * A program to call PrintArgs and AddArgs with command line arguments. It 
  7:  * basically simulates a command-line interface.
  8:  *
  9:  * @author Mark Young (A00000000)
 10:  */
 11: public class CallArgsProgram {

 13:     public static void main(String[] args) {
 14:         // create variables
 15:         Scanner kbd = new Scanner(System.in);
 16:         String line;
 17:         String[] words;

 19:         // describe yourself
 20:         System.out.println("\n\n"
 21:                 + "Call a Program with Command Line Arguments\n"
 22:                 + "------------------------------------------\n\n"
 23:                 + "At each prompt below, "
 24:                 + "enter a command in the form:\n\n"
 25:                 + "\tjava [program name] [command line arguments...]\n\n"
 26:                 + "and I will call that program "
 27:                 + "using those command line arguments.\n\n"
 28:                 + "Enter a blank line at the prompt when you're done.\n");
 29:         
 30:         // start user interaction loop
 31:         System.out.print("prompt] ");
 32:         line = kbd.nextLine().trim();   // delete spaces at front/back of input
 33:         while (!"".equals(line)) {
 34:             // split the input line into words
 35:             words = line.split("\\s+");

 37:             // process command
 38:             if (!"java".equals(words[0])) {
 39:                 // command must be java
 40:                 System.out.println("Sorry, I don't know the command '" 
 41:                         + words[0] +"'");
 42:             } else if (words.length < 2) {
 43:                 // command needs at least one argument (the program to run)
 44:                 System.out.println("You need to give a program name!");
 45:             } else if (words[1].equals("PrintArgs")) {
 46:                 // PrintArgs is a valid command
 47:                 PrintArgs.main(Arrays.copyOfRange(words, 2, words.length));
 48:             } else if (words[1].equals("AddArgs")) {
 49:                 // AddArgs is a valid command
 50:                 AddArgs.main(Arrays.copyOfRange(words, 2, words.length));
 51:             } else {
 52:                 // Other commands are not valid
 53:                 System.out.println("I'm sorry, I don't know the '"
 54:                         + words[1] + "' program");
 55:             }

 57:             // prompt for and read next command
 58:             System.out.print("prompt] ");
 59:             line = kbd.nextLine().trim();
 60:         }
 61:     }

 63: }