Verbessern Sie die Art und Weise, wie Debug-Befehlszeilenargumente übergeben werden
In meiner Konsolenanwendung gebe ich die folgenden Argumente weiter:
#Region " DEBUG CommandLine Arguments "
Private Function Set_CommandLine_Arguments() As List(Of String)
#If DEBUG Then
' Debug Commandline arguments for this application:
Dim DebugArguments = "HotkeyMaker.exe /Hotkey=Escape /run=notepad.exe"
Return DebugArguments.Split(" ").ToList
#Else
' Nomal Commandline arguments:
Return My.Application.CommandLineArgs.ToList
#End If
End Function
#End Region
Das hat aber ein großes offensichtliches Problem: Das Leerzeichen erzeugt falsch positive Argumente, zum Beispiel:
MyProgram.exe /Run="Process path with spaces.exe"
Alles, was wir wissen, ist, dass die Argumente normalerweise in Token getrennt sind, die durch doppelte Anführungszeichen getrennt sind" "
Zeichen oder aspace
char, so dass ich viele False Positives bekomme, wenn ich meine Debug-Argumente anpasse.
ImC#
oderVBNET
Wie kann ich die Funktion verbessern, um eine Liste der (benutzerdefinierten) Argumente korrekt getrennt zu erhalten?
UPDATE 2:
Ich habe dieses Beispiel gemacht, um zu versuchen, meine Absichten zu verdeutlichen:
Module Module1
''' <summary>
''' Debug commandline arguments for testing.
''' </summary>
Private ReadOnly DebugArguments As String =
"ThisProcess.exe /Switch1=Value /Switch2=""C:\folder with spaces\file.txt"""
''' <summary>
''' Here will be stored the commandline arguments of this application.
''' If DebugArguments variable is nothing then the "normal" arguments which are passed directly from the console are used here,
''' Otherwise, my custom debug arguments are used.
''' </summary>
Private Arguments As List(Of String) = Set_CommandLine_Arguments()
Sub Main()
Parse_Arguments()
End Sub
Private Sub Parse_Arguments()
For Each Arg As String In Arguments
MsgBox(Arg)
' Result:
' ------
' 1st arg: ThisProcess.exe
' 2nd arg: /Switch1=Value
' 3rd arg: /Switch2="C:\folder
' 4th arg: with
' 5th arg: spaces\file.txt"
Next Arg
' Expected arguments:
' ------------------
' 1st arg: ThisProcess.exe
' 2nd arg: /Switch1=Value
' 3rd arg: /Switch2="C:\folder with spaces\file.txt"
End Sub
Public Function Set_CommandLine_Arguments() As List(Of String)
#If DEBUG Then
If Not String.IsNullOrEmpty(DebugArguments) Then
' Retun the custom arguments.
Return DebugArguments.Split.ToList
Else
' Return the normal commandline arguments passed directly from the console.
Return My.Application.CommandLineArgs.ToList
End If
#Else
' Return the normal commandline arguments passed directly from the console.
Return My.Application.CommandLineArgs.ToList
#End If
End Function
End Module