Улучшите способ передачи аргументов командной строки отладки
Я мое консольное приложение, я передаю аргументы, как это:
#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
Но это имеет большую очевидную проблему, символ пробела будет выдавать ложные положительные аргументы, например:
MyProgram.exe /Run="Process path with spaces.exe"
Все мы знаем, что обычно аргументы разделяются токенами, разделенными двойными кавычками" "
символы илиspace
char, так что я получу много ложных срабатываний, настраивая мои аргументы отладки.
ВC#
или жеVBNET
Как я могу улучшить функцию, чтобы получить список (пользовательских) аргументов правильно разделены?
ОБНОВЛЕНИЕ 2:
Я сделал этот пример, чтобы попытаться уточнить мои намерения:
Module Module1
'''
''' Debug commandline arguments for testing.
'''
Private ReadOnly DebugArguments As String =
"ThisProcess.exe /Switch1=Value /Switch2=""C:\folder with spaces\file.txt"""
'''
''' 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.
'''
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