I am interested in how to implement the following: I start the program with ./programm_name through the console and, for example, enter -t 15 to specify a parameter, in my case, this is the duration. The output should indicate the number 15, instead of zero.

 int main(int argc, char **argv) { int durationTime = 0; //ΠΊΠΎΠ΄ if(strcmp(argv[i], "-t")) == 0) { printf("\nnew duration Time is - %d\n", durationTime); 
  • you pass two parameters: -t and 15 you can check it with the help of the value argc - Grundy
  • @Grundy argc not 3 show? zero - doesn’t the name of the program count in Si - teran
  • @teran, it may well be :) - Grundy
  • @teran - 0 is the program itself, 1 is the first argument, 2 is the second argument, and so on) how it will work if there are more arguments. - Insider
  • @Insider has already suggested using getopt() in the comments to the answer below. If you can pass several parameters, write to the question. If you want to write the parameters yourself, it is obviously necessary to argv over the argv array in search of the -t value and take the next element in the list as a value, checking it for validity / - teran

4 answers 4

Check the value of argv[2]

 int main(int argc, char * * argv) { int durationTime = 0; //ΠΊΠΎΠ΄ if (argc >= 3 && strcmp(argv[1], "-t") == 0) { durationTime = atoi(argv[2]); printf("\nnew duration Time is - %d\n", durationTime); } } 
  • 2
    Why not use getopt ? - 0andriy
  • in your case, it will printf string twice - Insider
  • @Insider twice where did you get it from? - teran
  • @teran just executed the code) - Insider

I strongly recommend not to reinvent the wooden bicycle with triangular wheels, but to use the getopt_long function used in all industrial developments. Something like:

 #include <getopt.h> extern char *optarg; extern int optind, opterr, optopt; struct param pr; // Π Π°Π·Π±ΠΎΡ€ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ² ΠΊΠΎΠΌΠ°Π½Π΄Π½ΠΎΠΉ строки // argc ΠΈ argv - ΠΈΠ· Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ main. int ParseParam(int argc, char **argv) { int c; int option_index = -1; static struct option long_options[] = { {"duration", required_argument, 0, 't' }, . . . {0, 0, 0, 0 } }; // Π—Π°Π΄Π°Ρ‘ΠΌ значСния ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ: memset(&pr, sizeof(struct param), 0); pr.t = 0; . . . // Π Π°Π·Π±ΠΎΡ€ ΠΎΠΏΡ†ΠΈΠΉ ΠΊΠΎΠΌΠ°Π½Π΄Π½ΠΎΠΉ строки while (1) { c = getopt_long(argc, argv, "t: . . .",long_options, &option_index); if (c == -1) break; switch (c) { case 't': pr.d = atoi(optarg); break; . . . default: printf("ΠžΡˆΠΈΠ±ΠΎΡ‡Π½Π°Ρ опция ΠΊΠΎΠΌΠ°Π½Π΄Π½ΠΎΠΉ строки\n"); return -1; } option_index = -1; } return 0; }; 

Dots must be replaced with other parameters, and the structure pr is your structure for the entered parameters.

    In general, the user can enter the arguments as a space, for example,

     program_name -t 15 

    as well as one argument

     program_name -t15 

    Moreover, the numerical value can also be entered with an error. for example

     program_name -t 15A 

    In addition, it is necessary to check that the total number of arguments is no more than that required to be specified.

    All this makes it difficult to check the arguments.

    Straightforward approach might look like this.

     int durationTime = 0; int valid_args = 1; if (argc > 1 ) { if ( ( valid_args = strncmp( argv[1], "-t", 2 ) == 0 ) ) { char *p; if ((valid_args = argv[1][2] == '\0' && argc == 3)) { p = argv[2]; } else if ((valid_args = argv[1][2] != '\0' && argc == 2)) { p = argv[1] + 2; } if (valid_args) { char *tail; durationTime = (int)strtol(p, &tail, 10); valid_args = *tail == '\0'; } } } if (!valid_args) puts("Error: incorrect arguments." "\nUsage: program [-t duration]"); 

      slightly changed the @ anton-shchyrov code and everything works as it should. That's who's interested)

       int main(int argc, char **argv) { int i = 0; int durationTime = 15; int ret = 0; int sampleRate = 48000; int startCapture = 0; int mainHeight = 1280; int mainWidth = 720; int previewHeight = 640; int previewWidth = 480; char captureFormat[7] = "format"; if (argc == 1) { help(); } for (i = 1; i < argc; i++) { if ((strcmp(argv[i], "-h") == 0) || (strcmp(argv[i], "--help") == 0)) { help(); } if (argc >= 3 && (strcmp(argv[i], "-t") == 0) || (strcmp(argv[i], "--time") == 0)) { durationTime = atoi(argv[i + 1]); startCapture++; } if ((strcmp(argv[i], "-c") == 0) || (strcmp(argv[i], "--capture") == 0)) { startCapture++; } if (argc >= 3 && (strcmp(argv[i], "-a") == 0) || (strcmp(argv[i], "--audio") == 0)) { sampleRate = atoi(argv[i + 1]); startCapture++; } if(argc >= 3 && (strcmp(argv[i], "-r") == 0) || (strcmp(argv[i], "--resolution") == 0)){ mainHeight = atoi(argv[i + 1]); mainWidth = atoi(argv[i + 2]); startCapture++; } if(argc >= 3 && (strcmp(argv[i], "-i") == 0) || (strcmp(argv[i], "--image") == 0)){ previewHeight = atoi(argv[i + 1]); previewWidth = atoi(argv[i + 2]); startCapture++; } if(argc >= 3 && (strcmp(argv[i], "-f") == 0) || (strcmp(argv[i], "--format") == 0)){ startCapture++; } } if (startCapture > 0) { //ΠΊΠΎΠ΄ } }