在vb.net中开发命令阅读时需要帮助 [英] Need help with developing command reading in vb.net

查看:166
本文介绍了在vb.net中开发命令阅读时需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我正在制作一个程序,该程序具有使用参数执行的命令.
假设有人输入:

写你好世界"

现在,我无法让它处理引号,但只能让它处理空格.因此,它将仅按空格分割命令.但是我也需要它来允许引用.谁能帮我?谢谢.
这是我到目前为止的代码:

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:

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

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


但这会给我一个消息框,并带有你好,因为它已被空格分隔.有人可以帮我弄清楚它吗?
谢谢,
iProgramIt

P.S.尽量不要质疑我为什么这样做;这对我的兄弟来说是个惊喜(他热衷于学习!)


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!)

推荐答案

分隔空格不是一个好主意:您将获得三个字符串.
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!


如果您将所有Arguments循环成这样,则您的解析器将可以工作:
Your parser would work if you will get ALL Arguments in loop like this:
Case "write"
              Dim sAllArgs As String = ""
              For Each arg In Arguments
                  sAllArgs = sAllArgs + arg + " "
              Next
              MsgBox(sAllArgs)



现在您的课程如下所示:



Now your class looks like this:

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:

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?


这篇关于在vb.net中开发命令阅读时需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