argp.h: Parameter soll mandatory sein



  • Hallo,

    ich arbeite gerade ein bisschen mit argp.h um die Parameter zu parsen, die meinem Programm uebergeben werden. Funktioniert soweit alles, bis auf eine ganz kleine Kleinigkeit. Folgender Code:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <argp.h>
    
    const char *argp_program_version = "grep 1.0";
    const char *argp_program_bug_address = "<xxxxxx@gmail.com>";
    static char doc[] = "A very simple implementation for the grep command";
    static char args_doc[] = "[filename]";
    static struct argp_option options[] = {
    	{"word",   'w', "WORD",  0, "The word to match/not match" },
    	{"invert-match",  'v', 0,      0,  "Invert the match result" },
    	{"number", 'n', 0, 0, "Print the line numbers" },
    	{ 0 }
    };
    
    typedef struct {
    	char *filename;
    	char *word;
    	bool showNumbers;
    	bool invertMatch;
    } arguments_t;
    
    static error_t parse_opt (int key, char *arg, struct argp_state *state) {
    	arguments_t *arguments = state->input;
    
    	switch (key) {
    		case 'w':
    			arguments->word = arg;
    			break;
    		case 'v':
    			arguments->invertMatch = true;
    			break;
    		case 'n':
    			arguments->showNumbers = true;
    			break;
    		case ARGP_KEY_NO_ARGS:
    			argp_usage(state);
    			break;
    		case ARGP_KEY_ARG:
    			arguments->filename = arg;
    			break;
    		default:
    			return ARGP_ERR_UNKNOWN;
    	}
    	return 0;
    }
    
    static struct argp argp = { options, parse_opt, args_doc, doc };
    
    int main (int argc, char **argv) {
    	arguments_t arguments;
    	arguments.filename = NULL;
    	arguments.word = NULL;
    	arguments.invertMatch = false;
    	arguments.showNumbers = false;
    
    	argp_parse(&argp, argc, argv, 0, 0, &arguments);
    
    	printf("filename: %s\n", arguments.filename);
    	printf("word: %s\n", arguments.word);
    	printf("show numbers: %d\n", arguments.showNumbers);
    	printf("inverse lines: %d\n", arguments.invertMatch);
    
    	return 0;
    }
    

    Mein Problem ist der "-w" Parameter. Der ist Pflicht und muss immer angegeben werden. Egal welche Parameter sonst so angegeben werden. Kann mir vielleicht jemand dabei helfen? Hab schon gegoogelt aber noch nichts gefunden.

    lg


Anmelden zum Antworten