What makes it possible for the program to run both via the console and in the graphical environment is that it is capable of recognizing the boot mode with which the user has booted it. These two possible modes are (1) double click and (2) via command line.

Next we can see the isatty function

/*
– while debugging it is convenient to set this if’s condition to “isatty(fileno(stdin))”
– so that it will always run the application through a window
*/
if (!isatty(fileno(stdin)))

that is the one that decides if the program has been started through the command line or via a double click. This function returns 1 if the open file descriptor passed to it by parameter refers to a terminal; otherwise it returns 0. Therefore, as can be seen, when 0 is returns, it means that the graphic environment must be executed.

Processing options

To process the options that the user can enter in the terminal, given that the lexical and syntactic analysis of any grammar is always complicated, it is always the best option to use the tools provided by the system. In our case: getopt_long takes care of precisely this by using as arguments argc and argvwhich are the input parameters of the main function. Any argv element that begins with ‘‘ (and is not exactly ‘‘ or ` — ‘) is an input element, that is, an option.

At the same time, it offers the flexibility of being able to introduce options in this way

or

The options are configured using the struct option that has the following structure:






Name: this is the option name

has_arg: can take three values:

  • no_argument (or 0) if the parameter does not support arguments;
  • required_argument (o 1) if the option necessarily requires an argument and finally
  • optional_argument (o 2) if, as the name suggests, the argument is optional.

Flag: specifies how the results are returned

Val: is the value that is loaded into the variable where the flag points to

Below the example done with Qt. As you can see there is a printUsagefunction that is only run in case the software is run on the terminal.

In any case, the Application, which handles the internal logic, is build accordingly. This application is separated from the mainWindow which constitutes our facade. This interface will then mediate between the end user and the different internal classes (Application in our case)

Categories: regular

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *