問題描述
Visual Basic 2013 ‑ 控制台輸入令牌? (Visual Basic 2013 ‑ Console Input Tokens?)
我正在嘗試從控制台窗口獲取用戶輸入,然後拆分為可以正常使用拆分的數組。我最近讀到了關於分裂的文章。我創建了以下數組來檢查拆分索引的每個位置的“輸入”可能性。但無法弄清楚如何實際檢查每個位置的有效輸入,更不用說對用戶的“命令”做一些事情了。
Dim l1args as Array = {"help","exit","start"} 'first position possibilities
Dim l2args as Array = {"server","exit"} 'second position possibilities
Console.Write("console# ")
Dim input = Console.ReadLine()
Dim tokens As String() = input.Split(CChar(" "))
誰能給我一些指導?我希望能夠檢查用戶輸入的組合併對其進行處理。然後,如果不存在或關閉可能性,我想向控制台寫一條錯誤消息,例如“命令無法識別”....
謝謝....
參考解法
方法 1:
This should be a "recurrent algorithm".
you should think like this "start" is function that gets a variable (what to start?).
This variable is also a command and so you need to run it before starting the "start command".
for example the command: "start print 1" lets say that print just prints 1, so basically what you are doing is getting the value of print 1 (the value of this is 1 because print 1 just prints 1) and then you will do start 1 (you will start 1, if there is no "1" make an error msg to the user.
The way I would do it is like this:
A command has a name and the amount of arguments it needs. for example the start command will have the name "start" and it need 1 argument.
OR the command help will have a name "help" and will need 0 arguments.
OR the command print will have a name "print" and will need 1 arguments.
Now lets say the user entered the command "start print chrome" you will look on the first word and see that start is a recognized command, so now you know you need to get its argument so you will go to the next word and run it as a command and give the result to the start command. So when going to the next word you see print this is also a recognized command so you will go to the next command to get the argument to the print command, now chrome is not a recognized command so you will assume that it is the value "chrome" (can be anythings else string, double ...) and you will run the command print with the argument "chrome" after that you will run the command start with the result of the command print.
Requested implementation:
This is a console application in vb.net . Usually I am now putting all the classes in one file, but to post it here I had to do so.
try using the command "print sum 9 10", this will print 19
Option Strict On
Option Explicit On
Module Module1
Public AllCommand As Dictionary(Of String, ConsoleCommand)
Sub Main()
GenerateAllCommands()
Dim UserCommand() As String
While True
UserCommand = Console.ReadLine().Split({" "c}, System.StringSplitOptions.RemoveEmptyEntries)
RunCommand(UserCommand, True, 0)
End While
End Sub
Public Function RunCommand(FullCommand() As String, IsFirst As Boolean, CommandIndex As Integer) As CommandReturnedValue
If CommandIndex > FullCommand.Length ‑ 1 Then
Return Nothing
End If
Dim CurrentCommand As String
If FullCommand.Length = 0 Then
If IsFirst Then
Console.WriteLine("Error ‑ Empty String is not a valid command")
End If
Return New CommandReturnedValue("", CommandIndex)
End If
CurrentCommand = FullCommand(CommandIndex).Trim().ToLower()
If CurrentCommand.Length = 0 Then
If IsFirst Then
Console.WriteLine("Error ‑ Empty String is not a valid command")
End If
Return New CommandReturnedValue("", CommandIndex)
End If
Dim TheCommnad As ConsoleCommand
If AllCommand.ContainsKey(CurrentCommand) Then
TheCommnad = AllCommand.Item(CurrentCommand)
Dim TempArgs(TheCommnad.Args ‑ 1) As CommandReturnedValue
Dim TempIndex As Integer = CommandIndex + 1
For i As Integer = 0 To TheCommnad.Args ‑ 1 Step 1
If TempIndex > FullCommand.Length ‑ 1 Then
Console.WriteLine("Error ‑ Need more arguments for """ & TheCommnad.CommandName & """ Command, You entered " & i & " Arguments, This command need " & TheCommnad.Args & " Arguents")
Return New CommandReturnedValue("", CommandIndex)
End If
TempArgs(i) = RunCommand(FullCommand, False, TempIndex)
If TempArgs(i) Is Nothing Then
Console.WriteLine("Error ‑ Need more arguments for """ & TheCommnad.CommandName & """ Command, You entered " & i & " Arguments, This command need " & TheCommnad.Args & " Arguents")
Return New CommandReturnedValue("", CommandIndex)
End If
If TempArgs(i).CommandIndex <> ‑1 Then
TempIndex = TempArgs(i).CommandIndex + 1
Else
TempIndex += 1
End If
Next
If TempArgs.Length = 0 Then
Return TheCommnad.DoCommand(TempArgs, CommandIndex)
Else
Return TheCommnad.DoCommand(TempArgs, TempArgs(TempArgs.Length ‑ 1).CommandIndex)
End If
Else
If IsFirst Then
Console.WriteLine("Error ‑ Unknown Command """ & CurrentCommand & """")
End If
Return New CommandReturnedValue(FullCommand(CommandIndex), CommandIndex)
End If
End Function
Public Sub GenerateAllCommands()
'NOTE ‑ ALL COMMAND MUST BE LOWER CASE
AllCommand = New Dictionary(Of String, ConsoleCommand)
AllCommand.Add("help", New HelpCommand())
AllCommand.Add("start", New StartCommand())
AllCommand.Add("print", New PrintCommand())
AllCommand.Add("sum", New SumCommand())
End Sub
End Module
Public Class CommandReturnedValue
Public Value As String
Public CommandIndex As Integer
Public Sub New(Value As String, Optional index As Integer = ‑1)
Me.Value = Value
Me.CommandIndex = index
End Sub
End Class
Public MustInherit Class ConsoleCommand
Public ReadOnly CommandName As String
Public ReadOnly Args As Integer
Public Sub New(Name As String, args As Integer)
Me.CommandName = Name
Me.Args = args
End Sub
Public MustOverride Function DoCommand(AllArgs() As CommandReturnedValue, CommandIndex As Integer) As CommandReturnedValue
End Class
Public Class StartCommand
Inherits ConsoleCommand
Public Sub New()
MyBase.New("start", 1)
End Sub
Public Overrides Function DoCommand(AllArgs() As CommandReturnedValue, CommandIndex As Integer) As CommandReturnedValue
If System.IO.File.Exists(AllArgs(0).Value) Then
Shell(AllArgs(0).Value, AppWinStyle.NormalFocus)
Else
Console.WriteLine("Error ‑ Unknown file (Start Command)")
End If
Return Nothing
End Function
End Class
Public Class HelpCommand
Inherits ConsoleCommand
Public Sub New()
MyBase.New("help", 0)
End Sub
Public Overrides Function DoCommand(AllArgs() As CommandReturnedValue, CommandIndex As Integer) As CommandReturnedValue
Console.WriteLine("I see you tryed doing the help commnad ;)")
Console.WriteLine("Commands: ")
Console.WriteLine(" Start <FileName> ‑ will try to start the <filename>")
Console.WriteLine(" help ‑ will show a help list")
Console.WriteLine(" print <value> ‑ will print (and return) <value>")
Return New CommandReturnedValue("Help Command", CommandIndex)
End Function
End Class
Public Class SumCommand
Inherits ConsoleCommand
Public Sub New()
MyBase.New("sum", 2)
End Sub
Public Overrides Function DoCommand(AllArgs() As CommandReturnedValue, CommandIndex As Integer) As CommandReturnedValue
Dim x As Integer
Dim y As Integer
Try
x = Convert.ToInt32(AllArgs(0).Value)
Catch ex As Exception
Console.WriteLine("Error ‑ Arguments to sum must be an integer, """ & AllArgs(0).Value & """ is not an integer")
Return Nothing
End Try
Try
y = Convert.ToInt32(AllArgs(1).Value)
Catch ex As Exception
Console.WriteLine("Error ‑ Arguments to sum must be an integer, """ & AllArgs(1).Value & """ is not an integer")
Return Nothing
End Try
Return New CommandReturnedValue((x + y).ToString, CommandIndex)
End Function
End Class
Public Class PrintCommand
Inherits ConsoleCommand
Public Sub New()
MyBase.New("print", 1)
End Sub
Public Overrides Function DoCommand(AllArgs() As CommandReturnedValue, CommandIndex As Integer) As CommandReturnedValue
Console.WriteLine("Print Command: """ & AllArgs(0).Value & """")
Return AllArgs(0)
End Function
End Class
English is not my mother tongue; please excuse any errors on my part.
(by swam、Green Fire)