Startsidan  ▸  Texter  ▸  Teknikblogg

Anders Hesselbom

Programmerare, skeptiker, sekulärhumanist, antirasist.
Författare till bok om C64 och senbliven lantis.
Röstar pirat.

Easy handeling of command line arguments

2010-04-01

The CommandLineArguments class is a useful tool when you are building an application that accepts different arguments from the command line, and argument with parameters. The class lets you check if the program was started using a specific argument, and also read the argument to the right, as if it was a parameter.

The ArgumentExists function checks for the presence of an argument and the GetIndexOfArgument function gives you the index of an argument, or negative one if not present. The GetArgumentParameter returns the argument next to a given argument.

Let’s say that you are writing an application called test.exe, and it is started like this:

test.exe -f hello -qq "hello again"

If you pass “-qq” to the GetArgumentParameter function, it will return “hello again“. If you pass “-f” to the ArgumentExists function, it returns true. And if you pass “yeah” to the GetIndexOfArgument function, it returns -1.

Example:

Dim Args As New CommandLineArguments()
Console.WriteLine("Param: " & Args.ArgumentExists("-f").ToString())
Console.ReadLine()

This is the source code for the class:

Public Class CommandLineArguments
    Inherits CollectionBase

    Private mIgnoreCase As Boolean

    Public Sub New()
        Me.New(True)
    End Sub

    Public Sub New(ByVal IgnoreCase As Boolean)
        Dim S() As String = System.Environment.GetCommandLineArgs()
        If S.Length > 1 Then
            For I As Integer = 1 To S.Length - 1
                List.Add(S(I))
            Next
        End If
        Me.mIgnoreCase = IgnoreCase
    End Sub

    Default Public ReadOnly Property Item(ByVal Index As Integer) As String
        Get
            Return CType(List(Index), String)
        End Get
    End Property

    Public Function ArgumentExists(ByVal Arg As String) As Boolean
        For Each S As String In Me
            If String.Compare(S, Arg, Me.mIgnoreCase) = 0 Then
                Return True
            End If
        Next
        Return False
    End Function

    Public Function GetIndexOfArgument(ByVal Arg As String) As Integer
        If Me.Count > 0 Then
            For I As Integer = 0 To Me.Count - 1
                If String.Compare(Me(I), Arg, Me.mIgnoreCase) = 0 Then
                    Return I
                End If
            Next
            Return -1
        Else
            Return -1
        End If
    End Function

    Public Function GetArgumentParameter(ByVal Arg As String) As String
        Dim Index As Integer = Me.GetIndexOfArgument(Arg)
        If Index > -1 Then
            If Index < (Me.Count - 1) Then
                Return Me(Index + 1)
            Else
                Return ""
            End If
        Else
            Return ""
        End If
    End Function

End Class

Categories: Visual Basic 9

2 responses to “Easy handeling of command line arguments”

  1. […] Easy handeling of command line arguments – WinSoft.se […]

  2. […] This post was mentioned on Twitter by Anders Hesselbom. Anders Hesselbom said: New blog post: Easy handeling of command line arguments http://www.winsoft.se/2010/04/easy-handeling-of-command-line-arguments/ […]

Leave a Reply

Your email address will not be published. Required fields are marked *



En kopp kaffe!

Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll!

Bjud på en kopp kaffe!

Om...

Kontaktuppgifter, med mera, finns här.

Följ mig

Twitter Instagram
GitHub RSS

Public Service

Folkbildning om public service.

Hem   |   linktr.ee/hesselbom   |   winsoft.se   |   80tal.se   |   Filmtips