尝试在 Java 中使用 CLI 工具
Trying to make a CLI tool in Java work
所以这是我的代码:
package ca.wax.main;
import java.util.Arrays;
import java.util.Scanner;
public class Wax {
static String commands[];
public static void main(String[]args){
CommandMethods cm = new CommandMethods();
commands = new String[3];
int stringLength = commands.length;
commands[0] = "deauth";
commands[1] = "exploit";
commands[2] = "set";
Scanner s = new Scanner(System.in);
String nextCommand;
System.out.println("Welcome to Wax!");
System.out.println();
System.out.println();
System.out.println();
do{
System.out.println();
System.out.print("Wax >");
nextCommand= s.next();
if (Arrays.asList(commands).contains(nextCommand)){
if (commands[0]==nextCommand){
cm.deauth();
}else{
System.out.println("Cannot find command, try again.");
}
}
}while(1==1);
}
}
和其他类并不重要。我需要知道如何制作一个循环,以适应我正在使用它的上下文。我没有任何命令行界面编程经验。我也想从头开始做这个。所以如果你们有什么建议请帮助我。
我看到了你的新问题回复。我建议编辑您的原始问题,而不是用更新后的问题回答自己。你说你想要获取用户输入,执行命令,然后再次请求输入。您可以通过创建 getCommand() 函数来获取用户的输入来做到这一点:
public static String getCommand() {
Scanner s = new Scanner(System.in);
boolean containsCommand;
String nextCommand;
do {
nextCommand = s.nextLine();
containsCommand = false;
for (int i = 0; i < commands.length; i++) {
if (commands[i].equals(nextCommand) {
containsCommand = true;
return nextCommand;
}
}
if (!containsCommand) {
System.out.println("please enter valid option");
}
while(!containsCommand);
}
然后这样称呼它:
while (!exitFlag) {
String command = getCommand();
if (command.equals("commandOne")) {
//do command one stuff
} // else if...
}
这将获取命令,运行命令,然后循环回到获取命令。请注意,此循环可能 运行 永远存在,因此我建议添加一种设置退出标志的方法。
所以这是我的代码:
package ca.wax.main;
import java.util.Arrays;
import java.util.Scanner;
public class Wax {
static String commands[];
public static void main(String[]args){
CommandMethods cm = new CommandMethods();
commands = new String[3];
int stringLength = commands.length;
commands[0] = "deauth";
commands[1] = "exploit";
commands[2] = "set";
Scanner s = new Scanner(System.in);
String nextCommand;
System.out.println("Welcome to Wax!");
System.out.println();
System.out.println();
System.out.println();
do{
System.out.println();
System.out.print("Wax >");
nextCommand= s.next();
if (Arrays.asList(commands).contains(nextCommand)){
if (commands[0]==nextCommand){
cm.deauth();
}else{
System.out.println("Cannot find command, try again.");
}
}
}while(1==1);
}
}
和其他类并不重要。我需要知道如何制作一个循环,以适应我正在使用它的上下文。我没有任何命令行界面编程经验。我也想从头开始做这个。所以如果你们有什么建议请帮助我。
我看到了你的新问题回复。我建议编辑您的原始问题,而不是用更新后的问题回答自己。你说你想要获取用户输入,执行命令,然后再次请求输入。您可以通过创建 getCommand() 函数来获取用户的输入来做到这一点:
public static String getCommand() {
Scanner s = new Scanner(System.in);
boolean containsCommand;
String nextCommand;
do {
nextCommand = s.nextLine();
containsCommand = false;
for (int i = 0; i < commands.length; i++) {
if (commands[i].equals(nextCommand) {
containsCommand = true;
return nextCommand;
}
}
if (!containsCommand) {
System.out.println("please enter valid option");
}
while(!containsCommand);
}
然后这样称呼它:
while (!exitFlag) {
String command = getCommand();
if (command.equals("commandOne")) {
//do command one stuff
} // else if...
}
这将获取命令,运行命令,然后循环回到获取命令。请注意,此循环可能 运行 永远存在,因此我建议添加一种设置退出标志的方法。