简体   繁体   English

launch.json args字段中的Visual Studio Community 2017空间

[英]Visual Studio Community 2017 spaces in launch.json args field

I'm currently trying to set the debugging startup arguments for a CMake based project in Visual Studio Community 2017. Normally this would be done via the launch.vs.json file. 我目前正在尝试为Visual Studio Community 2017中基于CMake的项目设置调试启动参数。通常,这可以通过launch.vs.json文件完成。 However, the arguments that I need to pass contain spaces in them. 但是,我需要传递的参数中包含空格。 For example, the below launch.vs.json file should pass the arguemnts FIRST ARGUMENT and SECOND ARGUMENT as the first and second argument. 例如,下面的launch.vs.json文件应将争辩参数FIRST ARGUMENT和SECOND ARGUMENT作为第一个和第二个参数传递。 However, the program ends up getting 4 arguments: FIRST, ARGUMENT, SECOND, and ARGUMENT. 但是,该程序最终得到4个参数:FIRST,ARGUMENT,SECOND和ARGUMENT。 I've tried various different encodings for spaces, but cannot get a space to be encoded properly in the resulting arguments. 我已经尝试过各种不同的空格编码,但是无法在结果参数中获得正确编码的空格。 This is especially problematic as one of the arguments to my program is a path inside C:\\Program Files. 这特别有问题,因为我程序的参数之一是C:\\ Program Files中的路径。 As such, the path is broken into 2 separate arguments, rather than one as it should be. 因此,该路径分为2个独立的参数,而不是应有的参数。 How do I make Visual Studio allow spaces within an argument? 如何使Visual Studio在参数中允许空格?

For reference, launching via the command line with this command works as expected: 作为参考,使用以下命令通过命令行启动可以正常工作:

argtest.exe "FIRST ARGUMENT" "SECOND ARGUMENT"

launch.vs.json launch.vs.json

{
  "version": "0.2.1",
  "defaults": {},
  "configurations": [
    {
      "type": "default",
      "project": "CMakeLists.txt",
      "projectTarget": "argtest.exe",
      "name": "argtest.exe",
      "args": ["FIRST ARGUMENT", "SECOND ARGUMENT"]
    }
  ]
}

test.c test.c的

#include <stdio.h>
int main(int argc, char ** argv) {
    int i;
    for (i = 0; i < argc; i++)
        printf("%d = %s\n", i, argv[i]);
    return 0;
}

CMakeLists.txt 的CMakeLists.txt

cmake_minimum_required (VERSION 2.8.8)
project (argtest)
SET( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/ )
add_executable(argtest test.c)

You need to include the quotes in the args strings and escape them. 您需要将引号包含在args字符串中并对其进行转义。 For example: 例如:

{
  "version": "0.2.1",
  "defaults": {},
  "configurations": [
    {
      "type": "default",
      "project": "CMakeLists.txt",
      "projectTarget": "argtest.exe",
      "name": "argtest.exe",
      "args": ["\"FIRST ARGUMENT\"", "\"SECOND ARGUMENT\""]
    }
  ]
}

It is easier doing this through Visual Studio itself. 通过Visual Studio本身更容易做到这一点。 To run a program with command-line arguments follow these steps: 要使用命令行参数运行程序,请按照下列步骤操作:

  1. Open the project in Visual Studio 在Visual Studio中打开项目
  2. Click on the 'Debug' tab in the menu bar. 点击菜单栏中的“调试”标签。
  3. Click on the '[NAME OF PROJECT] Properties' menu item. 单击“ [项目名称]属性”菜单项。
  4. A window will popup and should automatically go into the Debug menu within that window. 将会弹出一个窗口,并应自动进入该窗口中的“调试”菜单。 Under the 'Start options' section enter your command-line arguments with a space between each argument. 在“开始选项”部分下,输入命令行参数,每个参数之间应有一个空格。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM