how to handle arguments from terminal properly in a console application?



  • Control program state is relatively easy in an UI application because we can add buttons or other UI components to control the state. But when comes to console application, it's really difficult for me because I am really newer of writing a console application. So i have following questions about this topic:

    1. after init arguments set in main function, how to receive the quit command(like "q") form terminal or cmd to stop the current task(periodically run)?does following code suitable?if not what's the usual design for console application control?
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
       //some work code that periodically run 
     while(1){
        in>>order;
        if(order=="show"){
        //some operation
           while(1)
           {
             in>>exit;
            if(exit=="q")
            {
              //stop code 
               break;
              }
           }
        }
    else if(xxxx){}
    
    }
        return a.exec();
    }
    
    1. what is the properly way to exist console application after receive cmd from terminal?
    2. timer in the main function can connect to slots in the other thread ?

    these are current questions i want to ask,maybe more questions will add during the discussion .


  • Lifetime Qt Champion

    Hi,

    This Github gist might be an interesting starting point.



  • @SGaist I have tried your suggestion code but I'm just having trouble with your code on a Windows setup... I can't write in the console


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.