C:getopt无法识别选项。有人可以解释一下吗?

时间:2022-12-11 17:14:39

i have to write a programm, which can be started with options for a Lecture, but it seems that the option '-r' doesn't work.

我必须编写一个程序,可以使用Lecture的选项启动,但似乎选项'-r'不起作用。

I can compile it without any errors or warnings but if I'm going to start it, it tells me that '-r' is a Non-Option which will be caused from the for-loop after the switch-case block.

我可以编译它没有任何错误或警告,但如果我要启动它,它告诉我'-r'是一个非选项,它将由switch-case块之后的for循环引起。

CLI - Compile - Run

CLI - 编译 - 运行

#include <pthread.h>    /* used for threading */
#include <stdio.h>
#include <unistd.h>     
#include <ctype.h>      /* used for isprint() function */
#include <stdlib.h>     /* used for lrand48, labs, strtol etc. */
#include <time.h>       /* used for clock and etc */
#include <assert.h>     /* used for assert function */


int rflag = 0;      /* Flag for min size (rand) */
int Rflag = 0;      /* Flag for max size (rand) */
int rvalue = 0;     /* Defualt min size */
int Rvalue = 100;   /* Default max size */
int vflag = 0;      /* Flag for  verbose output */
extern const char *gitversion;

void *PrintHello(void *threadarg) {
    /******************************************************
    * - print in one line the Thread Number given in threadarg 
    *   and "Hello World" 
    * - sleep a random time (see line below!)
    ******************************************************/
    pthread_t tid = pthread_self();     /* The Thread Id of the current thread */
    int num = *((int*) threadarg);          /* Number of the Thread */

    long sleeping_time = (lrand48() % (Rvalue - rvalue + 1) + rvalue);
    if (sleeping_time < 0) {
        sleeping_time = labs(sleeping_time);
    }
    printf("Hello everybody, I'm thread number %d\n", num);
    if (vflag == 1) {
        printf("The Thread ID of Thread number %d is = %lu.\n", num, tid);
    }
    sleep(sleeping_time);
    /* say goodbye */       
    printf("%d: Thread is done after sleeping %ld[s]\n", num, sleeping_time);

    return(NULL); /* Dummy to make -Wall happy ... */
}

/*
 * Prints a little help to stdout
 */
void printHelp() {
    printf("------------------------------HELP--------------------------------\n");
    printf("Usage: [Option] [Argument(If required!)]\n\n");
    printf("Option '-h' - Prints this Help and the current GIT Revision\n");
    printf("Option '-v' - Prints more Informations corresponds to 'verbose'\n");
    printf("Option '-t' - Requiers a number as Argument which represents the\n");
    printf("              Number of Thread which will be created\n"); 
    printf("Option '-r' - Minimum Randomsize\n");
    printf("Option '-R' - Maximum Randomsize\n");
    printf("------------------------------------------------------------------\n");
    printf("Git Revision #:  %s\n", gitversion);
    printf("------------------------------------------------------------------\n");
}


int main(int argc, char *argv[]) {

    void *status;
    long t;
    int hflag = 0;
    int tflag = 0;
    long NumberOfThreads = 4;
    int c;
    int i;

    while((c = getopt(argc, argv, "ht:vr:R:")) != -1) {
        switch(c) {
            case 'h':
                hflag = 1;
                break;
            case 't':
                tflag = 1;
                NumberOfThreads = strtol(optarg, NULL, 10);
                /*assert(NumberOfThreads != 0);*/
                break;
            case 'v': 
                vflag = 1;
                break;
            case 'r':
                rflag = 1;
                rvalue = strtol(optarg, NULL, 10);
                if (rvalue < 0) {
                    fprintf(stderr, "The number has to be greater than zero!\n");
                    exit(-1);
                }
                break;
            case 'R':
                Rflag = 1;
                Rvalue = strtol(optarg, NULL, 10);
                if (Rvalue < 0) {
                    fprintf(stderr, "The number has to be greater than zero!\n");
                    exit(-1);
                } else if (Rvalue < rvalue) {
                    fprintf(stderr, "The max number for rand has to be greater than the min number!\n");
                    exit(-1);
                }
                break;
            case '?':
                if (optopt == 't') {
                    fprintf(stderr, "Option -%c requires an argument!\n", optopt);
                } else if (optopt == 'r') {
                    fprintf(stderr, "Option -%c requires an argument!\n", optopt);
                } else if (optopt == 'R') {
                    fprintf(stderr, "Option -%c requires an argument!\n", optopt);
                } else if (isprint(optopt)) {
                    fprintf(stderr, "Unknow Option -%c!\n", optopt);
                } else {
                    fprintf(stderr, "Unknown Option character '\\x%x'!\n", optopt);
                }
                return -1;
            default:
                fprintf(stderr, "Something did go wrong. Please use '-h' for help!\n");
        }    
        for (i = optind; i < argc; i++) {
            fprintf(stderr, "Non-option argument %s!\n", argv[i]);
            exit(-1);
        }
    }

    if (hflag == 1 && tflag == 0) {
        printHelp();
        exit(0);
    } else if (hflag == 1) {
        printHelp();
    }

    /*******************************************
    * Initialise thread attributes here        *
    *******************************************/
    pthread_t threads[NumberOfThreads];
    long thread_args[NumberOfThreads];
    int check_code;

    /* 
     * Create Threads
     */
    for (t = 0; t < NumberOfThreads; t++) {
        thread_args[t] = t;
        printf("Creating thread %ld\n", t);
        check_code = pthread_create(&threads[t], NULL, PrintHello, (void*) &thread_args[t]);
        /* Check if everything gone right */
        assert(check_code == 0);
        /* verbose output */
        if (vflag == 1) {
            printf("Thread %ld is creted and will do his job now!\n", t);
        }
    }
    for (t = 0; t < NumberOfThreads; t++) {
        check_code = pthread_join(threads[t], &status);
        assert(check_code == 0);
        printf("Main joining with thread %ld\n", t);
        if (vflag == 1) {
            printf("Completed joining with thread %ld status = %d\n", t, (int) status);
        }
    }
    return (0);
}

1 个解决方案

#1


2  

Your for loop where you display your Non-option values is inside your while loop where you call getopt(). Move it so it is after your while loop that contains the call to getopt().

显示非选项值的for循环位于while循环中,您调用getopt()。移动它,使它在包含对getopt()的调用的while循环之后。

As it stands your while loop is seeing the first option and then processing the for loop before it is done processing all the options.

因为它代表你的while循环正在查看第一个选项,然后在处理所有选项之前处理for循环。

#1


2  

Your for loop where you display your Non-option values is inside your while loop where you call getopt(). Move it so it is after your while loop that contains the call to getopt().

显示非选项值的for循环位于while循环中,您调用getopt()。移动它,使它在包含对getopt()的调用的while循环之后。

As it stands your while loop is seeing the first option and then processing the for loop before it is done processing all the options.

因为它代表你的while循环正在查看第一个选项,然后在处理所有选项之前处理for循环。