如何在 C++ 程序中使用命令行参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12285138/
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-27 16:07:26  来源:igfitidea点击:

How to use command line arguments in C++ program?

c++windowscmdcommand-linecommand-line-arguments

提问by Mohamed Ahmed Nabil

I know that to use command line arguments, I have to do this.

我知道要使用命令行参数,我必须这样做。

int main(int argc, char* argv[])

int main(int argc, char* argv[])

Now most of the documentation i read about taking in command line arguments explain the situation, something like this:

现在,我读到的大多数有关接受命令行参数的文档都解释了这种情况,如下所示:

Command-line arguments are given after the name of a program in command-line operating systems like DOS or Linux, and are passed in to the program from the operating system.

命令行参数在命令行操作系统(如 DOS 或 Linux)中的程序名称之后给出,并从操作系统传递给程序。

So the only way i know to open my program, is to open it normally like i would do, either start debugging or open the exe file

所以我知道打开我的程序的唯一方法是像我一样正常打开它,要么开始调试,要么打开 exe 文件

Now here it seems that, to use command line arguments the program has to be opened differently, Using the Command-Line (Windows Command Prompt for example), and then write the arguments after it.

现在看来,要使用命令行参数,程序必须以不同的方式打开,使用命令行(例如 Windows 命令提示符),然后在其后写入参数。

So my question is

所以我的问题是

How do i open my program using the Command-Line, and how do i enter the arguments after the program name?

如何使用命令行打开我的程序,以及如何在程序名称后输入参数?

回答by Tsubashi

For the purpose of simplicity, I will assume you are using Windows 7.

为简单起见,我假设您使用的是 Windows 7。

The simplest way is to open a DOS box and then drag-n-drop you application on to it. This will insert the path to your executable. Following that, you may begin typing the command line arguments you wish to pass it. It should end up looking something like this:

最简单的方法是打开一个 DOS 框,然后将您的应用程序拖放到它上面。这将插入可执行文件的路径。之后,您可以开始输入希望传递的命令行参数。它最终应该看起来像这样:

C:\Users\cscott> "C:\Users\cscott\Documents\myApp.exe" argument1 argument2

Note: As mentioned in the comments, this does not work on windows vista, a fact I was unaware of at the time of writing.

注意:正如评论中提到的,这在 windows vista 上不起作用,这是我在撰写本文时不知道的事实。

回答by Harper Shelby

I'm going to assume you're using an IDE, and I'll take a wild guess that it's Visual Studio. If I'm right, there are two approaches - one, open up the folder containing the executable that's been built - it'll be in {Solution Directory}/{Project Directory}/bin/{Build Configuration} by default. Run the command line there. The other option is to open the project properties, and under the "Debug" tab (in VS 2010 - it varies by version) put your command line flags in the box labeled "Command line arguments".

我将假设您使用的是 IDE,我会大胆猜测它是 Visual Studio。如果我是对的,有两种方法 - 一种,打开包含已构建的可执行文件的文件夹 - 默认情况下它将位于 {Solution Directory}/{Project Directory}/bin/{Build Configuration} 中。在那里运行命令行。另一个选项是打开项目属性,在“调试”选项卡下(在 VS 2010 中 - 它因版本而异)将命令行标志放在标有“命令行参数”的框中。

回答by Kos

Some ways how arguments can be passed to a program:

将参数传递给程序的一些方法:

  • Open your command prompt(like cmd.exeor PowerShell on Windows), then type: your_program.exe arg1 arg2 arg3.
    You can do the same thing in a shortcut or a script (like a batch or sh script).

  • Edit the run configuration in your IDE.
    For instance, Eclipse alllows you to set command-line arguments separately for each run configuration. This is helpful during development and debugging.

  • On Windows, drag and drop a fileonto the executable. The dragged file's filename will be passed as a command-line argument.

  • On Windows, associate a filename extension with a filetype (assoccommand) and associate that filetype with a command that runs your program (ftypecommand). Now when such a file is opened, either in the command interpreter or by e.g. double clicking, what happens behind the scenes is that your program is run with the path to that file as argument.

  • Run your executable programatically from another program and pass arguments as variables.
    For instance in Python:
    subprocess.call(['my_program.exe','arg1','arg2'])

  • 打开您的命令提示符(如cmd.exeWindows 上的 PowerShell),然后键入: your_program.exe arg1 arg2 arg3.
    您可以在快捷方式或脚本(如批处理或 sh 脚本)中执行相同的操作。

  • 在 IDE 中编辑运行配置。
    例如,Eclipse 允许您为每个运行配置单独设置命令行参数。这在开发和调试过程中很有帮助。

  • 在 Windows 上,将文件拖放到可执行文件上。拖动文件的文件名将作为命令行参数传递。

  • 在 Windows 上,将文件扩展名与文件类型 ( assoccommand) 相关联,并将该文件类型与运行程序的命令( command) 相关联ftype。现在,当打开这样的文件时,无论是在命令解释器中还是通过双击,幕后发生的事情是您的程序以该文件的路径作为参数运行。

  • 从另一个程序以编程方式运行您的可执行文件并将参数作为变量传递。
    例如在 Python 中:
    subprocess.call(['my_program.exe','arg1','arg2'])

