2010-11-10

[C/C++語言] 解析命令列的參數

// xxxx.exe -cfg path -tg target

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]) {
    int status = 0;

    for (int i=1; i<argc; i++){
        if (!strcmp(argv[i], "-tg")){
            i++;
            if (i < argc){
                printf("target is %s\n",argv[i]);
            }else {
                status=1; break;
            }
        }else if (!strcmp(argv[i], "-cfg")){
            i++;
            if (i < argc){
                printf("config path is %s\n",argv[i]);
            }else {
                status=1; break;
            }
        }
    }
    return status;
}

看到前輩寫的 code,記錄一下,原來很簡單就可以做到。

1 回應:

匿名 提到...

I have infer from a scarcely any of the articles on your website now, and I definitely like your tastefulness of blogging. I added it to my favorites entanglement age list and will be checking stand behind soon. Divert contain into public notice my orientation as approvingly and leave to me be familiar with what you think. Thanks.