Java 根据参数的顺序执行命令

Java 根据参数的顺序执行命令,java,arraylist,Java,Arraylist,我正在用java构建自己的命令行。我实现了所有其他命令。我想尝试根据ex的参数顺序执行命令) 命令:(历史Y-C),命令:(历史C-Y) (历史Y)表示打印(Y)个数的命令的上一个历史编号,(C)表示清除历史命令 所以(history cy)应该首先清除history命令,然后打印之前的命令的Y个数,这些命令将为空 (历史Y C)应打印Y个以前的命令,然后清除历史命令 如何实现基于不同顺序的代码输出结果 我正在使用split将命令拆分为“”(空格) 这就是我现在得到的 if (Split

我正在用java构建自己的命令行。我实现了所有其他命令。我想尝试根据ex的参数顺序执行命令)

命令:(历史Y-C),命令:(历史C-Y)

(历史Y)表示打印(Y)个数的命令的上一个历史编号,(C)表示清除历史命令

所以(history cy)应该首先清除history命令,然后打印之前的命令的Y个数,这些命令将为空

(历史Y C)应打印Y个以前的命令,然后清除历史命令

如何实现基于不同顺序的代码输出结果

我正在使用split将命令拆分为“”(空格)

这就是我现在得到的

    if (Split[0].equals("history")) {

         if (Split[1].equals("Y")) {
    int num = int parseint(command)
}
        if (Split[2].equals("C")) {
        " Then clear command"
}
}
从上面的代码来看,它是历史Y C 如何将其实现为用户选择以不同的顺序放置?

应用程序的main()方法已经从命令行接受命令的字符串数组(这就是args参数的用途),但它的工作方式确实不同,具体取决于命令行参数的提供方式。所有命令行参数都被视为字符串对象的字符串数组,无论这些参数是否包含在引号中,例如:

如果编译后的应用程序名为:MyApp.jar并且在main()方法中,我们有一些简单的代码将提供的命令行参数打印到控制台窗口,如:

public class MyApp {

    public static void main(String[] args) {
        for (int i = 0; i < args.length; i++) {
            System.out.println(args[i]);
        }
    }

}
窗口将显示:

Hi,
my
name
is
Johnny
Walker
Hi, my name is Johnny Walker
Hi, my name is Johnny Walker
What's yours?
Is it Bob?
您可以看到,应用程序命令行中提供的每个项目(由一个(或多个)空格分隔)都被视为单独的命令行参数。main()方法的参数总是用空格分隔,而不是典型的逗号()。但是,如果希望将整个字符串作为单个命令行参数提供给应用程序,则需要将整个字符串括在引号内,如下所示:

java -jar MyApp.jar "Hi, my name is Johnny Walker" "What's yours?" "Is it Bob?"
MyApp Help:

There are four categorical application functions available that can be
accessed via the Command Line (History, Current, Modify, and Delete).
Each function along with their specific parameters must be supplied as
quoted strings. The functions and their respective parameters can be
supplied in any order, for example:

**java -jar MyApp.jar "modify r=12 l=hypo n=43.55" "history c r=12"**

Categorical Application Functions:

Letter case is optional.

------------------------------------------------------------------
HISTORY:

Display Record(s) History.

    C   Clear History.
    R=  Integer - Record Number (0 for all).
------------------------------------------------------------------

CURRENT:

Displays Current Record and Linked Record (if any).
Optionaly displays defined Variable Name.


    R=  Integer - Make Record Current (0 for No Record).
    L=  Integer - Linked To Record Number (0 for No Record).
    N   Optional- Also Display Variable Name.
------------------------------------------------------------------

MODIFY:

Modifies a Record Culture.

    R=  Integer - Record Number to Modify (0 for All).
    L=  Integer - Change Linked Record Number (0 for No 
                Record).
    N=  String  - Change Variable Name ("" for No Name).
------------------------------------------------------------------

DELETE:

Deletes a Record Culture and optionally removes any Links to it.
Records start from a value of 1. 

    R=  Integer - Record Number to Delete (0 for All).
    y=  Boolean - True to Remove related Links to this
                          record.
------------------------------------------------------------------   

See application documentation for more information regarding Command 
Line or use this Command Line Option:  

    java -jar MyApp.jar /?
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;


public class MyApp {

    public static void main(String[] args) {
        // Are there Command Line arguments?
        if (args.length > 0) {
            // Yes there is...
            for (int i = 0; i < args.length; i++) {
                String[] suppliedArgs = args[i].toLowerCase().split("\\s+");
                // Function Categories
                switch (suppliedArgs[0]) {
                    case "/?":
                        doDisplayHelpInConsole();
                        System.exit(0);
                        break; 
                    case "history":
                        doHistory(suppliedArgs);
                        break;
                    case "current":
                        doCurrent(suppliedArgs);
                        break;
                    case "modify":
                        doModify(suppliedArgs);
                        break;
                    case "delete":
                        doDelete(suppliedArgs);
                        break;
                    default:
                        System.out.println("Skipping Unknown Command Line Function (" + 
                                        suppliedArgs[0] + ")!");
                }
            }
        }
        System.out.println("DONE!");
    }

