기본 콘텐츠로 건너뛰기

getopt 사용하기

Linux/UN*X에서 많이 쓰는 getopt 함수에 대해 정리한다. 다른거 다 필요 없고, 예제.

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

void
help(void)
{
    // 중략...
}

int
main(int argc, char* argv[])
{
    int opt;
    const char* optstr = "hf:";
    char filename[1024+1] = "";
    while (( opt = getopt(argc,argv,optstr) ) != -1 )
    {
        switch (opt)
        {
        case 'h': help(); exit(EXIT_SUCCESS);
        case 'f': strcpy(filename,optarg);break;
        case '?': // invalid option
            exit(EXIT_FAILURE);
        case ':': // invalid format
            exit(EXIT_FAILURE);
        }
    }
}

뭐... 대충 이런 식이다. 위에서 optstr에는 검출할 옵션 문자열을 정의하는 것으로 그냥 필요한 문자를 나열하고, 그 가운데 옵션이 더 필요한 녀석(예. -f filename.txt)만 뒤에 콜론을 붙여준다.

댓글

이 블로그의 인기 게시물

탐색기에서 OneDrive 이 2개로 보이는 문제

왜 2개가 보이는지 모르겠지만, Registry 삭제하면 됨 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace 하위 키에서 OneDrive 둘 중에 하나만 지워도 바로 반영됨. 참조:  https://answers.microsoft.com/en-us/msoffice/forum/all/duplicate-onedrives-in-file-explorer/49c935a6-287b-43a5-aed5-2dee2a1c1b22