title: C argp : Handle option require
body: <p>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. <strong>Both</strong> 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</p>
  <p>argp.c</p>
  <pre><code>#include &quot;../header/argp.h&quot;
  
  #include &lt;argp.h&gt;
  #include &lt;stdbool.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  
  const char *argp_program_version = &quot;WiBreak 0.0.1&quot;;
  static char doc[] = &quot;Program used to brute force URL.&quot;;
  static char args_doc[] = &quot;[-u URL] [-w WORDLIST]&quot;;
  static struct argp_option options[] = {
      {&quot;url&quot;, 'u', &quot;URL_TO_QUERY&quot;, 0, &quot;Compare lines instead of characters.&quot;},
      {&quot;wordlist&quot;, 'w', &quot;WORDLIST&quot;, 0, &quot;Compare words instead of characters.&quot;},
      {&quot;verbose&quot;, 'v', 0, OPTION_ARG_OPTIONAL, &quot;Show more info&quot;},
      {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-&gt;input;
      int *arg_cout = state-&gt;input;
      switch (key) {
          case 'u':
              arguments-&gt;url_to_query = arg;
              // printf(&quot;arg : %s\n&quot;, arg);
              break;
          case 'w':
              arguments-&gt;wordlist = arg;
              break;
          case 'v':
              arguments-&gt;verbose = 1;
              break;
          default:
              // argp_usage(state);
              return ARGP_ERR_UNKNOWN;
      }
      // Code never reach even when i initialyze arguments in the main.
      if (arguments-&gt;wordlist == &quot;&quot; || arguments-&gt;url_to_query == &quot;&quot;){
          argp_failure(state, 1, 0, &quot;too few arguments&quot;);
          exit(ARGP_ERR_UNKNOWN);
      }
      
      return 0;
  }
  
  struct argp argp = {options, parse_opt, args_doc, doc};
  </code></pre>
  <p>argp.h</p>
  <pre><code>#ifndef __ARGP__H
  #define __ARGP__H
  #include &lt;argp.h&gt;
  #include &lt;stdbool.h&gt;
  
  // 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
  </code></pre>
  <p>main.c</p>
  <pre><code>int main(int argc, char *argv[]) {
      struct arguments arguments;
  
      // arguments.mode = CHARACTER_MODE;
      // arguments.isCaseInsensitive = false;
      arguments.wordlist = &quot;&quot;;
      arguments.url_to_query = &quot;&quot;;
  
      argp_parse(&amp;argp, argc, argv, 0, 0, &amp;arguments);
  
      // printf(&quot;Wordlist : %s, Url : %s\n&quot;, arguments.wordlist,
      // arguments.url_to_query);
      // ...
  }
  </code></pre>
  <h1>EDIT</h1>
  <p>Turn out i manage to answer my question by changing the parse_opt as follow</p>
  <pre><code>case ARGP_KEY_END:
              // printf(&quot;arguments-&gt;wordlist : %s, arguments-&gt;url_to_query : %s\n&quot;, arguments-&gt;wordlist, arguments-&gt;url_to_query);
              if (arguments-&gt;wordlist == NULL || arguments-&gt;url_to_query == NULL){
                  argp_failure(state, 1, 0, &quot;required -u and -w. See --help for more information&quot;);
                  exit(ARGP_ERR_UNKNOWN);
              }
  </code></pre>