    private static void doDisplayHelpInConsole() {
        // Prepare to read the MyAppHelp.txt file from 
        // the run location of MyApp.jar. This help text
        // file must reside in the same location the jar 
        // file resides in.
        File helpFile = new File(new File("").getAbsolutePath() + 
                                File.separator + "MyAppHelp.txt");
        if (!helpFile.exists()) {
            System.out.println("Help is not available - File Not Found!" + 
                System.lineSeparator() + "[" + helpFile.getAbsolutePath() + 
                "]");
            return;
        }

        try {
            // Try with Resources - Auto closes file.
            try (Scanner reader = new Scanner(helpFile)) {
                while (reader.hasNextLine()) {
                    System.out.println(reader.nextLine());
                }
            }
        }
        catch (FileNotFoundException ex) {
            Logger.getLogger(MyApp.class.getName()).log(Level.SEVERE, null, ex);
            // or use:  ex.printStackTrace();
        }
    }

    private static void doHistory(String[] args) {
        for (int i = 1; i < args.length; i++) {
            // Split the parameter name and its value
            String[] params = args[i].split(" = |= | =|=");
            // Process Parameters
            switch (params[0]) {
                case "c":
                    // Do what C is suppose to do...
                    clearHistory();                  // A method to clear history
                    System.out.println("History Cleared!");
                    break;
                case "r":
                    // Do what R is suppose to do... 
                    // Is the supplied value Numerical?
                    if (params[1].matches("\\d+")) {
                        // Yes...convert to integer
                        int recordNum = Integer.parseInt(params[1]);
                        displayRecord(recordNum);    // A method to display record
                    }
                    break;
                default:
                    System.out.println("Unknown Assignment for History (" + 
                            params[0] + ")! History Failed!"); 
            }
        }
    }

    private static void doCurrent(String[] args) {
        int recordNum = 0;
        for (int i = 1; i < args.length; i++) {
            // Split the parameter name and its value
            String[] params = args[i].split(" = |= | =|=");
            // Process Parameters
            switch (params[0]) {
                case "r":
                    // Do what R is suppose to do...
                    // Is the supplied value Numerical?
                    if (params[1].matches("\\d+")) {
                        // Yes...convert to integer
                        recordNum = Integer.parseInt(params[1]);
                        displayRecord(recordNum);    // A method to display record
                    }
                    break;
                case "l":
                    // Do what L is suppose to do... 
                    // Is the supplied value Numerical?
                    if (params[1].matches("\\d+")) {
                        // Yes...convert to integer
                        int lRecordNum = Integer.parseInt(params[1]);
                        System.out.println("Linked to Record Number: " + lRecordNum);
                    }
                    break;
                case "n":
                    // Do what N is suppose to do... 
                    String varName = getVariableName(recordNum);
                    System.out.println("Variable Name: " + varName);
                    break;    
                default:
                    System.out.println("Unknown Assignment for Current (" + 
                            params[0] + ")! Current Failed!");
            }
        }
    }

    private static void doModify(String[] args) {
        int recordNum = 0;
        for (int i = 1; i < args.length; i++) {
            // Split the parameter name and its value
            String[] params = args[i].split(" = |= | =|=");
            // Process Parameters
            switch (params[0]) {
                case "r":
                    // Do what R is suppose to do...
                    // Is the supplied value Numerical?
                    if (params[1].matches("\\d+")) {
                        // Yes...convert to integer
                        recordNum = Integer.parseInt(params[1]);
                    }
                    break;
                case "l":
                    // Do what L is suppose to do... 
                    // Is the supplied value Numerical?
                    if (params[1].matches("\\d+")) {
                        // Yes...convert to integer
                        int lRecordNum = Integer.parseInt(params[1]);
                        System.out.println("Modifying Linked Record Number To: " + lRecordNum);
                        modifyLinkedRecordNumber(recordNum, lRecordNum);
                    }
                    break;
                case "n":
                    // Do what N is suppose to do... 
                    String varName = getVariableName(recordNum);
                    System.out.println("Modifying Variable Name To: " + varName);
                    modifyVariableName(recordNum, varName);
                    break;    
                default:
                    System.out.println("Unknown Assignment for Modify (" + 
                            params[0] + ")! Modify Failed!");
            }
        }
    }

    private static void doDelete(String[] args) {
        int recordNum = 0;
        for (int i = 1; i < args.length; i++) {
            // Split the parameter name and its value
            String[] params = args[i].split(" = |= | =|=");
            // Process Parameters
            switch (params[0]) {
                case "r":
                    // Do what R is suppose to do...
                    // Is the supplied value Numerical?
                    if (params[1].matches("\\d+")) {
                        // Yes...convert to integer
                        recordNum = Integer.parseInt(params[1]);
                        if (deleteRecord(recordNum)) {
                            System.out.println("Record Number " + recordNum + 
                                               " has been Deleted.");
                        }
                    }
                    break;
                case "y":
                    // Do what Y is suppose to do... 
                    boolean doDel = false;
                    if (params[1].equalsIgnoreCase("true")) {
                        if (deleteAllLinksTo(recordNum)) {
                            System.out.println("All Links related to Record Number " + 
                                                recordNum + " have been removed.");
                        }
                    }
                    else if (!params[1].equalsIgnoreCase("false")) {
                        System.out.println("Unknown Boolean argument supplied for Y "
                                + "parameter of the DELETE function (" + 
                                params[i] + ")! Link Deletion Failed!");
                    }
                    break;
                default:
                    System.out.println("Unknown Assignment for Delete (" + 
                            params[0] + ")! Delete Failed!"); 
            }
        }
    }

