命令行参数的可靠模式

时间:2011-07-13 21:27:41

标签: .net vb.net command-line-arguments

我看到很多程序都使用带有标志的命令行参数,例如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

但它使用起来很丑陋,容易出错,而且效率低下。我一直在俯视的更好的方式是什么?

3 个答案:

答案 0 :(得分:3)

请你查看Best way to parse command line arguments in C#?,关于类似主题有一些有趣的答案(C#,但这不重要)。它还显示了您可能想要使用的“即用型”库的链接。

答案 1 :(得分:1)

在许多(大多数?)Linux程序中,这些选项由getopt处理。这是一个非常通用的系统,因此开始并获得灵感可能是一个好点。

答案 2 :(得分:-1)

使用。创建表单 - 名为OptString和text的文本框:-in“c:\ my docs \ my files”+ n -sort:a -xyz - 名为wop的文本框(错误选项的缩写) - 名为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