在命令行应用程序中与文本在同一行上获取输入

时间:2018-07-15 13:07:24

标签: java

如何在Java应用程序中在与命令行相同的行上获取输入:

我目前有

private static String input(String msg) {
    Log.log(Level.INFO, "" + msg + ": ");
    return null;
}

public void listen() {
    while (true) {
        // Display input message
        input("\ntest-cli");

        // Gather user input
        String rawInput = scanner.nextLine();

        // Check if the users wishes to quit
        if (rawInput.equals("quit") || rawInput.equals("exit")) {
            break;
        }

        // Gather the raw arguments entered by the user
        String[] rawArgs = rawInput.split("\\s+");

        // Check any command or argument was entered
        if (!rawArgs[0].equals("")) {

            /* Check if debug mode is false, if so then run command in try catch to avoid crashing the application
            if an error is thrown. Otherwise run the command with no safety net */

            if (!debugMode) {
                try {
                    call(rawArgs);
                } catch (ArrayIndexOutOfBoundsException e) {
                    print("Command couldn't execute, perhaps not enough arguments? Try: " + rawArgs[0] + " help");
                } catch (Exception e) {
                    print("Command failed to execute.");
                }
            } else {
                call(rawArgs);
            }
        }
    }
}

static void print(String msg) {
    Log.log(Level.INFO, msg);
}

输出:

test-cli: 
help

如何使它看起来像这样(rawInputinput在同一行)

test-cli> help

0 个答案:

没有答案