2012-11-19 7 views
5

コマンドラインでプログラムを開くときに、複数の名前付きパラメータを受け入れる機能をプログラムに追加する必要があります。すなわち複数の名前付きコマンドラインパラメータを解析する

program.exe /param1=value /param2=value 

となり、これらのパラメータをプログラムの変数として利用できるようになります。私はこれを達成するためにいくつかの方法を見つけましたが、それをすべて一緒にする方法を理解できていないようです。

以下のコードを使用して名前付きパラメータを1つ渡してリカバリすることができました。可能なすべての名前付きパラメータに対して複製することができましたが、これを行う方法としてはあてはまらないことがわかりました。

Dim inputArgument As String = "/input=" 
    Dim inputName As String = "" 

    For Each s As String In My.Application.CommandLineArgs 
     If s.ToLower.StartsWith(inputArgument) Then 
      inputName = s.Remove(0, inputArgument.Length) 
     End If 
    Next 

代わりに、私は

My.Application.CommandLineArgs 

を使用して、コマンドラインから複数の無名のパラメータを取得することができます。しかし、これはパラメータのすべてが同じ順序/形式で毎回渡されている必要があります。私は毎回パラメータのランダムなサブセットを渡すことができる必要があります。

最終的に、私ができることを望むのは、各引数と値を分けて、後で使用するために多次元配列にロードすることです。私は、 "="で文字列を区切り、 "/"を取り除くことでこれを行う方法を見つけることができることを知っていますが、私はこれにいくぶん新しいですから、 "preffered"複数の名前付きパラメータを使用しますか?

答えて

6

これを処理することの好みは、Command Line Parser Libraryのような既存のライブラリを使用することです。 (ただし、デフォルトでは、それは--input=Value代わりの/input=valueをベースに、different input formatを使用しています。)

これはあなたに、自分でコードを書くことの柔軟性と堅牢性の多くを取得し、あなたのコードを簡素化しないという利点を提供します。

+0

これはまさに私が探していたものです。そして、私は実際にどのような入力形式が使われているか気にしません...私はまだ何も配布していません。クイックヘルプに感謝します。 – scholzr

1

ここでは、やりたいことをするための小さな機能があります。構造体に名前と値のペアですべてのパラメータを格納することができます。

Module Module1 
Private Structure NameCommandLineStuct 
    Dim Name As String 
    Dim Value As String 
End Structure 
Private CommandLineArgs As New List(Of NameCommandLineStuct) 

Sub Main() 
    If ParseCommandLine() Then 
     For Each commandItem As NameCommandLineStuct In CommandLineArgs 
      Select Case commandItem.Name.ToLower 
       Case "one" 
        Console.Write(String.Format("key one is {0}", commandItem.Value)) 
       Case "two" 
        Console.Write(String.Format("key two is {0}", commandItem.Value)) 
      End Select 
     Next 
    End If 
End Sub 
Function ParseCommandLine() As Boolean 
    'step one, Do we have a command line? 
    If String.IsNullOrEmpty(Command) Then 
     'give up if we don't 
     Return False 
    End If 

    'does the command line have at least one named parameter? 
    If Not Command.Contains("/") Then 
     'give up if we don't 
     Return False 
    End If 
    'Split the command line on our slashes. 
    Dim Params As String() = Split(Command, "/") 

    'Iterate through the parameters passed 
    For Each arg As String In Params 
     'only process if the argument is not empty 
     If Not String.IsNullOrEmpty(arg) Then 
      'and contains an equal 
      If arg.Contains("=") Then 

       Dim tmp As NameCommandLineStuct 
       'find the equal sign 
       Dim idx As Integer = arg.IndexOf("=") 
       'if the equal isn't at the end of the string 
       If idx < arg.Length - 1 Then 
        'parse the name value pair 
        tmp.Name = arg.Substring(0, idx).Trim() 
        tmp.Value = arg.Substring(idx + 1).Trim() 
        'add it to the list. 
        CommandLineArgs.Add(tmp) 
       End If 
      End If 
     End If 

    Next 
    Return True 
End Function 
End Module 
関連する問題