0

I have created a console program in qt5, it should get arguments from the console before it's running.

This is the part of the code where I use the arguments passed from the console:

void foo::start(){
    if(arguments.contains(--help))
        show help function
    else if (arguments.contains(--ipinfo))
        show ip info function
    else if (arguments.contains(--time))
        show time info function
    else 
        nothing
}

My programs name is initlizer. When I run my program via console with an argument, I want to get the arguments from the console using qt5. For example:

$initlizer --help >> show help function
$initlizer --time >> show time function
Agaz Wani
  • 5,514
  • 8
  • 42
  • 62
  • 3
    Just about any [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) should tell you how to get arguments. In short, you never wondered about the `argc` and `argv` arguments to the `main` function? – Some programmer dude Apr 27 '18 at 13:18
  • @Someprogrammerdude You could probably remove the "good" ;) – UKMonkey Apr 27 '18 at 13:21
  • 1
    Also see [Obtaining command line arguments in a Qt application](https://stackoverflow.com/q/2918353/608639) and [QApplication app(argc, argv)](https://stackoverflow.com/q/5770154/608639) – jww Apr 27 '18 at 14:26

2 Answers2

2

The "Qt way" of dealing with commandline arguments is to pass main's argc and argv to the QCoreApplication constructor, then use QCommandLineParser to query the arguments. (That link includes lots of example code.)

timday
  • 24,582
  • 12
  • 83
  • 135
1

In C++ to pass arguments to your console program you must add parameters to your source codes main function. These parameters in your main function determines what input values he receives and use in it's execution.

For example:

This is an example of a non-parameters main function:

public int main ()
{ 
    // Functions body. 
}

This is an example of a main function that receive an string as input:

public int main (int argc, char * argv[])
{
    // Functions body.
}

This is an example of a main function that receives multiple arguments as input:

 public int main (int argc, char * argv[],  // other parameters)
{
    // Functions body.
}

To do it in a Qt way you must define the parameters in the QCoreApplication constructor when you instantiate the app and then use the QCommandLineParser to get the arguments passed by the console.

See an example in the Detailed Description section of this page.

You can get more information the parameters in main function from this page.

Community
  • 1
  • 1
CryogenicNeo
  • 937
  • 12
  • 25