Click here to Skip to main content
15,868,340 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi guys,
I am making a program that has commands which are executed with arguments.
Say if someone types:

write "hello world"

Now, I cannot get it to handle quotes, but I can only let it handle spaces. So it will only split commands by the spaces. But I also need it to allow quotes. Can anyone help me? Thanks.
Here is the code I have so far:
VB
Public Class Command
    Private _cmd As String
    Private _args As String()
    Public Property Arguments As String()
        Get
            Return _args
        End Get
        Set(ByVal value As String())
            _args = value
        End Set
    End Property
    Public ReadOnly Property CommandName As String
        Get
            Return CMD.Split(" ")(0)
        End Get
    End Property
    Public Property CMD As String
        Get
            Return _cmd
        End Get
        Set(ByVal value As String)
            Dim args(_cmd.Split(" ").Count - 1) As String
            Dim i As Integer = 0
            For Each arg As String In _cmd.Split(" ")
                If i > 0 Then
                    args(i - 1) = arg
                End If
                i += 1
            Next
            _args = args
            _cmd = value
        End Set
    End Property
    Public Sub New(ByVal xcmd As String)
        _cmd = xcmd
    End Sub
    Public Sub ActivateCommand()
        Select Case CommandName.ToLower
            Case "write"
                MsgBox(Arguments(0))
        End Select
    End Sub
End Class


I declare a command with
VB
Dim cmd As New Command("write ""Hello World""")
cmd.ActivateCommand()

But it gives me as message box with "Hello, because it has been split by spaces. Can someone help me get it right?
Thanks,
iProgramIt

P.S. Try not to question why I am doing this; it is for a surprise for my brother (he's keen to learn!)
Posted
Comments
Richard MacCutchan 1-Oct-15 7:12am    
You just need to add some logic into your parser, so that when it sees a quote character it ignores all spaces until it sees the next quote.

Splitting on spaces is not a good idea: you will get three strings.
write
"Hello
World"
And then you have to try to work out what the heck is going on.
And things get even worse when you start to allow operators:
write "hello" + "world"
Isn't too bad, but
write "hello"+"world" 
is only two strings, and that makes your life really difficult.

I'd start by looking at parsers and tokenizers[^]: they will be harder to get started with, but will save you a huge amount of time very, very quickly!
 
Share this answer
 
Your parser would work if you will get ALL Arguments in loop like this:
VB
Case "write"
              Dim sAllArgs As String = ""
              For Each arg In Arguments
                  sAllArgs = sAllArgs + arg + " "
              Next
              MsgBox(sAllArgs)


Now your class looks like this:

VB
Public Class Command
        Private _cmd As String
        Private _args As String()
        Public Property Arguments As String()
            Get
                Return _args
            End Get
            Set(ByVal value As String())
                _args = value
            End Set
        End Property

        Public ReadOnly Property CommandName As String
            Get
                Return CMD.Split(" ")(0)
            End Get
        End Property

        Public Property CMD As String
            Get
                Return _cmd
            End Get
            Set(ByVal value As String)
                Dim args(_cmd.Split(" ").Count - 1) As String
                Dim i As Integer = 0
                For Each arg As String In _cmd.Split(" ")
                    If i > 0 Then
                        args(i - 1) = arg
                    End If
                    i += 1
                Next
                _args = args
                _cmd = value
            End Set
        End Property

        Public Sub New(ByVal xcmd As String)
            _cmd = xcmd
            CMD = xcmd
        End Sub

        Public Sub ActivateCommand()
            Select Case CommandName.ToLower
                Case "write"
                    Dim sAllArgs As String = ""
                    For Each arg In Arguments
                        sAllArgs = sAllArgs + arg + " "
                    Next
                    MsgBox(sAllArgs)
            End Select
        End Sub
    End Class


And the command simply:

VB
Dim cmd As New Command(Dim cmd As New Command("write Hello World"))
cmd.ActivateCommand()


This seems to be what you want, don't you?
 
Share this answer
 
v5
Comments
SmokeHead 1-Oct-15 7:40am    
Nice, +5 !
Leo Chapiro 1-Oct-15 7:41am    
Thanks! :)
iProgramIt 1-Oct-15 23:21pm    
This looks very good! Thank you so much! Is there a way to include quotes though in other commands, otherwise it could get confused..?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900