Handling command line flags in C/C++

前端 未结 7 881
星月不相逢
星月不相逢 2020-12-14 21:03

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
<         


        
7条回答
  •  感动是毒
    2020-12-14 21:29

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

    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

    If you compile this program into a.out, and run it as:

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

    You should see output:

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

提交回复
热议问题