Next: , Up: Writing a Client Application   [Contents]


4.3.1 Handling command-line options

Options can then be added easily by adding global variables and expanding the options array. For example, the following would add a string-option and a binary flag (defaulting to NULL and GNUNET\_NO respectively):

static char *string_option;
static int a_flag;

// ...
  struct GNUNET_GETOPT_CommandLineOption options[] = {
    GNUNET_GETOPT_option_string ('s', "name", "SOMESTRING",
     gettext_noop ("text describing the string_option NAME"),
     &string_option},
    GNUNET_GETOPT_option_flag ('f', "flag",
     gettext_noop ("text describing the flag option"), 
     &a_flag),
    GNUNET_GETOPT_OPTION_END
  };
  string_option = NULL;
  a_flag = GNUNET_SYSERR;
// ...

Issues such as displaying some helpful text describing options using the --help argument and error handling are taken care of when using this approach. Other GNUNET\_GETOPT\_-functions can be used to obtain integer value options, increment counters, etc. You can even write custom option parsers for special circumstances not covered by the available handlers. To check if an argument was specified by the user you initialize the variable with a specific value (e.g. NULL for a string and GNUNET\_SYSERR for a integer) and check after parsing happened if the values were modified.

Inside the run method, the program would perform the application-specific logic, which typically involves initializing and using some client library to interact with the service. The client library is supposed to implement the IPC whereas the service provides more persistent P2P functions.

Exercise: Add a few command-line options and print them inside of run. What happens if the user gives invalid arguments?