[VB.net] Commandline arguments

Pagina: 1
Acties:
  • 121 views sinds 30-01-2008
  • Reageer

  • PolarBear
  • Registratie: Februari 2001
  • Niet online
  • Geef eerst globaal aan wat het probleem is
VB console applicatie (doel doet niet terzake). Enkele parameters moeten (althans dat is de meest elegante oplossing) kunnen worden opgegeven via command line.
Gebruike taal: Visual Basic.Net
  • Geef vervolgens aan waar je denkt dat het aan ligt
Voorzover ik kan nagaan kan de commandline parameter worden uitgelezen met de onderstaande code.
Visual Basic:
1
Microsoft.VisualBasic.Command()


Voorbeeld code volgens Microsoft:
code:
1
2
3
4
5
6
7
Function GetCommandLineArgs() As String()
   ' Declare variables.
   Dim separators As String = " "
   Dim commands As String = Microsoft.VisualBasic.Command()
   Dim args() As String = commands.Split(separators.ToCharArray)
   Return args
End Function

Deze functie retourneert dus een string array (?) maar checkt niets qua inhoud.

Mijn code met RegExp
code:
1
2
Dim regExpPath As New System.Text.RegularExpressions.Regex("(/path:)(\w\x3A){*}\s")
Console.WriteLine(regExpPath.Match(Microsoft.VisualBasic.Command).ToString)

Met deze code hoop ik van de ingetypte Bot.exe /path:c: om te bouwen naar /path:c: (mooier zou zijn dat ik alleen c: over hou maar ik heb geen idee hoe dat kan).
  • Geef vervolgens aan wat je al geprobeerd hebt.
Ik heb al wat uurtjes met RegExp lopen klooien ik denk en hoop dat daar het probleem in zit. Ik ben al een tijdje met http://regexlib.com/RETester.aspx de regular expression proberen aan te passen maar dat werkt ook niet helemaal naar wens.
  • Geef altijd aan wat voor eventuele foutmeldingen of waarschuwingen je krijgt.
Geen foutmeldingen, gewoon weg niet de gewenst output.
  • Bonus vraag
Is dit uberhaupt wel een goede manier om commandline arguments te gebruiken ?

Dit is nog puur development, dus enige foutafhandeling etc ontbreekt

  • gorgi_19
  • Registratie: Mei 2002
  • Laatst online: 26-05 17:50

gorgi_19

Kruimeltjes zijn weer op :9

* gorgi_19 gaat voor de bonus vraag... :P

Waarom pak je niet Environment.Commandline?

Digitaal onderwijsmateriaal, leermateriaal voor hbo


  • PolarBear
  • Registratie: Februari 2001
  • Niet online
Omdat het zoeken in de help functie van Visual Studio niet verder kwam dan die oude VB functie. :?

Overigens ziet deze functie er inderdaad een stuk beter uit, ik vraag me alleen nog steeds af hoe het beste de verschillende opties gesplits kunnen worden.
code:
1
2
      Dim arguments As [String]() = Environment.GetCommandLineArgs()
      Console.WriteLine("GetCommandLineArgs: {0}", [String].Join(", ", arguments))

[ Voor 15% gewijzigd door PolarBear op 09-04-2004 22:01 ]


  • PolarBear
  • Registratie: Februari 2001
  • Niet online
Ter informatie ik heb het opgelost.
Met hulp van de volgende site's:
http://www.codeproject.com/csharp/Command_Line.asp
http://www.eggheadcafe.com/articles/cstovbweb/converter.aspx

Visual Basic:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
Imports System
Imports System.Collections.Specialized
Imports System.Text.RegularExpressions


Public Class Arguments
    Private Parameters As StringDictionary

    Public Sub New(ByVal Args() As String)
        Parameters = New StringDictionary()
        Dim Spliter As New Regex("^-{1,2}|^/|=|:", RegexOptions.IgnoreCase And RegexOptions.Compiled)
        Dim Remover As New Regex("^['""]?(.*?)['""]?$", RegexOptions.IgnoreCase And RegexOptions.Compiled)
        Dim Parameter As String = Nothing
        Dim Parts() As String

        Dim Txt As String
        For Each Txt In Args
            Parts = Spliter.Split(Txt, 3)

            Select Case Parts.Length
                Case 1
                    If Not (Parameter Is Nothing) Then
                        If Not Parameters.ContainsKey(Parameter) Then
                            Parts(0) = Remover.Replace(Parts(0), "$1")

                            Parameters.Add(Parameter, Parts(0))
                        End If
                        Parameter = Nothing
                    End If

                Case 2
                    If Not (Parameter Is Nothing) Then
                        If Not Parameters.ContainsKey(Parameter) Then
                            Parameters.Add(Parameter, "true")
                        End If
                    End If
                    Parameter = Parts(1)

                Case 3
                    If Not (Parameter Is Nothing) Then
                        If Not Parameters.ContainsKey(Parameter) Then
                            Parameters.Add(Parameter, "true")
                        End If
                    End If
                    Parameter = Parts(1)

                    If Not Parameters.ContainsKey(Parameter) Then
                        Parts(2) = Remover.Replace(Parts(2), "$1")
                        Parameters.Add(Parameter, Parts(2))
                    End If

                    Parameter = Nothing
            End Select
        Next Txt

        If Not (Parameter Is Nothing) Then
            If Not Parameters.ContainsKey(Parameter) Then
                Parameters.Add(Parameter, "true")
            End If
        End If
    End Sub 'New 

    Default Public ReadOnly Property Item(ByVal Param As String) As String
        Get
            Return Parameters(Param)
        End Get
    End Property
End Class 'Arguments


code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
            ' Command line parsing      
            Dim CommandLine As New Arguments(Environment.GetCommandLineArgs())

            ' Look for specific arguments values and display 
            ' them if they exist (return null if they don't)
            If Not (CommandLine("param1") Is Nothing) Then
                Console.WriteLine(("Param1 value: " + CommandLine("param1")))
            Else
                Console.WriteLine("Param1 not defined !")
            End If
            If Not (CommandLine("height") Is Nothing) Then
                Console.WriteLine(("Height value: " + CommandLine("height")))
            Else
                Console.WriteLine("Height not defined !")
            End If
            If Not (CommandLine("width") Is Nothing) Then
                Console.WriteLine(("Width value: " + CommandLine("width")))
            Else
                Console.WriteLine("Width not defined !")
            End If
            If Not (CommandLine("size") Is Nothing) Then
                Console.WriteLine(("Size value: " + CommandLine("size")))
            Else
                Console.WriteLine("Size not defined !")
            End If
            If Not (CommandLine("debug") Is Nothing) Then
                Console.WriteLine(("Debug value: " + CommandLine("debug")))
            Else
                Console.WriteLine("Debug not defined !")
            End If
            ' Wait for key
            Console.Out.WriteLine("Arguments parsed. Press a key")
            Console.Read()

[ Voor 6% gewijzigd door PolarBear op 10-04-2004 19:46 ]