回答by Jesus Ramos

In Windows you have to navigate using the command prompt to your executable location and you can run it by saying Myexe.exe first_arg second_arg.

在 Windows 中,您必须使用命令提示符导航到您的可执行位置,您可以通过说Myexe.exe first_arg second_arg.

Alternatively you can right-click your exe file and in the file settings you can specify some command line arguments to provide to it when it is opened by double clicking.

或者,您可以右键单击您的 exe 文件,并在文件设置中指定一些命令行参数,以便在双击打开它时提供给它。

Another way is also by writing a simple batch script that just calls your program like C:/Full/Path/To/Your/Program/app.exe first_arg second_argand running that.

另一种方法是编写一个简单的批处理脚本,它只是调用您的程序C:/Full/Path/To/Your/Program/app.exe first_arg second_arg并运行它。

In Visual Studio or your preferred IDE you will have the option in the project settings to specify some command line arguments to your program when executing from inside the IDE.

在 Visual Studio 或您首选的 IDE 中,您可以在项目设置中选择在从 IDE 内部执行时为您的程序指定一些命令行参数。

回答by Moataz Elmasry

Here's a simple example I use in linux

这是我在 linux 中使用的一个简单示例

./myprogram args1 args2

and u can parse it like this

你可以像这样解析它

int
main (int argc, char **argv)
{
  if (argc >= 2) {
     std::string param(argv[1]);
  }
  ///etc
}

回答by t_smith

You can write when launching from command prompt, you can make shortcut and add arguments after name, you can add arguments in some IDE when debugging or you can cal your program with other program using some arguments.

您可以在从命令提示符启动时编写,您可以创建快捷方式并在名称后添加参数,您可以在调试时在某些 IDE 中添加参数,或者您可以使用某些参数与其他程序一起调用您的程序。

回答by Skandh

<path of your program> <Arguments separated by space>

回答by Daniel Casserly

You can do this by either opening a command prompt and cd to the path and enter the exe name followed by your params:

您可以通过打开命令提示符和 cd 到路径并输入 exe 名称后跟您的参数来执行此操作:

 eg: bob.exe bob dylan

where your exe is bob and the two params are bob and dylan...

你的exe是bob,两个参数是bob和dylan...

...or you can make a shortcut and right click, choose properties, shortcut and add the params to the end of the target field.

...或者您可以创建一个快捷方式并右键单击,选择属性,快捷方式并将参数添加到目标字段的末尾。

"C:\bob.exe" /bob dylan

There may be an option in your IDE depending on what that is.

您的 IDE 中可能有一个选项,具体取决于它是什么。

回答by James Kanze

Something is going to start your program. It's up to that something to pass it arguments. All of the usual shells will parse the command line (although not always in the same way) to present you the arguments. Under Windows, left clicking on buttons on the desktop and in the task bar will open up a configuration window, which has a "Shortcut" tab where you can enter the command line as a "Target:". The rc files under Unix (executed on start-up) are basically shell scripts, and the cron files (timed start-up) also take a command line (and not just an isolated command). And so on.

有些东西会启动你的程序。这取决于传递它的参数。所有常用的 shell 都会解析命令行(尽管并不总是以相同的方式)以向您呈现参数。在 Windows 下,左键单击桌面和任务栏中的按钮将打开一个配置窗口,其中有一个“快捷方式”选项卡,您可以在其中输入命令行作为“目标:”。Unix 下的 rc 文件(启动时执行)基本上是 shell 脚本,cron 文件(定时启动)也采用命令行(而不仅仅是一个孤立的命令)。等等。

In other contexts, you can map the file type (extension) to a command which will be executed when you click on a file of that type, or download it. In such cases, if nothing else, you will at least get the full path to the file.

在其他上下文中,您可以将文件类型(扩展名)映射到单击该类型文件或下载该文件时将执行的命令。在这种情况下,如果不出意外,您至少将获得文件的完整路径。

In the few cases where you can only get the name of the file, it's fairly easy to write your own shell script to add extra arguments when it invokes your program.

在您只能获取文件名的少数情况下,编写自己的 shell 脚本以在调用程序时添加额外的参数是相当容易的。