    private static void clearHistory() {
        // A method to clear all history.
        //Code goes here........
    }

    private static void displayRecord (int recordNum) {
        // A method to display a specific record.
        //Code goes here........
    }

    private static String getVariableName(int recNum) {
        // A method to get variable name for a specific record.
        String varName = "";
        //Code goes here........
        return varName;
    }

    private static void modifyLinkedRecordNumber(int recordNum, int newLinkedNum) {
        // A method to modify the liknked record number for a speciic record.
        //Code goes here........
    }

    private static void modifyVariableName(int recordNum, String varName) {
        // A method to modify the Variable Name for a speciic record.
        //Code goes here........
    }

    private static boolean deleteRecord (int recordNum) {
        // A method to Delete a speciic record.
        //Code goes here........
        return true;
    }

    private static boolean deleteAllLinksTo (int recordNum) {
        // A method to Delete ALL Links to the supplied record.
        //Code goes here........
        return true;
    }

}
窗口将显示:

Hi,
my
name
is
Johnny
Walker
Hi, my name is Johnny Walker
Hi, my name is Johnny Walker
What's yours?
Is it Bob?
如果要在命令行中添加更多类似的字符串,则需要将它们全部括在引号内,并用如下空白分隔:

java -jar MyApp.jar "Hi, my name is Johnny Walker" "What's yours?" "Is it Bob?"
MyApp Help:

There are four categorical application functions available that can be
accessed via the Command Line (History, Current, Modify, and Delete).
Each function along with their specific parameters must be supplied as
quoted strings. The functions and their respective parameters can be
supplied in any order, for example:

**java -jar MyApp.jar "modify r=12 l=hypo n=43.55" "history c r=12"**

Categorical Application Functions:

Letter case is optional.

------------------------------------------------------------------
HISTORY:

Display Record(s) History.

    C   Clear History.
    R=  Integer - Record Number (0 for all).
------------------------------------------------------------------

CURRENT:

Displays Current Record and Linked Record (if any).
Optionaly displays defined Variable Name.


    R=  Integer - Make Record Current (0 for No Record).
    L=  Integer - Linked To Record Number (0 for No Record).
    N   Optional- Also Display Variable Name.
------------------------------------------------------------------

MODIFY:

Modifies a Record Culture.

    R=  Integer - Record Number to Modify (0 for All).
    L=  Integer - Change Linked Record Number (0 for No 
                Record).
    N=  String  - Change Variable Name ("" for No Name).
------------------------------------------------------------------

DELETE:

Deletes a Record Culture and optionally removes any Links to it.
Records start from a value of 1. 

    R=  Integer - Record Number to Delete (0 for All).
    y=  Boolean - True to Remove related Links to this
                          record.
------------------------------------------------------------------   

See application documentation for more information regarding Command 
Line or use this Command Line Option:  

    java -jar MyApp.jar /?
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;


public class MyApp {

    public static void main(String[] args) {
        // Are there Command Line arguments?
        if (args.length > 0) {
            // Yes there is...
            for (int i = 0; i < args.length; i++) {
                String[] suppliedArgs = args[i].toLowerCase().split("\\s+");
                // Function Categories
                switch (suppliedArgs[0]) {
                    case "/?":
                        doDisplayHelpInConsole();
                        System.exit(0);
                        break; 
                    case "history":
                        doHistory(suppliedArgs);
                        break;
                    case "current":
                        doCurrent(suppliedArgs);
                        break;
                    case "modify":
                        doModify(suppliedArgs);
                        break;
                    case "delete":
                        doDelete(suppliedArgs);
                        break;
                    default:
                        System.out.println("Skipping Unknown Command Line Function (" + 
                                        suppliedArgs[0] + ")!");
                }
            }
        }
        System.out.println("DONE!");
    }

    private static void doDisplayHelpInConsole() {
        // Prepare to read the MyAppHelp.txt file from 
        // the run location of MyApp.jar. This help text
        // file must reside in the same location the jar 
        // file resides in.
        File helpFile = new File(new File("").getAbsolutePath() + 
                                File.separator + "MyAppHelp.txt");
        if (!helpFile.exists()) {
            System.out.println("Help is not available - File Not Found!" + 
                System.lineSeparator() + "[" + helpFile.getAbsolutePath() + 
                "]");
            return;
        }

        try {
            // Try with Resources - Auto closes file.
            try (Scanner reader = new Scanner(helpFile)) {
                while (reader.hasNextLine()) {
                    System.out.println(reader.nextLine());
                }
            }
        }
        catch (FileNotFoundException ex) {
            Logger.getLogger(MyApp.class.getName()).log(Level.SEVERE, null, ex);
            // or use:  ex.printStackTrace();
        }
    }

    private static void doHistory(String[] args) {
        for (int i = 1; i < args.length; i++) {
            // Split the parameter name and its value
            String[] params = args[i].split(" = |= | =|=");
            // Process Parameters
            switch (params[0]) {
                case "c":
                    // Do what C is suppose to do...
                    clearHistory();                  // A method to clear history
                    System.out.println("History Cleared!");
                    break;
                case "r":
                    // Do what R is suppose to do... 
                    // Is the supplied value Numerical?
                    if (params[1].matches("\\d+")) {
                        // Yes...convert to integer
                        int recordNum = Integer.parseInt(params[1]);
                        displayRecord(recordNum);    // A method to display record
                    }
                    break;
                default:
                    System.out.println("Unknown Assignment for History (" + 
                            params[0] + ")! History Failed!"); 
            }
        }
    }

    private static void doCurrent(String[] args) {
        int recordNum = 0;
        for (int i = 1; i < args.length; i++) {
            // Split the parameter name and its value
            String[] params = args[i].split(" = |= | =|=");
            // Process Parameters
            switch (params[0]) {
                case "r":
                    // Do what R is suppose to do...
                    // Is the supplied value Numerical?
                    if (params[1].matches("\\d+")) {
                        // Yes...convert to integer
                        recordNum = Integer.parseInt(params[1]);
                        displayRecord(recordNum);    // A method to display record
                    }
                    break;
                case "l":
                    // Do what L is suppose to do... 
                    // Is the supplied value Numerical?
                    if (params[1].matches("\\d+")) {
                        // Yes...convert to integer
                        int lRecordNum = Integer.parseInt(params[1]);
                        System.out.println("Linked to Record Number: " + lRecordNum);
                    }
                    break;
                case "n":
                    // Do what N is suppose to do... 
                    String varName = getVariableName(recordNum);
                    System.out.println("Variable Name: " + varName);
                    break;    
                default:
                    System.out.println("Unknown Assignment for Current (" + 
                            params[0] + ")! Current Failed!");
            }
        }
    }

    private static void doModify(String[] args) {
        int recordNum = 0;
        for (int i = 1; i < args.length; i++) {
            // Split the parameter name and its value
            String[] params = args[i].split(" = |= | =|=");
            // Process Parameters
            switch (params[0]) {
                case "r":
                    // Do what R is suppose to do...
                    // Is the supplied value Numerical?
                    if (params[1].matches("\\d+")) {
                        // Yes...convert to integer
                        recordNum = Integer.parseInt(params[1]);
                    }
                    break;
                case "l":
                    // Do what L is suppose to do... 
                    // Is the supplied value Numerical?
                    if (params[1].matches("\\d+")) {
                        // Yes...convert to integer
                        int lRecordNum = Integer.parseInt(params[1]);
                        System.out.println("Modifying Linked Record Number To: " + lRecordNum);
                        modifyLinkedRecordNumber(recordNum, lRecordNum);
                    }
                    break;
                case "n":
                    // Do what N is suppose to do... 
                    String varName = getVariableName(recordNum);
                    System.out.println("Modifying Variable Name To: " + varName);
                    modifyVariableName(recordNum, varName);
                    break;    
                default:
                    System.out.println("Unknown Assignment for Modify (" + 
                            params[0] + ")! Modify Failed!");
            }
        }
    }

    private static void doDelete(String[] args) {
        int recordNum = 0;
        for (int i = 1; i < args.length; i++) {
            // Split the parameter name and its value
            String[] params = args[i].split(" = |= | =|=");
            // Process Parameters
            switch (params[0]) {
                case "r":
                    // Do what R is suppose to do...
                    // Is the supplied value Numerical?
                    if (params[1].matches("\\d+")) {
                        // Yes...convert to integer
                        recordNum = Integer.parseInt(params[1]);
                        if (deleteRecord(recordNum)) {
                            System.out.println("Record Number " + recordNum + 
                                               " has been Deleted.");
                        }
                    }
                    break;
                case "y":
                    // Do what Y is suppose to do... 
                    boolean doDel = false;
                    if (params[1].equalsIgnoreCase("true")) {
                        if (deleteAllLinksTo(recordNum)) {
                            System.out.println("All Links related to Record Number " + 
                                                recordNum + " have been removed.");
                        }
                    }
                    else if (!params[1].equalsIgnoreCase("false")) {
                        System.out.println("Unknown Boolean argument supplied for Y "
                                + "parameter of the DELETE function (" + 
                                params[i] + ")! Link Deletion Failed!");
                    }
                    break;
                default:
                    System.out.println("Unknown Assignment for Delete (" + 
                            params[0] + ")! Delete Failed!"); 
            }
        }
    }

    private static void clearHistory() {
        // A method to clear all history.
        //Code goes here........
    }

    private static void displayRecord (int recordNum) {
        // A method to display a specific record.
        //Code goes here........
    }

    private static String getVariableName(int recNum) {
        // A method to get variable name for a specific record.
        String varName = "";
        //Code goes here........
        return varName;
    }

