1

after reading a famous pdf about argp, I wanted to make something with it, but I'm having a problem, in this example:

static int parse_opt (int key, char *arg, struct argp_state *state)
{
    switch (key)
    {
        case 'd':
        {
            unsigned int i;
            for (i = 0; i < atoi (arg); i++)
                printf (".");
            printf ("\n");
            break;
        }
    }
    return 0;
}

int main (int argc, char **argv)
{
    struct argp_option options[] = 
    {
        { "dot", 'd', "NUM", 0, "Show some dots on the screen"},
        { 0 }
    };
    struct argp argp = { options, parse_opt, 0, 0 };
    return argp_parse (&argp, argc, argv, 0, 0, 0);
}

The -d accepts an argument of int type, but if I want to get a char or char array as an argument? The pdf doesn't covers that neither the docs.

I'm beginning to learn C, I know it in a basic way, I'm more familiar with other lenguages, so to learn more about it I want to archive this but I don'get it how can I make it accept a char array.

Code that didn't work when comparing arg with a char:

static int parse_opt(int key, char *arg, struct argp_state *state)
{       
    switch(key)
    {
        case 'e':
        {
            //Here I want to check if "TOPIC" has something, in this case, a char array
            //then based on that, do something.
            if (0 == strcmp(arg, 'e'))
            {
                printf("Worked");
            }
        }
    }

    return 0;
}//End of parse_opt

int main(int argc, char **argv)
{
    struct argp_option options[] = 
    {
        {"example", 'e', "TOPIC", 0, "Shows examples about a mathematical topic"},
        {0}
    };

    struct argp argp = {options, parse_opt};

    return argp_parse (&argp, argc, argv, 0, 0, 0); 
}//End of main

Thanks in advance.

Diego Francisco
  • 1,650
  • 1
  • 17
  • 31
  • Mind clarifying `The -d accepts an argument of int type,` ? – sjsam Dec 20 '16 at 04:04
  • 2
    " -d accepts an argument of int type". That's not true. `arg` is always a string. It's your code that is changing it to an `int`. If you want to store it as a string then just don't call `atoi`. – kaylum Dec 20 '16 at 04:10
  • @sjsam Sure, there's a unsigned int called i, that has a relation with "NUM" tha is -d's argument, "NUM" argument justs accepts integers and I want it to accept a char or char array, I hope I was clear – Diego Francisco Dec 20 '16 at 04:12
  • @kaylum The code above isn't my actual project, I got it from the pdf, in my code I tried to compare arg with a char but it didn'work because it says that it expected const char but argument is of type int, bear with me if I'm being a total noob. – Diego Francisco Dec 20 '16 at 04:16
  • 1
    Well, then show the code that you tried that did not work. Can't point out the error if we can't see the code. – kaylum Dec 20 '16 at 04:17
  • @Stargateur I basically want to make -e option accept an argument of type char instead of an int, note that I'm using argp lib – Diego Francisco Dec 20 '16 at 04:35

1 Answers1

1

https://www.gnu.org/software/libc/manual/html_node/Argp.html

#include <stdio.h>
#include <argp.h>
#include <string.h>

static int parse_opt(int key, char *arg, struct argp_state *state) {
  (void)state; // We don't use state
  switch (key) {
  case 'c': {
    if (strlen(arg) == 1) { // we only want one char
      char c = *arg;        // or arg[0]
      printf("my super char %c !!!\n", c);
    } else {
      return 1;
    }
  }
  }
  return 0;
}

int main(int argc, char **argv) {
  struct argp_option const options[] = {
      {"char", 'c', "c", 0, "a super char", 0}, {0}};
  struct argp const argp = {options, &parse_opt, NULL, NULL, NULL, NULL, NULL};
  argp_parse(&argp, argc, argv, 0, NULL, NULL);
}
Stargateur
  • 24,473
  • 8
  • 65
  • 91
  • I kinda get it, but can you explain the part in the struct argp when it says NULL, examples in the doc don't show something like that or at least in the example I'm seeing. – Diego Francisco Dec 20 '16 at 05:21
  • It's just to avoir warning because `struct argp` has a lot of field, [doc](https://www.gnu.org/software/libc/manual/html_node/Argp-Parsers.html#Argp-Parsers). To know what is `NULL` read [this](http://stackoverflow.com/questions/1296843/what-is-the-difference-between-null-0-and-0) – Stargateur Dec 20 '16 at 05:23