我正在用java构建自己的命令行。我实现了所有其他命令。我想尝试根据ex参数的顺序执行命令)
命令:(历史Y C),命令:(历史Y )
(历史Y)表示打印以前的(Y)号命令的历史记录号,(C)表示清除历史记录命令。
所以(历史C)应该首先清除历史命令,并打印Y号以前的命令,这将是空的;
(历史Y)应该打印Y号以前的命令,然后清除历史命令。
如何根据不同的顺序实现代码输出结果
我用“”(空格)分割命令。
这就是我现在得到的
if (Split[0].equals("history")) {
if (Split[1].equals("Y")) {
int num = int parseint(command)
}
if (Split[2].equals("C")) {
" Then clear command"
}
}从上面的代码,它是历史Y如何实现它作为用户的选择,以不同的顺序?
发布于 2018-05-11 17:55:04
应用程序的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]);
}
}
}当您从以下位置启动编译的应用程序时,让我们假设窗口的命令提示符(cmd)是:
java -jar MyApp.jar Hi, my name is Johnny Walker窗口将显示:
Hi,
my
name
is
Johnny
Walker您可以看到,应用程序命令行中由一个(或多个)空格分隔的每一项都被视为单独的命令行参数。main()方法的参数总是用空格分隔,而不是典型的逗号(,)。但是,如果希望将整个字符串作为单个命令行参数提供给应用程序,则需要将整个字符串括在引号中,如下所示:
java -jar MyApp.jar "Hi, my name is Johnny Walker"窗口将显示:
Hi, my name is Johnny Walker如果您想要向命令行添加更多这样的字符串,那么它们都需要用引号括起来,并使用如下空格分隔:
java -jar MyApp.jar "Hi, my name is Johnny Walker" "What's yours?" "Is it Bob?"窗口将显示:
Hi, my name is Johnny Walker
What's yours?
Is it Bob?考虑到以上这些,我不明白你的困难在哪里。如果您的应用程序的用户允许以任何顺序提供命令行参数,那么就让他们这样做。在我看来,如果可以提供许多不同的命令行参数,那么允许它们按随机顺序提供是更有意义的,因为要记住必须提供它们的确切顺序是非常困难的。在收到论点后,应用程序需要理清顺序的必要性。
由于您希望创建自己的命令行解析器,它将由特定的分类应用程序函数或可能几个特定的分类函数组成,因此需要指示用户命令行参数是如何提供给应用程序的,甚至允许使用典型的/?命令行选项提供帮助上下文,例如:
可以通过命令行访问四个应用程序分类函数(历史记录、当前、修改和删除)。每个函数以及它们的特定参数必须以引号字符串的形式提供。这些函数及其各自的参数可以按任何顺序提供,例如: java -jar MyApp.jar“修改r l n”历史c r" 有关命令行或使用此命令行选项的更多信息,请参见应用程序文档:java -jar MyApp.jar /?
应用程序的基于文本的帮助文件可能如下所示:
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 /?在应用程序的用于处理命令行选项的main()方法中,如下所示:
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;
}
}https://stackoverflow.com/questions/50280535
复制相似问题