    private static void modifyLinkedRecordNumber(int recordNum, int newLinkedNum) {
        // A method to modify the liknked record number for a speciic record.
        //Code goes here........
    }

    private static void modifyVariableName(int recordNum, String varName) {
        // A method to modify the Variable Name for a speciic record.
        //Code goes here........
    }

    private static boolean deleteRecord (int recordNum) {
        // A method to Delete a speciic record.
        //Code goes here........
        return true;
    }

    private static boolean deleteAllLinksTo (int recordNum) {
        // A method to Delete ALL Links to the supplied record.
        //Code goes here........
        return true;
    }

}
窗口将显示:

Hi,
my
name
is
Johnny
Walker
Hi, my name is Johnny Walker
Hi, my name is Johnny Walker
What's yours?
Is it Bob?
考虑到上述情况,我不明白你的困难在哪里。如果允许应用程序的用户以任何顺序提供命令行参数,那么就让他们这样做。在我看来,如果有很多不同的命令行参数可以提供,那么允许它们以随机顺序提供就更有意义了,因为要记住它们必须提供的确切顺序是极其困难的。在收到参数后,应用程序需要整理排序的必要性

由于您希望创建自己的命令行解析器,该解析器将由一个特定的分类应用程序函数或几个特定的分类函数组成,因此您需要准确地指示用户如何将命令行参数提供给应用程序,甚至允许将帮助上下文与使用典型的/?命令行选项,例如:

有四个应用程序分类函数可供选择 通过命令行访问(历史、当前、修改和删除)。 每个函数及其特定参数必须按照以下要求提供: 带引号的字符串。这些功能及其各自的参数可以是 以任何顺序提供,例如:

java-jar MyApp.jar“modify r l n”“history c r”

有关命令的更多信息,请参阅应用程序文档 行或使用此命令行选项:

java-jar MyApp.jar/?

应用程序的基于文本的帮助文件可能如下所示:

java -jar MyApp.jar "Hi, my name is Johnny Walker" "What's yours?" "Is it Bob?"
MyApp Help:

There are four categorical application functions available that can be
accessed via the Command Line (History, Current, Modify, and Delete).
Each function along with their specific parameters must be supplied as
quoted strings. The functions and their respective parameters can be
supplied in any order, for example:

**java -jar MyApp.jar "modify r=12 l=hypo n=43.55" "history c r=12"**

Categorical Application Functions:

Letter case is optional.

------------------------------------------------------------------
HISTORY:

Display Record(s) History.

    C   Clear History.
    R=  Integer - Record Number (0 for all).
------------------------------------------------------------------

CURRENT:

Displays Current Record and Linked Record (if any).
Optionaly displays defined Variable Name.


    R=  Integer - Make Record Current (0 for No Record).
    L=  Integer - Linked To Record Number (0 for No Record).
    N   Optional- Also Display Variable Name.
------------------------------------------------------------------

MODIFY:

Modifies a Record Culture.

    R=  Integer - Record Number to Modify (0 for All).
    L=  Integer - Change Linked Record Number (0 for No 
                Record).
    N=  String  - Change Variable Name ("" for No Name).
------------------------------------------------------------------

DELETE:

Deletes a Record Culture and optionally removes any Links to it.
Records start from a value of 1. 

    R=  Integer - Record Number to Delete (0 for All).
    y=  Boolean - True to Remove related Links to this
                          record.
------------------------------------------------------------------   

See application documentation for more information regarding Command 
Line or use this Command Line Option:  

    java -jar MyApp.jar /?
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;


public class MyApp {

    public static void main(String[] args) {
        // Are there Command Line arguments?
        if (args.length > 0) {
            // Yes there is...
            for (int i = 0; i < args.length; i++) {
                String[] suppliedArgs = args[i].toLowerCase().split("\\s+");
                // Function Categories
                switch (suppliedArgs[0]) {
                    case "/?":
                        doDisplayHelpInConsole();
                        System.exit(0);
                        break; 
                    case "history":
                        doHistory(suppliedArgs);
                        break;
                    case "current":
                        doCurrent(suppliedArgs);
                        break;
                    case "modify":
                        doModify(suppliedArgs);
                        break;
                    case "delete":
                        doDelete(suppliedArgs);
                        break;
                    default:
                        System.out.println("Skipping Unknown Command Line Function (" + 
                                        suppliedArgs[0] + ")!");
                }
            }
        }
        System.out.println("DONE!");
    }

    private static void doDisplayHelpInConsole() {
        // Prepare to read the MyAppHelp.txt file from 
        // the run location of MyApp.jar. This help text
        // file must reside in the same location the jar 
        // file resides in.
        File helpFile = new File(new File("").getAbsolutePath() + 
                                File.separator + "MyAppHelp.txt");
        if (!helpFile.exists()) {
            System.out.println("Help is not available - File Not Found!" + 
                System.lineSeparator() + "[" + helpFile.getAbsolutePath() + 
                "]");
            return;
        }

        try {
            // Try with Resources - Auto closes file.
            try (Scanner reader = new Scanner(helpFile)) {
                while (reader.hasNextLine()) {
                    System.out.println(reader.nextLine());
                }
            }
        }
        catch (FileNotFoundException ex) {
            Logger.getLogger(MyApp.class.getName()).log(Level.SEVERE, null, ex);
            // or use:  ex.printStackTrace();
        }
    }

