用 C++ 创建程序从命令行获取参数

发布于 2024-11-05 02:20:02 字数 464 浏览 0 评论 0原文

我正在尝试学习如何用 C++ 编写一个程序,当您运行它时,您可以告诉它运行并在一行中指定所有选项。例如,您可以在 CMD 中执行 ipconfig /all,它会使用选项 /all 运行 ipconfig.exe。另一个例子是 shutdown -f,它使用选项 -f 告诉计算机关闭。例如,我想制作一个从 URL 下载某些内容的程序,并将其称为“下载程序”。从命令行输入 downloader http://filehere.com /h 它将下载带有 /h 选项的文件,我将在程序中定义其属性。我不需要有关如何制作下载器的代码或指南,我只是想学习如何指定 /h 等选项。是否有您知道并可以发布的任何指南或任何示例代码?我尝试过搜索指南,但我想我只是不知道这个操作实际上叫什么。谢谢。

I am trying to learn how to make a program in C++ that when you run it, you can tell it to run and specify options all in one line. For example you can do ipconfig /all in CMD and it runs ipconfig.exe with the option /all. Another example would be shutdown -f which tells the computer to shutdown with the option -f. For example, I want to make a program that downloads something from a URL and call it for example downloader. From command line one would type downloader http://filehere.com /h which would download the file with the /h option which I would define its property in my program. I don't want code or guides on how to make a downloader I am just trying to learn how to specify options like the /h. Are there any guides out there that you know of and could post or any sample code? I have tried searching for guides, but I think I just don't know what this operation is actually called. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云仙小弟 2024-11-12 02:20:02

您通常将 main 函数定义为采用两个参数:int argcchar *argv[],例如:

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

第一个参数是多少您的程序收到的参数,argv 是指向它们的指针。请注意,这不是强制性的,您可以随意命名它们,但这是惯例。只要确保您的类型匹配即可。

您可以使用选项解析库,但这些库通常是特定于操作系统的。检查是否收到 /h 的一种简单方法是:

int got_h = 0;
for (int i=0; i<argc; ++i)
   if (strcmp(argv[i], "/h") == 0)
      got_h = 1;
...
if (got_h)
   ...

argv[argc] 将始终为 NULL,以便更轻松地迭代它们。

更多信息请参见:http://www .site.uottawa.ca/~lucia/courses/2131-05/labs/Lab3/CommandLineArguments.html

You typically define your main function to take two arguments: int argc and char *argv[], such as:

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

The first argument is how many parameters your program received, argv is a pointer to them. Note, this isn't mandated, you can name them whatever you want, but that's the convention. Just make sure your types match up.

You can use an option-parsing library, but those are often OS-specific. One simple way to check if you received a /h is:

int got_h = 0;
for (int i=0; i<argc; ++i)
   if (strcmp(argv[i], "/h") == 0)
      got_h = 1;
...
if (got_h)
   ...

argv[argc] will always be NULL to make iterating through them easier.

Some more information here: http://www.site.uottawa.ca/~lucia/courses/2131-05/labs/Lab3/CommandLineArguments.html

清醇 2024-11-12 02:20:02

main 函数采用两个参数,传统上称为 argcargv

int main (int argc, char * argv[])
{
    // ...
}

argc 包含传递给函数的参数数量。命令行,并且 argv 数组包含此类参数(argv[0] 是用于调用程序的名称); argv 数组的最后一个元素(即 argv[argc])包含一个 NULL 指针。

The main function takes two arguments, traditionally named argc and argv:

int main (int argc, char * argv[])
{
    // ...
}

argc contains the number of arguments passed on the command line, and the argv array contains such arguments (with argv[0] being the name used to invoke your program); the last element of the argv array (i.e. argv[argc]) contains a NULL pointer.

放血 2024-11-12 02:20:02

根据您的熟练程度和使用指针的倾向,您可能更喜欢将命令行捕获为 vector

// UNTESTED CODE
int main(int argc, char **argv) {
  std::vector<std::string> args(argv+1, argv+argc);

  if(args.empty()) {
    std::cout << "Usage: downloader URL [options]\n";
    return 1;
  }
  if(std::find(args.begin(), args.end(), "/h") != args.end()) {
    option_h = true;
  }
  Download(args[0]);
}

Depending upon your proficiency and inclination to use pointers, you may prefer to capture the command line as a vector<string>:

// UNTESTED CODE
int main(int argc, char **argv) {
  std::vector<std::string> args(argv+1, argv+argc);

  if(args.empty()) {
    std::cout << "Usage: downloader URL [options]\n";
    return 1;
  }
  if(std::find(args.begin(), args.end(), "/h") != args.end()) {
    option_h = true;
  }
  Download(args[0]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文