C++ Class
2020.09.01 22:52

main함수 명령줄 옵션 해석

조회 수 9117 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

C/C++로 작성된 응용프로그램의 main함수는 두개의 인수(관습적으로 argc와 argv)를 전달받을 수 있다. 프로그래머는 응용프로그램에 명령줄옵셔들을 전달하여 프로그램을 상황에 따라 제어하는데 많이 사용된다. (사실 main함수에는 세번째 인수로 환경변수를 전달 받을 수 있다.)

 
Qt응용프로그램도 C++ 응용프로그램이므로 main함수에 옵션을 전달 할 수 있다. 이 글에서는 Qt응용프로그램에서 옵션을 해석할 때 편리하게 사용할 수 있는 클래스를 소개하고 간단하게 사용하는 방법을 설명한다.
 
명령줄 옵션은 보통 짧은 이름 옵션과 긴 이름 옵션으로 분류하는데 짧은 이름 옵션은 -(하이픈)과 영문자, 숫자 또는 기호의 단일 문자로 나타내는 옵션 형식이고 긴 이름 옵션은 --(하이픈 두개)에 영단어로 표현하는 형식이다. 
 
 
QCoreApplication 객체에 인수 전달
 
Qt 기반의 응용프로그램의 main함수는 대부분 QCoreApplication 객체를 생성하고 이때 main함수의 인수를 전달한다. 다음은 기본적인 Qt 응용프로그램 main함수를 보여준다.
#include <QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    // QApplication 객체를 생성할 때 argc와 argv를 전달한다.
    QApplication app(argc, argv);

    // MainWindow를 생성한다.
    MainWindow w;
    w.resize(1280, 720);
    w.show();

    return app.exec();
}

 

이 후 QApplication 객체를 통해 인수를 얻을 수 있다. 

QStringList args = app.arguments();

 

 

QCommandLineParser 클래스로 옵션 해석
 
QCommandLineParser 클래스는 편리하게 명령 줄 옵션을 처리하는 방법을 제공한다. 옵션 집합을 정의하고, 명령 줄 인수를 구문 분석하고, 실제로 사용 된 옵션과 옵션 값을 저장하는 기능을 제공한다.
 
이 클래스를 사용하려면 다음과 같이 헤더파일을 포함해줘야 한다.
#include <QCommandLineParser>
 
다음은 QCommandLineParser 클래스를 사용하는 기본적인 예제 소스 코드이다.
int main(int argc, char *argv[])
{
    // QApplication 객체를 생성할 때 argc와 argv를 전달한다.
    QApplication app(argc, argv);

    MainWindow w;
    w.resize(1280, 720);

    QCommandLineParser parser;

    // -h (help)옵션을 추가해준다.
    parser.addHelpOption();

    // 짧은 이름(-s)을 가진 bool 옵션 추가.
    QCommandLineOption showWindowOption("s", QCoreApplication::translate("main", "Show Window"));
    parser.addOption(showWindowOption);

    // 여러 이름을(-f, --force)가진 bool 옵션 추가.
    QCommandLineOption forceOption(QStringList() << "f" << "force",
            QCoreApplication::translate("main", "Overwrite existing files."));
    parser.addOption(forceOption);

    // 실제 명령 줄 인수 처리
    parser.process(app);

    bool show = parser.isSet(showWindowOption);

    if(show){
        /* 옵션(-s)이 포함되면 윈도우를 보여준다. */
        w.show();
    }

    return app.exec();
}

 


  1. No Image notice

    Qt프로그래밍(QtQuick) Beginner를 위한 글 읽는 순서

    Date2019.01.05 CategoryQML and Qt Quick By운영자 Views131086
    read more
  2. No Image

    싱글 샷(Single-Shot) 시그널/슬롯 연결

    Date2021.05.12 CategoryQt 6 Bymakersweb Views7999
    Read More
  3. No Image

    응용프로그램 자동실행 설정 (on Windows)

    Date2021.05.08 CategoryC++ Class Bymakersweb Views5652
    Read More
  4. No Image

    Qt 6 에서 프로퍼티 바인딩

    Date2021.04.03 CategoryQt 6 Bymakersweb Views5427
    Read More
  5. No Image

    QML과 JavaScript 의 숫자 관련 내장된 함수

    Date2021.03.28 CategoryQML and Qt Quick Bymakersweb Views7256
    Read More
  6. Qt 5 코드를 Qt 6로 포팅하기 위해 도움이 되는 Clazy Framework

    Date2021.03.01 CategoryQt 6 Bymakersweb Views6283
    Read More
  7. C++로 작성한 클래스를 QML에서 생성

    Date2021.02.10 CategoryQML and Qt Quick Bymakersweb Views10823
    Read More
  8. Qt MQTT의 pus/sub 튜토리얼

    Date2021.02.06 CategoryMobile and Embedded Bymakersweb Views7828
    Read More
  9. Qt 를 사용하거나 기반으로 하는 응용프로그램

    Date2021.01.30 CategoryGeneral and Desktop Bymakersweb Views10695
    Read More
  10. No Image

    Loader를 사용하여 동적으로 QML 로드

    Date2021.01.19 CategoryQML and Qt Quick Bymakersweb Views8584
    Read More
  11. No Image

    QThread 및 QMutex 예제

    Date2021.01.12 CategoryC++ Class Bymakersweb Views7616
    Read More
  12. No Image

    그래픽 소프트웨어에서 디자인 내보내기 (Exporting Designs from Graphics Software)

    Date2020.12.25 CategoryQML and Qt Quick Byj2doll Views6872
    Read More
  13. No Image

    Qt5Compat 라이브러리를 사용하여 Qt5에서 Qt6로 포팅

    Date2020.12.05 CategoryQt 6 Bymakersweb Views5751
    Read More
  14. Qt Quick Controls 2에 네이티브 데스크탑 스타일 추가

    Date2020.11.23 CategoryQt 6 Bymakersweb Views8738
    Read More
  15. No Image

    QML 바인딩 끊김 진단

    Date2020.11.08 CategoryQML and Qt Quick Bymakersweb Views6188
    Read More
  16. No Image

    QML과 코루틴(Coroutines)

    Date2020.11.03 CategoryQML and Qt Quick Bymakersweb Views6340
    Read More
  17. No Image

    Qt 6의 비동기 API

    Date2020.10.19 CategoryQt 6 Bymakersweb Views5914
    Read More
  18. No Image

    QRandomGenerator 클래스를 사용하여 난수(random values) 생성

    Date2020.10.17 CategoryC++ Class Bymakersweb Views7426
    Read More
  19. No Image

    Qt 6에서 QList 변경사항

    Date2020.10.08 CategoryQt 6 Bymakersweb Views6027
    Read More
  20. No Image

    Qt 6.0의 개발 호스트 및 대상 플랫폼

    Date2020.09.16 CategoryQt 6 Bymakersweb Views10033
    Read More
  21. No Image

    main함수 명령줄 옵션 해석

    Date2020.09.01 CategoryC++ Class Bymakersweb Views9117
    Read More
Board Pagination Prev 1 2 3 4 5 9 Next
/ 9