    private static void doHistory(String[] args) {
        for (int i = 1; i < args.length; i++) {
            // Split the parameter name and its value
            String[] params = args[i].split(" = |= | =|=");
            // Process Parameters
            switch (params[0]) {
                case "c":
                    // Do what C is suppose to do...
                    clearHistory();                  // A method to clear history
                    System.out.println("History Cleared!");
                    break;
                case "r":
                    // Do what R is suppose to do... 
                    // Is the supplied value Numerical?
                    if (params[1].matches("\\d+")) {
                        // Yes...convert to integer
                        int recordNum = Integer.parseInt(params[1]);
                        displayRecord(recordNum);    // A method to display record
                    }
                    break;
                default:
                    System.out.println("Unknown Assignment for History (" + 
                            params[0] + ")! History Failed!"); 
            }
        }
    }

    private static void doCurrent(String[] args) {
        int recordNum = 0;
        for (int i = 1; i < args.length; i++) {
            // Split the parameter name and its value
            String[] params = args[i].split(" = |= | =|=");
            // Process Parameters
            switch (params[0]) {
                case "r":
                    // Do what R is suppose to do...
                    // Is the supplied value Numerical?
                    if (params[1].matches("\\d+")) {
                        // Yes...convert to integer
                        recordNum = Integer.parseInt(params[1]);
                        displayRecord(recordNum);    // A method to display record
                    }
                    break;
                case "l":
                    // Do what L is suppose to do... 
                    // Is the supplied value Numerical?
                    if (params[1].matches("\\d+")) {
                        // Yes...convert to integer
                        int lRecordNum = Integer.parseInt(params[1]);
                        System.out.println("Linked to Record Number: " + lRecordNum);
                    }
                    break;
                case "n":
                    // Do what N is suppose to do... 
                    String varName = getVariableName(recordNum);
                    System.out.println("Variable Name: " + varName);
                    break;    
                default:
                    System.out.println("Unknown Assignment for Current (" + 
                            params[0] + ")! Current Failed!");
            }
        }
    }

    private static void doModify(String[] args) {
        int recordNum = 0;
        for (int i = 1; i < args.length; i++) {
            // Split the parameter name and its value
            String[] params = args[i].split(" = |= | =|=");
            // Process Parameters
            switch (params[0]) {
                case "r":
                    // Do what R is suppose to do...
                    // Is the supplied value Numerical?
                    if (params[1].matches("\\d+")) {
                        // Yes...convert to integer
                        recordNum = Integer.parseInt(params[1]);
                    }
                    break;
                case "l":
                    // Do what L is suppose to do... 
                    // Is the supplied value Numerical?
                    if (params[1].matches("\\d+")) {
                        // Yes...convert to integer
                        int lRecordNum = Integer.parseInt(params[1]);
                        System.out.println("Modifying Linked Record Number To: " + lRecordNum);
                        modifyLinkedRecordNumber(recordNum, lRecordNum);
                    }
                    break;
                case "n":
                    // Do what N is suppose to do... 
                    String varName = getVariableName(recordNum);
                    System.out.println("Modifying Variable Name To: " + varName);
                    modifyVariableName(recordNum, varName);
                    break;    
                default:
                    System.out.println("Unknown Assignment for Modify (" + 
                            params[0] + ")! Modify Failed!");
            }
        }
    }

    private static void doDelete(String[] args) {
        int recordNum = 0;
        for (int i = 1; i < args.length; i++) {
            // Split the parameter name and its value
            String[] params = args[i].split(" = |= | =|=");
            // Process Parameters
            switch (params[0]) {
                case "r":
                    // Do what R is suppose to do...
                    // Is the supplied value Numerical?
                    if (params[1].matches("\\d+")) {
                        // Yes...convert to integer
                        recordNum = Integer.parseInt(params[1]);
                        if (deleteRecord(recordNum)) {
                            System.out.println("Record Number " + recordNum + 
                                               " has been Deleted.");
                        }
                    }
                    break;
                case "y":
                    // Do what Y is suppose to do... 
                    boolean doDel = false;
                    if (params[1].equalsIgnoreCase("true")) {
                        if (deleteAllLinksTo(recordNum)) {
                            System.out.println("All Links related to Record Number " + 
                                                recordNum + " have been removed.");
                        }
                    }
                    else if (!params[1].equalsIgnoreCase("false")) {
                        System.out.println("Unknown Boolean argument supplied for Y "
                                + "parameter of the DELETE function (" + 
                                params[i] + ")! Link Deletion Failed!");
                    }
                    break;
                default:
                    System.out.println("Unknown Assignment for Delete (" + 
                            params[0] + ")! Delete Failed!"); 
            }
        }
    }

    private static void clearHistory() {
        // A method to clear all history.
        //Code goes here........
    }

