2

I started recently to use argp but i can't find a way to make my option required. I want to see the help when -u (--url) and -w (--wordlist) is not given. Both of them need to be required in order for my programme to work. I look on the internet but can't find the same problem anywhere else. Thanks for the help

argp.c

#include "../header/argp.h"

#include <argp.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>

const char *argp_program_version = "WiBreak 0.0.1";
static char doc[] = "Program used to brute force URL.";
static char args_doc[] = "[-u URL] [-w WORDLIST]";
static struct argp_option options[] = {
    {"url", 'u', "URL_TO_QUERY", 0, "Compare lines instead of characters."},
    {"wordlist", 'w', "WORDLIST", 0, "Compare words instead of characters."},
    {"verbose", 'v', 0, OPTION_ARG_OPTIONAL, "Show more info"},
    {0}};


// TODO: Need to handle when no argument are given.
static error_t parse_opt(int key, char *arg, struct argp_state *state) {
    struct arguments *arguments = state->input;
    int *arg_cout = state->input;
    switch (key) {
        case 'u':
            arguments->url_to_query = arg;
            // printf("arg : %s\n", arg);
            break;
        case 'w':
            arguments->wordlist = arg;
            break;
        case 'v':
            arguments->verbose = 1;
            break;
        default:
            // argp_usage(state);
            return ARGP_ERR_UNKNOWN;
    }
    // Code never reach even when i initialyze arguments in the main.
    if (arguments->wordlist == "" || arguments->url_to_query == ""){
        argp_failure(state, 1, 0, "too few arguments");
        exit(ARGP_ERR_UNKNOWN);
    }
    
    return 0;
}

struct argp argp = {options, parse_opt, args_doc, doc};

argp.h

#ifndef __ARGP__H
#define __ARGP__H
#include <argp.h>
#include <stdbool.h>

// static error_t parse_opt(int key, char *arg, struct argp_state *state)
extern struct argp argp;

struct arguments {
    // enum { CHARACTER_MODE, WORD_MODE, LINE_MODE } mode;
    char *wordlist;
    char *url_to_query;
    bool verbose;
    // bool isCaseInsensitive;
};


#endif

main.c

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

    // arguments.mode = CHARACTER_MODE;
    // arguments.isCaseInsensitive = false;
    arguments.wordlist = "";
    arguments.url_to_query = "";

    argp_parse(&argp, argc, argv, 0, 0, &arguments);

    // printf("Wordlist : %s, Url : %s\n", arguments.wordlist,
    // arguments.url_to_query);
    // ...
}

EDIT

Turn out i manage to answer my question by changing the parse_opt as follow

case ARGP_KEY_END:
            // printf("arguments->wordlist : %s, arguments->url_to_query : %s\n", arguments->wordlist, arguments->url_to_query);
            if (arguments->wordlist == NULL || arguments->url_to_query == NULL){
                argp_failure(state, 1, 0, "required -u and -w. See --help for more information");
                exit(ARGP_ERR_UNKNOWN);
            }
RaiZy_Style
  • 103
  • 10
  • 1
    `__ARGP__H` Do not use identifiers starting with double underscore in your code. They are reserved. – KamilCuk Oct 13 '21 at 12:27
  • `I want to see the help when -u (--url) and -w (--wordlist) is not given` So add `argp_usage()` before `argp_failure(state, 1, 0, "too few arguments");` ? I would also change `"too few arguments"` to `"you have to give -u and -w options"`. But if the options are required, why not take them as positional arguments anyway? – KamilCuk Oct 13 '21 at 12:31
  • 1
    @RaiZy_Style - As you _manage to answer_ your _question_, it would be good style to post this as an Answer rather than in the Question. – Armali Oct 14 '21 at 18:36

1 Answers1

0

I change the argp file and init my struct to null in the main.

See code under argp.c

#include "../header/argp.h"

#include <argp.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>

const char *argp_program_version = "WiBreak 0.0.1";
const char *argp_program_bug_address = "arthur.guyotpremel@gmail.com";
static char doc[] = "Program used to brute force URL.";
static char args_doc[] = "[-u URL] [-w WORDLIST]";
static struct argp_option options[] = {
    {"url", 'u', "URL_TO_QUERY", 0, "Compare lines instead of characters [REQUIRED]."},
    {"wordlist", 'w', "WORDLIST", 0, "Compare words instead of characters [REQUIRED]."},
    {"verbose", 'v', 0, OPTION_ARG_OPTIONAL, "Show more info"},
    {0}};


// TODO: Need to handle when no argument are given.
static error_t parse_opt(int key, char *arg, struct argp_state *state) {
    struct arguments *arguments = state->input;
    int *arg_cout = state->input;
    switch (key) {
        case 'u':
            arguments->url_to_query = arg;
            // printf("arg : %s\n", arg);
            break;
        case 'w':
            arguments->wordlist = arg;
            break;
        case 'v':
            arguments->verbose = 1;
            break;
        
        case ARGP_KEY_END:
            // printf("arguments->wordlist : %s, arguments->url_to_query : %s\n", arguments->wordlist, arguments->url_to_query);
            if (arguments->wordlist == NULL || arguments->url_to_query == NULL){
                argp_failure(state, 1, 0, "required -u and -w. See --help for more information");
                exit(ARGP_ERR_UNKNOWN);
            }

        default:
            // argp_usage(state);
            return ARGP_ERR_UNKNOWN;
    }
    
    return 0;
}

struct argp argp = {options, parse_opt, args_doc, doc};

main.c

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

    // arguments.mode = CHARACTER_MODE;
    // arguments.isCaseInsensitive = false;
    arguments.wordlist = NULL;
    arguments.url_to_query = NULL;

    argp_parse(&argp, argc, argv, 0, 0, &arguments);

    // printf("Wordlist : %s, Url : %s\n", arguments.wordlist,arguments.url_to_query);
    // arguments.url_to_query);
    // ...
}
RaiZy_Style
  • 103
  • 10
  • If you need to add additional information, you can do so by clicking the "edit" button under the question, instead of posting an answer. StackOverflow does not use a forum-style posting system. Please take a look at the help section for more info: https://stackoverflow.com/help/how-to-answer – Z4-tier Oct 13 '21 at 15:48