提高了我传递的调试命令行参数的方式 [英] Improve the way that I'm passing debug commandline arguments

查看:227
本文介绍了提高了我传递的调试命令行参数的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林我的控制​​台应用程序我传递的参数是这样的:

  #REGIONDEBUG命令行参数

    专用功能Set_CommandLine_Arguments()方式列表(串)

#如果DEBUG然后,
        这个应用程序的调试命令行参数:
        昏暗的DebugArguments =HotkeyMaker.exe /热键=逃生/run=notepad.exe
        返回DebugArguments.Split().ToList
#其他
        NOMAL命令行参数:
        返回My.Application.CommandLineArgs.ToList
#END如果

    端功能

#END地区
 

不过,有一个大的明显的问题,空格字符会产生假阳性的参数,例如:

  MyProgram.exe /运行=处理路径与spaces.exe
 

我们所知道的,作为正常参数是分开的标记通过封闭双引号delimitted 字符或空格字符,所以我会得到很多误报定制我的调试参数。

C# VBNET 我怎么能提高函数来获得(自定义)的论点正确分隔的一个列表?

  

更新2:

我在这个例子中,试图澄清我的意图:

 模块模块1

    '''<总结>
    '''调试命令行参数进行测试。
    '''< /总结>
    私人只读DebugArguments作为字符串=
    ThisProcess.exe /交换机1 =价值/交换机2 =C:用空格\文件夹\ file.txt的

    '''<总结>
    '''这里将被储存在此应用程序的命令行参数。
    '''如果DebugArguments变量什么然后将它们直接从控制台通过了正常的参数这里使用
    '''。否则,我的自定义调试参数使用。
    '''< /总结>
    私人参数作为列表(字符串)= Set_CommandLine_Arguments()

    副主()
        Parse_Arguments()
    结束小组

    私人小组Parse_Arguments()

        对于每个ARG作为字符串参数

            MSGBOX(ARG)
            ' 结果:
            ------
            第一ARG:ThisProcess.exe
            第二ARG:/交换机1 =值
            第三个参数:/开关2 =C:\文件夹
            第四ARG:有
            5 ARG:空格\ file.txt的

        接下来精氨酸

        预计参数:
        ------------------
        第一ARG:ThisProcess.exe
        第二ARG:/交换机1 =值
        第三个参数:/开关2 =C:\用空格\ file.txt的文件夹

    结束小组

    公共功能Set_CommandLine_Arguments()方式列表(串)

#如果DEBUG然后,

    如果不String.IsNullOrEmpty(DebugArguments)然后
        Retun自定义的参数。
        返回DebugArguments.Split.ToList
    其他
        返回直接从控制台通过正常的命令行参数。
        返回My.Application.CommandLineArgs.ToList
    结束如果

#其他

        返回直接从控制台通过正常的命令行参数。
        返回My.Application.CommandLineArgs.ToList

#END如果

    端功能

前端模块
 

解决方案

我想可能是你要管理的调试命令行的方式。

如果你去到项目属性 - >调试,在启动选项可以设置起始命令行。如果你也逃脱文件/路径名用引号(如​​Windows不会在注册表和快捷方式路径),参数/路径将保持不变。例如:

/热键=退出/运行=\ Program Files文件\文件夹名称\ NOTEPAD.EXE

有多种方式来获取命令行参数,但净利润将解析命令行给你。二,将工作在任何地方有:

  myCmd = Environment.CommandLine
的args = Environment.GetCommandLineArgs()
 

第一个将返回完整的命令行包括当前应用作为第一链段的名称。 GetCommandLineArgs 将解析命令行成一个字符串数组,再次的args(0)是当前可执行文件的名称。

  vbArgs = My.Application.CommandLineArgs
 

这是略有不同,只有参数返回(没有exe文件名)和返回是一个 ReadOnlyCollection还(字符串)

还有另一种方式,让人联想到DOS,对过去的日子的,这是非常有用的:

 公用Sub主要(参数为String())
 

而不是开始从主窗体的应用程序,在属性 - >应用程序,设置它的启动对象的子主,然后创建一个模块的子如上。如果添加一个字符串数组参数,NET将解析命令行,跳过可执行文件的名称,并通过你的参数到数组中。我个人preFER这一点,因为我很少关心可执行文件的名称。

  

这可以是特别有用的在这种情况下,因为如果有一个   命令行,你可能不希望显示窗体或启动   消息泵。即使采用了直板的WinForm程序,开机顺序   事情可以是不同的或有额外的步骤时,传递要打开的文件或什么的。

您可以通过为在一开始就说明设置起始命令行得到相同的一组在VS / VB参数的个数。所有GetCommandArgs变化将返回在IDE中相同的方式为在运行时(除了那些包括EXE文件的名称,将报告 ... myApp.vshost.exe 在IDE)。

示例

使用这个命令行的VS项目属性:

/热键=退出/运行=\ Program Files文件\文件夹名称\ NOTEPAD.EXE/ ARG =这是一个ARG

(换行符肯定扭曲的东西在这里)

 公用Sub主要(参数为String())
    对于j为整数= 0要args.Length  -  1
        Console.WriteLine(ARG {0} == {1},J,ARGS(J))
    下一个
结束小组
 

输出:

  

阿根廷0 == /热键=逃生
  ARG 1 == / RUN = \ Program Files文件\文件夹名称\ NOTEPAD.EXE
  ARG 2 == / ARG =这是一个ARG