    private static void displayRecord (int recordNum) {
        // A method to display a specific record.
        //Code goes here........
    }

    private static String getVariableName(int recNum) {
        // A method to get variable name for a specific record.
        String varName = "";
        //Code goes here........
        return varName;
    }

    private static void modifyLinkedRecordNumber(int recordNum, int newLinkedNum) {
        // A method to modify the liknked record number for a speciic record.
        //Code goes here........
    }

    private static void modifyVariableName(int recordNum, String varName) {
        // A method to modify the Variable Name for a speciic record.
        //Code goes here........
    }

    private static boolean deleteRecord (int recordNum) {
        // A method to Delete a speciic record.
        //Code goes here........
        return true;
    }

    private static boolean deleteAllLinksTo (int recordNum) {
        // A method to Delete ALL Links to the supplied record.
        //Code goes here........
        return true;
    }

}
在应用程序的main()方法中,用于处理命令行选项的方法可能如下所示:

java -jar MyApp.jar "Hi, my name is Johnny Walker" "What's yours?" "Is it Bob?"
MyApp Help:

There are four categorical application functions available that can be
accessed via the Command Line (History, Current, Modify, and Delete).
Each function along with their specific parameters must be supplied as
quoted strings. The functions and their respective parameters can be
supplied in any order, for example:

**java -jar MyApp.jar "modify r=12 l=hypo n=43.55" "history c r=12"**

Categorical Application Functions:

Letter case is optional.

------------------------------------------------------------------
HISTORY:

Display Record(s) History.

    C   Clear History.
    R=  Integer - Record Number (0 for all).
------------------------------------------------------------------

CURRENT:

Displays Current Record and Linked Record (if any).
Optionaly displays defined Variable Name.


    R=  Integer - Make Record Current (0 for No Record).
    L=  Integer - Linked To Record Number (0 for No Record).
    N   Optional- Also Display Variable Name.
------------------------------------------------------------------

MODIFY:

Modifies a Record Culture.

    R=  Integer - Record Number to Modify (0 for All).
    L=  Integer - Change Linked Record Number (0 for No 
                Record).
    N=  String  - Change Variable Name ("" for No Name).
------------------------------------------------------------------

DELETE:

Deletes a Record Culture and optionally removes any Links to it.
Records start from a value of 1. 

    R=  Integer - Record Number to Delete (0 for All).
    y=  Boolean - True to Remove related Links to this
                          record.
------------------------------------------------------------------   

See application documentation for more information regarding Command 
Line or use this Command Line Option:  

    java -jar MyApp.jar /?
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;


public class MyApp {

    public static void main(String[] args) {
        // Are there Command Line arguments?
        if (args.length > 0) {
            // Yes there is...
            for (int i = 0; i < args.length; i++) {
                String[] suppliedArgs = args[i].toLowerCase().split("\\s+");
                // Function Categories
                switch (suppliedArgs[0]) {
                    case "/?":
                        doDisplayHelpInConsole();
                        System.exit(0);
                        break; 
                    case "history":
                        doHistory(suppliedArgs);
                        break;
                    case "current":
                        doCurrent(suppliedArgs);
                        break;
                    case "modify":
                        doModify(suppliedArgs);
                        break;
                    case "delete":
                        doDelete(suppliedArgs);
                        break;
                    default:
                        System.out.println("Skipping Unknown Command Line Function (" + 
                                        suppliedArgs[0] + ")!");
                }
            }
        }
        System.out.println("DONE!");
    }

    private static void doDisplayHelpInConsole() {
        // Prepare to read the MyAppHelp.txt file from 
        // the run location of MyApp.jar. This help text
        // file must reside in the same location the jar 
        // file resides in.
        File helpFile = new File(new File("").getAbsolutePath() + 
                                File.separator + "MyAppHelp.txt");
        if (!helpFile.exists()) {
            System.out.println("Help is not available - File Not Found!" + 
                System.lineSeparator() + "[" + helpFile.getAbsolutePath() + 
                "]");
            return;
        }

        try {
            // Try with Resources - Auto closes file.
            try (Scanner reader = new Scanner(helpFile)) {
                while (reader.hasNextLine()) {
                    System.out.println(reader.nextLine());
                }
            }
        }
        catch (FileNotFoundException ex) {
            Logger.getLogger(MyApp.class.getName()).log(Level.SEVERE, null, ex);
            // or use:  ex.printStackTrace();
        }
    }

    private static void doHistory(String[] args) {
        for (int i = 1; i < args.length; i++) {
            // Split the parameter name and its value
            String[] params = args[i].split(" = |= | =|=");
            // Process Parameters
            switch (params[0]) {
                case "c":
                    // Do what C is suppose to do...
                    clearHistory();                  // A method to clear history
                    System.out.println("History Cleared!");
                    break;
                case "r":
                    // Do what R is suppose to do... 
                    // Is the supplied value Numerical?
                    if (params[1].matches("\\d+")) {
                        // Yes...convert to integer
                        int recordNum = Integer.parseInt(params[1]);
                        displayRecord(recordNum);    // A method to display record
                    }
                    break;
                default:
                    System.out.println("Unknown Assignment for History (" + 
                            params[0] + ")! History Failed!"); 
            }
        }
    }

