C++ 快速输入命令行参数进行 Visual Studio 调试?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5460270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:13:11  来源:igfitidea点击:

Quickly enter command-line parameters for Visual Studio debugging?

c++visual-studiovisual-studio-2010debugging

提问by paperjam

I want to change my command-line arguments and then debug my executable.

我想更改我的命令行参数,然后调试我的可执行文件。

With the default Visual Studio UI, this takes me several tortuous mouse and keyboard actions:

使用默认的 Visual Studio UI,这需要我进行一些曲折的鼠标和键盘操作:

Project ... right click ... Configuration Properties ... Debugging ... Command Arguments ... type args... ENTER ... F5

项目 ... 右键单击​​ ... 配置属性 ... 调试 ... 命令参数 ...输入 args... ENTER ... F5

Is there a way to make this common action as easy as other common operations, for example, searching all files for a pattern which goes:

有没有办法让这个常见操作像其他常见操作一样简单,例如,搜索所有文件以找到一个模式:

CNTL+SHIFT+F ... type search pattern... ENTER

CNTL+SHIFT+F ...输入搜索模式... ENTER

For example, is there an way to create a custom edit box to allow quick access to the debug command-line arguments? Or a way to have a key-binding pop up a simple "debug dialog" where the args can be entered and debugging started directly? e.g.

例如,有没有办法创建自定义编辑框以允许快速访问调试命令行参数?或者有一种方法让键绑定弹出一个简单的“调试对话框”,可以在其中输入参数并直接开始调试?例如

ALT+F5 ... type args... ENTER

ALT+F5 ...输入参数... 回车

I am using C++ and Visual Studio 2010 Express. Thanks!

我正在使用 C++ 和 Visual Studio 2010 Express。谢谢!

采纳答案by Konstantin Tenzin

Macro below should help. Open "Tools->Macros->Macro Explorer", then create new module, edit it, and copy-paste code below. Required command is SetCommandArgsProperty. UI is not nice, but it works (VS 2005, I hope this will also work in VS 2010). Then add any shortcut you like to run this macro.

下面的宏应该会有所帮助。打开“工具->宏->宏资源管理器”,然后创建新模块,编辑它,然后复制粘贴下面的代码。所需的命令是 SetCommandArgsProperty。UI 不是很好,但它有效(VS 2005,我希望这也适用于 VS 2010)。然后添加您喜欢的任何快捷方式来运行此宏。

Here are some details:

以下是一些细节:

  • Find startup project
  • Select it active configuration and find property with name "CommandArguments"
  • Create edit box with the current value in it
  • Update property if OK is selected

    Sub SetCommandArgsProperty()
        Dim newVal As Object
        newVal = InputValue(GetCommandArgsPropertyValue())
        If TypeOf newVal Is String Then
            SetCommandArgsProperty(newVal)
        End If
    End Sub
    
    Function InputValue(ByVal defaultText As String)
        Dim frm As New System.Windows.Forms.Form
        Dim btn As New System.Windows.Forms.Button
        Dim edit As New System.Windows.Forms.TextBox
    
        edit.Text = defaultText
        edit.Width = 100
    
        btn.Text = "OK"
        btn.DialogResult = System.Windows.Forms.DialogResult.OK
    
        frm.Text = "Input command line properties"
    
        frm.Controls.Add(btn)
        btn.Dock = System.Windows.Forms.DockStyle.Bottom
    
        frm.Controls.Add(edit)
        edit.Dock = System.Windows.Forms.DockStyle.Top
    
        frm.Height = 80
        frm.Width = 300
    
        If frm.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            Return edit.Text
        End If
        Return System.DBNull.Value
    End Function
    
    Function GetCommandArgsProperty() As EnvDTE.Property
        Dim solution As Solution
        Dim project As Project
        Dim sb As SolutionBuild
        Dim str As String
        Dim cm As ConfigurationManager
        Dim config As Configuration
        Dim properties As Properties
        Dim prop As EnvDTE.Property
    
        solution = DTE.Solution
        sb = solution.SolutionBuild
        For Each str In sb.StartupProjects
            project = solution.Item(str)
            cm = project.ConfigurationManager
            config = cm.ActiveConfiguration
            properties = config.Properties
            For Each prop In properties
                If prop.Name = "CommandArguments" Then
                    Return prop
                End If
            Next
        Next
    End Function
    
    Function GetCommandArgsPropertyValue()
        Return GetCommandArgsProperty().Value
    End Function
    
    Sub SetCommandArgsProperty(ByVal value As String)
        GetCommandArgsProperty().Value = value
    End Sub
    
  • 查找启动项目
  • 选择它的活动配置并找到名称为“CommandArguments”的属性
  • 创建包含当前值的编辑框
  • 如果选择 OK 则更新属性

    Sub SetCommandArgsProperty()
        Dim newVal As Object
        newVal = InputValue(GetCommandArgsPropertyValue())
        If TypeOf newVal Is String Then
            SetCommandArgsProperty(newVal)
        End If
    End Sub
    
    Function InputValue(ByVal defaultText As String)
        Dim frm As New System.Windows.Forms.Form
        Dim btn As New System.Windows.Forms.Button
        Dim edit As New System.Windows.Forms.TextBox
    
        edit.Text = defaultText
        edit.Width = 100
    
        btn.Text = "OK"
        btn.DialogResult = System.Windows.Forms.DialogResult.OK
    
        frm.Text = "Input command line properties"
    
        frm.Controls.Add(btn)
        btn.Dock = System.Windows.Forms.DockStyle.Bottom
    
        frm.Controls.Add(edit)
        edit.Dock = System.Windows.Forms.DockStyle.Top
    
        frm.Height = 80
        frm.Width = 300
    
        If frm.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            Return edit.Text
        End If
        Return System.DBNull.Value
    End Function
    
    Function GetCommandArgsProperty() As EnvDTE.Property
        Dim solution As Solution
        Dim project As Project
        Dim sb As SolutionBuild
        Dim str As String
        Dim cm As ConfigurationManager
        Dim config As Configuration
        Dim properties As Properties
        Dim prop As EnvDTE.Property
    
        solution = DTE.Solution
        sb = solution.SolutionBuild
        For Each str In sb.StartupProjects
            project = solution.Item(str)
            cm = project.ConfigurationManager
            config = cm.ActiveConfiguration
            properties = config.Properties
            For Each prop In properties
                If prop.Name = "CommandArguments" Then
                    Return prop
                End If
            Next
        Next
    End Function
    
    Function GetCommandArgsPropertyValue()
        Return GetCommandArgsProperty().Value
    End Function
    
    Sub SetCommandArgsProperty(ByVal value As String)
        GetCommandArgsProperty().Value = value
    End Sub
    

回答by osirisgothra

The extension CLIArgsMadeEasy 2010/2012 is a great little thing that puts the project's debug session's command line arguments right in a little text box on the visual studio toolbar, IMO, its alot easier and less tedious than using macros.

扩展 CLIArgsMadeEasy 2010/2012 是一个很棒的小东西,它把项目的调试会话的命令行参数放在 Visual Studio 工具栏上的一个小文本框中,IMO,它比使用宏更容易和更乏味。

The Link
http://visualstudiogallery.msdn.microsoft.com/8159cd7d-2c81-47f3-9794-a347ec1fba09?SRC=VSIDE

链接
http://visualstudiogallery.msdn.microsoft.com/8159cd7d-2c81-47f3-9794-a347ec1fba09?SRC=VSIDE

You can just type CLIArgsMadeEasy in your search box in the extensions manager which will find it fairly quickly in the gallery, thats how I installed it, if you need to know. Hope this helps!

您可以在扩展管理器的搜索框中键入 CLIArgsMadeEasy,这将在库中很快找到它,这就是我安装它的方式,如果您需要知道的话。希望这可以帮助!

回答by qwerty

At least in Visual Studio 2012, you can use Alt+F7shortcut to directly access project properties.

至少在 Visual Studio 2012 中,您可以使用Alt+F7快捷方式直接访问项目属性。

Furthermore, the opened Property Pages normally remembers the last opened item, i.e. Configuration Properties -> Debugging.

此外,打开的属性页通常会记住上次打开的项目,即Configuration Properties -> Debugging