空格是preserved第二ARG(1),因为他们逃脱了引号,因为用户将不得不这样做 - 引号也将被删除。将它们复制到进行处理或其他应用程序需要做一个列表。

您会得到相同的实参同一个地方以同样的方式是否是运行时或在没有IDE中做不同的。

Im my console application I pass the arguments like this:

#Region " DEBUG CommandLine Arguments "

    Private Function Set_CommandLine_Arguments() As List(Of String)

#If DEBUG Then
        ' Debug Commandline arguments for this application:
        Dim DebugArguments = "HotkeyMaker.exe /Hotkey=Escape /run=notepad.exe"
        Return DebugArguments.Split(" ").ToList
#Else
        ' Nomal Commandline arguments:
        Return My.Application.CommandLineArgs.ToList
#End If

    End Function

#End Region

But that has a big obvious problem, the space character will produce false positive arguments, for example:

MyProgram.exe /Run="Process path with spaces.exe"

All we know that as normally the arguments are separated in tokens delimitted by enclosed double quotes " " chars or a space char, so I will get a lot of false positives customizing my debug arguments.

In C# or VBNET how I can improve the function to obtain a list of (custom) arguments correctly separated?

UPDATE 2:

I made this example to try to clarify my intentions:

Module Module1

    ''' <summary>
    ''' Debug commandline arguments for testing.
    ''' </summary>
    Private ReadOnly DebugArguments As String =
    "ThisProcess.exe /Switch1=Value /Switch2=""C:\folder with spaces\file.txt"""

    ''' <summary>
    ''' Here will be stored the commandline arguments of this application.
    ''' If DebugArguments variable is nothing then the "normal" arguments which are passed directly from the console are used here,
    ''' Otherwise, my custom debug arguments are used.
    ''' </summary>
    Private Arguments As List(Of String) = Set_CommandLine_Arguments()

    Sub Main()
        Parse_Arguments()
    End Sub

    Private Sub Parse_Arguments()

        For Each Arg As String In Arguments

            MsgBox(Arg)
            ' Result:
            ' ------
            ' 1st arg: ThisProcess.exe
            ' 2nd arg: /Switch1=Value
            ' 3rd arg: /Switch2="C:\folder
            ' 4th arg: with
            ' 5th arg: spaces\file.txt"

        Next Arg

        ' Expected arguments:
        ' ------------------
        ' 1st arg: ThisProcess.exe
        ' 2nd arg: /Switch1=Value
        ' 3rd arg: /Switch2="C:\folder with spaces\file.txt"

    End Sub

    Public Function Set_CommandLine_Arguments() As List(Of String)

#If DEBUG Then

    If Not String.IsNullOrEmpty(DebugArguments) Then
        ' Retun the custom arguments.
        Return DebugArguments.Split.ToList
    Else
        ' Return the normal commandline arguments passed directly from the console.
        Return My.Application.CommandLineArgs.ToList
    End If

#Else

        ' Return the normal commandline arguments passed directly from the console.
        Return My.Application.CommandLineArgs.ToList

#End If

    End Function

End Module

解决方案

I think it might be the way you are trying to manage the debug command line.

If you go to Project Properties -> Debug, under start options you can set a starting command line. If you also escape file/path names with quotes (like Windows does in the registry and for shortcut paths), the arguments/path will remain intact. For instance:

/Hotkey=Escape /run="\Program Files\folder name\notepad.exe"

There are multiple ways to get the command line arguments, but NET will parse the commandline for you. Two that will work anywhere are:

myCmd = Environment.CommandLine
args = Environment.GetCommandLineArgs() 

The first will return the full commandline including the name of the current app as the first segment. GetCommandLineArgs will parse the commandline into a string array, again with args(0) being the name of the current executable.

vbArgs = My.Application.CommandLineArgs

This is slightly different, with just the arguments returned (no exe name) and the return is a ReadOnlyCollection(of String)

There is still another way, reminiscent of the bygone days of DOS, which is very useful:

Public Sub Main(args As String())

Rather than start the app from a main form, in Properties -> Application, set it the start up object as Sub Main, then create a sub in a module as above. If you add a string array parameter, NET will parse the commandline, skip the executable name and pass you the arguments in the array. I personally prefer this because I rarely care about the executable name.

This may be particularly useful in this case because if there is a commandline, you probably do not want to show a Form or start a message pump. Even with a straight WinForm app, the start up order of things can be different or include extra steps when passed a file to open or whatever.

You can get the same set of args in VS/VB by setting the starting commandline as described at the outset. All of the GetCommandArgs variations will return the same info in the IDE as at runtime (with the exception that those which include the EXE name, which will report ...myApp.vshost.exe in the IDE).

Example

Using this commandline in VS project properties:

/Hotkey=Escape /run="\Program Files\folder name\notepad.exe" "/arg=this is an arg"

(Line breaks surely distort things here)

Public Sub Main(args As String())
    For j As Integer = 0 To args.Length - 1
        Console.WriteLine("arg {0} == {1}", j, args(j))
    Next
End Sub

Output:

arg 0 == /Hotkey=Escape
arg 1 == /run=\Program Files\folder name\notepad.exe
arg 2 == /arg=this is an arg

Spaces are preserved in the second arg(1), because they were escaped with quotes as the user will have to do - the quotes are also removed. Copy them to a list for processing or whatever the app needs to do.

You get the same args the same way from the same place whether it is runtime or in the IDE with nothing to do differently.

这篇关于提高了我传递的调试命令行参数的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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