    private static void doCurrent(String[] args) {
        int recordNum = 0;
        for (int i = 1; i < args.length; i++) {
            // Split the parameter name and its value
            String[] params = args[i].split(" = |= | =|=");
            // Process Parameters
            switch (params[0]) {
                case "r":
                    // Do what R is suppose to do...
                    // Is the supplied value Numerical?
                    if (params[1].matches("\\d+")) {
                        // Yes...convert to integer
                        recordNum = Integer.parseInt(params[1]);
                        displayRecord(recordNum);    // A method to display record
                    }
                    break;
                case "l":
                    // Do what L is suppose to do... 
                    // Is the supplied value Numerical?
                    if (params[1].matches("\\d+")) {
                        // Yes...convert to integer
                        int lRecordNum = Integer.parseInt(params[1]);
                        System.out.println("Linked to Record Number: " + lRecordNum);
                    }
                    break;
                case "n":
                    // Do what N is suppose to do... 
                    String varName = getVariableName(recordNum);
                    System.out.println("Variable Name: " + varName);
                    break;    
                default:
                    System.out.println("Unknown Assignment for Current (" + 
                            params[0] + ")! Current Failed!");
            }
        }
    }

    private static void doModify(String[] args) {
        int recordNum = 0;
        for (int i = 1; i < args.length; i++) {
            // Split the parameter name and its value
            String[] params = args[i].split(" = |= | =|=");
            // Process Parameters
            switch (params[0]) {
                case "r":
                    // Do what R is suppose to do...
                    // Is the supplied value Numerical?
                    if (params[1].matches("\\d+")) {
                        // Yes...convert to integer
                        recordNum = Integer.parseInt(params[1]);
                    }
                    break;
                case "l":
                    // Do what L is suppose to do... 
                    // Is the supplied value Numerical?
                    if (params[1].matches("\\d+")) {
                        // Yes...convert to integer
                        int lRecordNum = Integer.parseInt(params[1]);
                        System.out.println("Modifying Linked Record Number To: " + lRecordNum);
                        modifyLinkedRecordNumber(recordNum, lRecordNum);
                    }
                    break;
                case "n":
                    // Do what N is suppose to do... 
                    String varName = getVariableName(recordNum);
                    System.out.println("Modifying Variable Name To: " + varName);
                    modifyVariableName(recordNum, varName);
                    break;    
                default:
                    System.out.println("Unknown Assignment for Modify (" + 
                            params[0] + ")! Modify Failed!");
            }
        }
    }

    private static void doDelete(String[] args) {
        int recordNum = 0;
        for (int i = 1; i < args.length; i++) {
            // Split the parameter name and its value
            String[] params = args[i].split(" = |= | =|=");
            // Process Parameters
            switch (params[0]) {
                case "r":
                    // Do what R is suppose to do...
                    // Is the supplied value Numerical?
                    if (params[1].matches("\\d+")) {
                        // Yes...convert to integer
                        recordNum = Integer.parseInt(params[1]);
                        if (deleteRecord(recordNum)) {
                            System.out.println("Record Number " + recordNum + 
                                               " has been Deleted.");
                        }
                    }
                    break;
                case "y":
                    // Do what Y is suppose to do... 
                    boolean doDel = false;
                    if (params[1].equalsIgnoreCase("true")) {
                        if (deleteAllLinksTo(recordNum)) {
                            System.out.println("All Links related to Record Number " + 
                                                recordNum + " have been removed.");
                        }
                    }
                    else if (!params[1].equalsIgnoreCase("false")) {
                        System.out.println("Unknown Boolean argument supplied for Y "
                                + "parameter of the DELETE function (" + 
                                params[i] + ")! Link Deletion Failed!");
                    }
                    break;
                default:
                    System.out.println("Unknown Assignment for Delete (" + 
                            params[0] + ")! Delete Failed!"); 
            }
        }
    }

    private static void clearHistory() {
        // A method to clear all history.
        //Code goes here........
    }

    private static void displayRecord (int recordNum) {
        // A method to display a specific record.
        //Code goes here........
    }

    private static String getVariableName(int recNum) {
        // A method to get variable name for a specific record.
        String varName = "";
        //Code goes here........
        return varName;
    }

    private static void modifyLinkedRecordNumber(int recordNum, int newLinkedNum) {
        // A method to modify the liknked record number for a speciic record.
        //Code goes here........
    }

    private static void modifyVariableName(int recordNum, String varName) {
        // A method to modify the Variable Name for a speciic record.
        //Code goes here........
    }

    private static boolean deleteRecord (int recordNum) {
        // A method to Delete a speciic record.
        //Code goes here........
        return true;
    }

    private static boolean deleteAllLinksTo (int recordNum) {
        // A method to Delete ALL Links to the supplied record.
        //Code goes here........
        return true;
    }

}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.Scanner;
导入java.util.logging.Level;
导入java.util.logging.Logger;
公共类MyApp{
公共静态void main(字符串[]args){
//有命令行参数吗?
如果(args.length>0){
//是的,有。。。
对于(int i=0;i