Handling command line flags in C/C++

2019-01-24 05:45发布

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

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.

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.

7条回答
做个烂人
2楼-- · 2019-01-24 06:16

The easiest thing is to write your main() like so:

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

Then inside that main you decide what happens to the command line arguments or "flags". You find them in argv and their number is argc.

查看更多
登录 后发表回答