处理命令行标志在C / C ++ [英] Handling command line flags in C/C++

查看:89
本文介绍了处理命令行标志在C / C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找一个非常简单的解释/补习什么标志。据我所知,标志工作指示命令做什么。例如:

I am looking for a very simple explanation/tutorial on what flags are. I understand that flags work indicate a command what to do. For example:

rm -Rf test

我知道,rm命令将删除test文件夹,而且-Rf标志将强制命令擦除不仅是文件夹,但文件了。

I know that the rm command will remove the test folder and that the -Rf flags will force the command to erase not just the folder but the files in it.

不过,在哪里读的标志/编译?什么处理的标志吗?可以我举个例子,我自己写的C / C ++程序,并指定不同的标志,这样的程序做不同的事情?我希望我提出正确的问题。如果没有,请让我知道。

But, where are the flags read/compiled??? What handles the flags? Can I, for example, write my own C/C++ program and designate different flags so that the program does different things? I hope I am asking the right questions. If not, please let me know.

推荐答案

这个简单的程序应该证明传递给程序的参数(包括程序名本身。)

This simple program should demonstrate the arguments passed to the program (including the program name itself.)

解析,除preting,并利用这些参数是由程序员(你),虽然有可以帮助库。

Parsing, interpreting and using those arguments is up to the programmer (you), although there are libraries available to help.

int main(int argc, char* argv[])
{
    int i;
    for(i=0; i<argc; ++i)
    {   printf("Argument %d : %s\n", i, argv[i]);
    }
    return 0;
}


如果您编译这个程序到的a.out ,并运行它为:

prompt$>  ./a.out ParamOne ParamTwo -rf x.c

您应该看到输出:

Argument 0 : a.out
Argument 1 : ParamOne
Argument 2 : ParamTwo
Argument 3 : -rf
Argument 4 : x.c

这篇关于处理命令行标志在C / C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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