2011-07-13 47 views
1

我看到很多程序都帶有帶標誌的命令行參數,例如gcc hello.c -o hello。當然,我可以實現我的應用程序:命令行參數的可靠模式

Dim args() As String = Environment.GetCommandLineArgs() 
Dim oi As Integer = Array.IndexOf("-o", args) 
If oi > -1 AndAlso oi < args.Length Then 
    CompileTo(args(oi + 1)) 'Or whatever 
Else 
    CompileTo("out.exe") 'Or whatever 
End If 

但它的醜陋,令人討厭的使用,容易出錯,和低效。有什麼更好的方式,我一直忽略?

回答

1

在許多(大多數?)Linux程序中,這些選項由getopt處理。這是一個相當通用的系統,因此它可能是啓動並獲得靈感的一個好點。

3

你可以看看Best way to parse command line arguments in C#?,有關於類似主題(C#的一些有趣的答案,但這應該不重要)。它還顯示了您可能要使用的「即用型」庫的鏈接。

+0

+1看起來不錯,非常靈活! – fvu

+1

看起來不錯!我認爲這對我的應用程序來說可能有點多。 +1 – Ryan

-1

創建一個 形式 - 文本框命名OptString和文字:-in 「C:\我的文檔\我的文件」 + N -sort:一個-xyz - 命名痛擊文本框(簡稱錯誤選項) - 命名ParseBt 按鈕 - 名爲RTB的RichTextBox(簡稱RichTextBox中)

添加以下代碼並運行它:

Public Class Form1 
Dim ListOfPossibleOptions As New Collection 

Dim RejectedOptions As String 

Private Sub ParseBt_Click(sender As System.Object, e As System.EventArgs) Handles ParseBt.Click 
    ListOfPossibleOptions.Clear() 
    ListOfPossibleOptions.Add(New OneOption("Input", " "c, True)) 
    ListOfPossibleOptions.Add(New OneOption("Sort", ":"c)) 
    ListOfPossibleOptions.Add(New OneOption("New")) 

    ParseOptions(ListOfPossibleOptions, RejectedOptions) 

    Dim s As String = "" 
    For Each CurrOption As OneOption In ListOfPossibleOptions 
     s &= CurrOption.OptionText & ": " 
     If CurrOption.Present Then 
      s &= "Present option=" & CurrOption.OptionChar 
      If CurrOption.ValueReq Then 
       s &= " value=" & CurrOption.TextValue 
      End If 
     Else 
      s &= "not present" 
     End If 
     s &= vbCrLf 
    Next 
    rtb.Text = s 
End Sub 
Sub ParseOptions(ListOfPossibleOptions As Collection, RejectedOptions As String) 
    Dim Fnd As Boolean 
    Dim OptionString As String = OptString.Text.Trim.ToUpper & " " 
    Dim OptionFnd As OneOption 
    RejectedOptions = "" 
    While OptionString.Length() > 0 
     Fnd = False 
     Do While True 
      For Each CurrOption As OneOption In ListOfPossibleOptions 
       Dim ot As String = CurrOption.OptionText.ToUpper 
       For l As Integer = 1 To CurrOption.OptionText.Length 
        If l < OptionString.Length Then 
         Debug.WriteLine("|" & ot.Substring(0, l) & "|" & OptionString.Substring(1, l) & "|") 
         If ot.Substring(0, l) = OptionString.Substring(1, l) Then 
          If OptionString.Substring(1 + l, 1) = CurrOption.Separator Then 
           Fnd = True 
           OptionFnd = CurrOption 
           OptionFnd.Present = True 
           OptionFnd.OptionChar = OptionString(0) 
           OptionString = OptionString.Substring(l + 2) 
           Exit Do ' found 
          End If 
         Else 
          Exit For ' next possible option 
         End If 
        End If 
       Next 
      Next 
      Exit Do ' not found 
     Loop 
     If Not Fnd Then 
      Dim i As Integer = OptionString.IndexOf(" ") 
      RejectedOptions &= OptionString.Substring(0, i) & " " 
      OptionString = OptionString.Substring(i).TrimStart 
     Else 
      If OptionFnd.ValueReq Then 
       If OptionFnd.Quoted Then 
        If OptionString(0) = """" Then 
         OptionString = OptionString.Substring(1) 
         Dim i As Integer = OptionString.IndexOf("""") 
         If i > -1 Then 
          OptionFnd.TextValue = OptionString.Substring(0, i) 
          OptionString = OptionString.Substring(i + 1).TrimStart 
         Else ' closing quote missing 
          i = OptionString.IndexOf(" ") 
          OptionFnd.TextValue = """" & OptionString.Substring(0, i) 
          OptionString = OptionString.Substring(i).TrimStart 
         End If 
        Else ' not quoted 
         Dim i As Integer = OptionString.IndexOf(" ") 
         OptionFnd.TextValue = OptionString.Substring(0, i) 
         OptionString = OptionString.Substring(i).TrimStart 
        End If 
       Else ' never quoted 
        Dim i As Integer = OptionString.IndexOf(" ") 
        OptionFnd.TextValue = OptionString.Substring(0, i) 
        OptionString = OptionString.Substring(i).TrimStart 
       End If 
      End If 
     End If 
    End While 
    wop.Text = RejectedOptions 
End Sub 
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 
End Sub 
End Class 
Public Class OneOption 
'input 
Friend OptionText As String ' required 
Friend Separator As Char ' if specified, CurrOption value follows the option, like : in -SORT:A 
Friend Quoted As Boolean ' value may be "q u o t e d" 
'work 
Friend ValueReq As Boolean ' is value required (as seperator was given in NEW)? 
'output 
Friend Present As Boolean ' option present 
Friend OptionChar As Char ' options was preceeded by + -/whatever 
Friend TextValue As String ' the value following the option 
Sub New(pOptionText As String) ' option without a value, a switch 
    OptionText = pOptionText 
    reset() 
End Sub 
Sub New(pOptionText As String, pSeparator As Char) ' option with a value, the separator between option and value has to be specified 
    OptionText = pOptionText 
    reset() 
    Separator = pSeparator 
    ValueReq = True 
    Quoted = False 
End Sub 
Sub New(pOptionText As String, pSeparator As Char, pQuoted As Boolean) ' option with a possibly "quoted value", the separator between option and value has to be specified 
    OptionText = pOptionText 
    reset() 
    Separator = pSeparator 
    ValueReq = True 
    Quoted = pQuoted 
End Sub 
Sub reset() 
    Separator = " "c 
    Quoted = False 
    Present = False 
    ValueReq = False 
End Sub 
End Class 
+0

我在一個控制檯應用程序中,Windows窗體演示是......不相關的。我認爲我理解你的代碼,但是它有點大,寫得不是很好......(例如,對於我的情況,請勿使用「Do While True ... Loop」)。 – Ryan

+0

在拒絕之前查看解決方案!例程ParseOptions在控制檯環境中完成所有你想要的,表單僅用於運行和演示它。下次好運。 – Martin

+0

嗯,是的 - 但你並不需要混亂它才能使它工作。然後是過於複雜的字符串搜索算法。我已經有了答案。 – Ryan