Next: , Previous: Group options, Up: Top   [Contents][Index]


6 ¥³¥ó¥Õ¥£¥°¥ì¡¼¥·¥ç¥ó¥Õ¥¡¥¤¥ë

¥³¥Þ¥ó¥É¥é¥¤¥ó¥ª¥×¥·¥ç¥ó¤òľÀÜ¥³¥ó¥Õ¥£¥°¥ì¡¼¥·¥ç¥ó¥Õ¥¡¥¤¥ë¤Ç»ØÄꤹ¤ë¤È ÊØÍø¤Ê¤È¤­¤â¿¤¤¤Î¤Ç¡¤¥ª¥×¥·¥ç¥ó¤ÎÃͤϥ³¥Þ¥ó¥É¥é¥¤¥ó¥ª¥×¥·¥ç¥ó¤¬Í¿¤¨¤é ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¡¤¤³¤Î¥Õ¥¡¥¤¥ë¤«¤éÆÉ¤ß¤³¤Þ¤ì¤ë¤è¤¦¤Ë¤·¤Þ¤¹¡¥¥³¥Þ¥ó¥É¥é¥¤ ¥ó¥ª¥×¥·¥ç¥ó--conf-parser¤¬gengetopt¤ÇÍ¿¤¨¤é¤ì¤Æ¤¤¤ë¾ì¹ç¡¤É¸½àŪ ¤Ê¥³¥Þ¥ó¥É¥é¥¤¥ó¥ª¥×¥·¥ç¥ó¤Î¹½Ê¸²òÀÏ´ï°Ê³°¤Ë¡¤°Ê²¼¤ÎÄɲäι½Ê¸²òÀϴ郎 À¸À®¤µ¤ì¤Þ¤¹(̾Á°¤Ï<commandline_parser>_configfile¤Ç¤¹)¡¥

int
<cmd_parser_name>_configfile (char * const filename,
                              struct gengetopt_args_info *args_info,
                              int override,
                              int initialize,
                              int check_required);

¥Ñ¥é¥á¡¼¥¿override¤Ï¡¤¥³¥ó¥Õ¥£¥°¥ì¡¼¥·¥ç¥ó¥Õ¥¡¥¤¥ë¤«¤éÆÉ¤ß¹þ¤Þ¤ì ¤¿Ãͤò¡¤¥³¥Þ¥ó¥É¥é¥¤¥ó¤Ç»ØÄꤵ¤ì¤¿¤â¤Î¤ËÍ¥À褵¤»¤ëɬÍפ¬¤¢¤ë¤«¤É¤¦¤«¤ò ÅÁ¤¨¤Þ¤¹¡¥initialize¤Ï¡¤args_info¹½Â¤ÂΤò½é´ü²½¤¹¤ëɬÍפ¬¤¢¤ë¤« ¤É¤¦¤«¤òÅÁ¤¨¤Þ¤¹¡¥

½ÅÍס§¥³¥ó¥Õ¥£¥°¥ì¡¼¥·¥ç¥ó¥Õ¥¡¥¤¥ë¤Î¹½Ê¸²òÀÏ´ï¤ò¡¤É¸½à¤Î¥³¥Þ¥ó¥É¥é¥¤¥ó ¥ª¥×¥·¥ç¥ó¹½Ê¸²òÀÏ´ï¤ÎÁ°¤Ë¸Æ¤Ó½Ð¤¹¾ì¹ç¡¤ÌÀ¼¨Åª¤Ëinitialize¤ò¿¿¤Ë ÀßÄꤹ¤ëɬÍפ¬¤¢¤ê(Î㤨¤Ð¡¤0¤Ç¤Ï¤Ê¤¤¤â¤Î)¡¤¤½¤¦¤·¤Ê¤±¤ì¤ÐͽÁۤǤ­¤Ê¤¤·ë ²Ì¤¬É½¼¨¤µ¤ì¤ë²ÄǽÀ­¤¬¤¢¤ê¤Þ¤¹¡¥

check_required¤Ï¡¤É¬¿Ü¤Î¥ª¥×¥·¥ç¥ó¤òÄ´ºº¤¹¤ëɬÍפ¬¤¢¤ë¤«¤É¤¦¤«¤ò ÅÁ¤¨¤Þ¤¹¡¥

¥³¥ó¥Õ¥£¥°¥ì¡¼¥·¥ç¥ó¥Õ¥¡¥¤¥ë¤Î¹½Ê¸²òÀÏ´ï¤òɸ½à¤Î¥³¥Þ¥ó¥É¥é¥¤¥ó¥ª¥×¥·¥ç ¥ó¤Î¹½Ê¸²òÀÏ´ï¤ÎÁ°¤Ë¸Æ¤Ó½Ð¤¹¾ì¹ç¤Ç¡¤É¸½à¤Î¥³¥Þ¥ó¥É¥é¥¤¥ó¤Î¹½Ê¸²òÀÏ´ï¤ò ¸Æ¤Ó½Ð¤·¤¿¤¤¾ì¹ç¡¤°Ê²¼¤ÎÆóÈÖÌܤΥС¼¥¸¥ç¥ó¤ò»ÈÍѤ¹¤ëɬÍפ¬¤¢¤ê¡¤¥³¥ó¥Õ¥£ ¥°¥ì¡¼¥·¥ç¥ó¥Õ¥¡¥¤¥ë¤Ç¼ý½¸¤·¤¿Ãͤò¼º¤ï¤Ê¤¤¤è¤¦¤Ë¡¤½é´ü²½¤Ç0¤òÅϤ¹É¬Íפ¬ ¤¢¤ê¤Þ¤¹¡¥

int
<cmd_parser_name>2 (int argc,
                    char * const *argv,
                    struct gengetopt_args_info *args_info,
                    int override,
                    int initialize,
                    int check_required);

Ãí°Õ¡§¤³¤Î¥Ð¡¼¥¸¥ç¥ó¤òÍѤ¤¤ë¤È¡¤¥³¥Þ¥ó¥É¥é¥¤¥ó¤ÇÅϤ¹¥ª¥×¥·¥ç¥ó¤ò¥³¥ó¥Õ¥£ ¥°¥ì¡¼¥·¥ç¥ó¥Õ¥¡¥¤¥ë¤«¤éÆÉ¤ß¹þ¤ó¤À¤â¤Î¤ËÍ¥À褵¤»¤ëɬÍפ¬¤¢¤ë¤«¤É¤¦¤«¤ò »ØÄꤹ¤ë¤³¤È¤â²Äǽ¤Ç¤¹¡¥override¤ò0¤ËÀßÄꤷ¡¤¥³¥ó¥Õ¥£¥°¥ì¡¼¥·¥ç ¥ó¥Õ¥¡¥¤¥ë¤«¤é´û¤ËÆÉ¤ß¹þ¤Þ¤ì¤Æ¤¤¤ë¤â¤Î¤ËÂФ·¡¤¥³¥Þ¥ó¥É¥é¥¤¥ó¥ª¥×¥·¥ç¥ó ¤¬Í¿¤¨¤é¤ì¤¿¾ì¹ç¡¤¥¨¥é¡¼¤¬È¯À¸¤·¤Þ¤¹¡¥¤µ¤é¤Ë¡¤Â­¤ê¤Ê¤¤É¬¿Ü¥ª¥×¥·¥ç¥ó¤Î Ä´ºº¤ò¤¹¤ëɬÍפ¬¤¢¤ë¤«¤É¤¦¤«¤ò»ØÄꤹ¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¡¥¤³¤ì¤Ï¡¤É¬¿Ü¥°¥ë¡¼ ¥×¤Î¥ª¥×¥·¥ç¥ó¤Ë¤â´Ø·¸¤·¤Þ¤¹(Group options)¡¥

ɬ¿Ü¥ª¥×¥·¥ç¥ó¤ÎÄ´ºº¤òÍ׵ᤷ¤Ê¤¤¤³¤È¤Ë·è¤á¤¿¾ì¹ç¡¤¥³¥Þ¥ó¥É¥é¥¤¥ó¤Î¹½Ê¸ ²òÀϤ¬¡¤°Ê²¼¤Î¤è¤¦¤ËÀ¸À®¤µ¤ì¤¿´Ø¿ô¤ò»ÈÍѤ·¤ÆÌá¤Ã¤¿¸å¤Ç¡¤¼êư¤ÇÄ´ºº¤¹¤ë ¤³¤È¤¬²Äǽ¤Ç¤¹¡¥

int
<cmd_parser_name>_required (struct gengetopt_args_info *args_info,
                            const char *program_name);

¤³¤³¤Ç¡¤program_name¤Ï¼Â¹Ô·Á¼°¤Î̾Á°¤Ç¤¹(Ä̾°ú¿ô¤È¤·¤Æ argv[0]¤ÇÅϤ·¤Þ¤¹)¡¥´Ø¿ô¤¬0°Ê³°¤ÎÃͤòÊÖ¤¹¾ì¹ç¡¤É¬¿Ü¥ª¥×¥·¥ç¥ó¤Ë ­¤ê¤Ê¤¤¤â¤Î¤¬¤¢¤ê¤Þ¤¹¡¥¥¨¥é¡¼¤Ï¤³¤Î´Ø¿ô¤Ç´û¤Ë½ÐÎϤµ¤ì¤Æ¤¤¤Þ¤¹¡¥¤³¤ì¤Ï¡¤ ɬ¿Ü¥°¥ë¡¼¥×¤Î¥ª¥×¥·¥ç¥ó¤Ë¤â´Ø·¸¤·¤Þ¤¹(Group options)¡¥

¥³¥ó¥Õ¥£¥°¥ì¡¼¥·¥ç¥ó¥Õ¥¡¥¤¥ë¤Ï¡¤°Ê²¼¤Î¤è¤¦¤Êñ½ã¤Ê¹½Ê¸¤Ë¤Ê¤Ã¤Æ¤¤¤Þ¤¹¡¥ ‘#’¤Ç»Ï¤Þ¤ë¹Ô¤Ï¥³¥á¥ó¥È¤È¹Í褵¤ì¤Þ¤¹¡¥

<option_name> = {<option_val>} 

¤Þ¤¿¤Ï¡¤(¥ª¥×¥·¥ç¥ó¤¬°ú¿ô¤ò¼è¤é¤Ê¤¤¾ì¹ç)ñ½ã¤Ë°Ê²¼¤Î¤è¤¦¤Ë¤Ê¤Ã¤Æ¤¤¤Þ¤¹¡¥

<option_name>

¤³¤ì¤Ï¡¤option_name¤¬Í¿¤¨¤é¤ì¤Æ¤¤¤Æ¡¤°ú¿ô¤ò¤¦¤±¤¤¤ì¤ë¾ì¹ç¤¬Ãͤ¬ option_val¤Ë¤Ê¤Ã¤Æ¤¤¤ë¤³¤È¤ò°ÕÌ£¤·¤Þ¤¹¡¥=¤Ïɬ¿Ü¤Ç¤Ï¤¢¤ê¤Þ ¤»¤ó¡¥

Î㤨¤Ð¡¤¤³¤Îµ¡Ç½¤ò»ÈÍѤ·¤Æ¤¤¤ë°Ê²¼¤Î¥×¥í¥°¥é¥à¤ò¹Í¤¨¤Þ¤¹(¤³¤ì¤Ï¥Æ¥¹¥È¤Î test_conf_parser¤Ç¤¹)¡¥

/* test_conf_parser.c test */

/* test all kinds of options and the conf file parser */

#include <stdlib.h>
#include <stdio.h>

#include "test_conf_parser_cmd.h"

static struct my_args_info args_info;

int
main (int argc, char **argv)
{  
  int i;
  int result = 0;

  if (test_conf_parser_cmd_parser (argc, argv, &args_info) != 0) {
    result = 1;
    goto stop;
  }

  /* override cmd options, but do not initialize args_info, check for required options */
  if (test_conf_parser_cmd_parser_configfile
      (args_info.conf_file_arg, &args_info, 1, 0, 1) != 0) 
    {
      result = 1;
      goto stop;
    }

  printf ("value of required: %s\n", args_info.required_arg);
  printf ("value of string: %s\n", args_info.string_arg);
  printf ("value of no-short_given: %d\n", args_info.no_short_given);
  printf ("value of int: %d\n", args_info.int_arg);
  printf ("value of float: %f\n", args_info.float_arg);

  printf ("value of multi-string_given: %d\n", args_info.multi_string_given);
  for (i = 0; i < args_info.multi_string_given; ++i)
    printf ("  value of multi-string: %s\n", args_info.multi_string_arg [i]);

  printf ("value of multi-string-def_given: %d\n",
          args_info.multi_string_def_given);
  for (i = 0; i < args_info.multi_string_def_given; ++i)
    printf ("  value of multi-string-def: %s\n",
            args_info.multi_string_def_arg [i]);
  if (!args_info.multi_string_def_given && args_info.multi_string_def_arg [0])
    printf ("default value of multi-string-def: %s\n",
            args_info.multi_string_def_arg [0]);

  printf ("value of opta: %s\n", args_info.opta_arg);

  printf ("noarg given %d times\n", args_info.noarg_given);
  printf ("noarg_noshort given %d times\n", args_info.noarg_noshort_given);

  printf ("opt-arg given: %d\n", args_info.opt_arg_given);
  printf ("opt-arg value: %s\n", (args_info.opt_arg_arg ? args_info.opt_arg_arg : "not given"));

  if (args_info.file_save_given) {
    if (test_conf_parser_cmd_parser_file_save (args_info.file_save_arg, &args_info) == EXIT_FAILURE)
      result = 1;
    else
      printf ("saved configuration file %s\n", args_info.file_save_arg);
  }

 stop:
  test_conf_parser_cmd_parser_free (&args_info);

  return result;
}

¤½¤·¤Æ¡¤°Ê²¼¤Î¥³¥ó¥Õ¥£¥°¥ì¡¼¥·¥ç¥ó¥Õ¥¡¥¤¥ë¤ò»ÈÍѤ·¤Æ¤¤¤ë¾õ¶·¤ò¹Í¤¨¤Þ¤¹¡¥

# required option
required "this is a test"
float 3.14
no-short
string another

¤½¤·¤Æ¡¤test_conf_parser¤ò°Ê²¼¤Î¤è¤¦¤Ë¼Â¹Ô¤·¤Þ¤¹¡¥

./test_conf_parser -r bar -i 100 --float 2.14 --conf-file test_conf.conf 
value of required: this is a test
value of string: another
value of no-short: 1
value of int: 100
value of float: 3.140000

Âå¤ï¤ê¤Ë¡¤°ú¿ô¤ËÍ¥À褵¤»¤ë¤¿¤átest_conf_parser_cmd_parser_configfile¤ò0¤È¤·¤Æ¸Æ¤Ó½Ð¤¹¾ì¹ç¡¤°Ê²¼¤Î¤è¤¦¤Ê·ë²Ì¤Ë¤Ê¤ê¤Þ¤¹¡¥

value of required: bar
value of string: another
value of no-short: 1
value of int: 100
value of float: 2.140000

¤³¤ÎÆóÈÖÌܤÎÎã¤Ç¤Ï¡¤ÆóÈÖÌܤΥС¼¥¸¥ç¥ó¤Î¥³¥Þ¥ó¥É¥é¥¤¥ó¹½Ê¸²òÀÏ´ï¤ò»ÈÍÑ ¤·¤Æ¤¤¤Þ¤¹¡¥ºÇ½é¤Ë¥³¥ó¥Õ¥£¥°¥ì¡¼¥·¥ç¥ó¥Õ¥¡¥¤¥ë¹½Ê¸²òÀÏ´ï¤ò¸Æ¤Ó½Ð¤·¡¤¤½ ¤ì¤«¤é¥³¥Þ¥ó¥É¥é¥¤¥ó¹½Ê¸²òÀÏ´ï¤ò¸Æ¤Ó½Ð¤·¤Þ¤¹(¥³¥Þ¥ó¥É¥é¥¤¥ó¥ª¥×¥·¥ç¥ó¤Ï¡¤ ¥³¥ó¥Õ¥£¥°¥ì¡¼¥·¥ç¥ó¥Õ¥¡¥¤¥ë¤Î¥ª¥×¥·¥ç¥ó¤ËÍ¥À褷¤Þ¤¹)¡¥

/* test_conf_parser_ov2.c test */

/* test all kinds of options and the conf file parser */
/* differently from test_conf_parser_ov.c, first scan the conf file and
   then the command line */

#include <stdlib.h>
#include <stdio.h>

#include "test_conf_parser_cmd.h"

static struct my_args_info args_info;

int
main (int argc, char **argv)
{  
  /* do not override command line options, initialize args_info */
  if (test_conf_parser_cmd_parser_configfile
      ("./test_conf2.conf", &args_info, 0, 1, 0) != 0)
    exit(1);

  if (test_conf_parser_cmd_parser2 (argc, argv, &args_info, 1, 0, 1) != 0)
    exit(1) ;

  printf ("value of required: %s\n", args_info.required_arg);
  printf ("value of string: %s\n", args_info.string_arg);
  printf ("value of no-short_given: %d\n", args_info.no_short_given);
  printf ("value of int: %d\n", args_info.int_arg);
  printf ("value of float: %f\n", args_info.float_arg);

  test_conf_parser_cmd_parser_free (&args_info);

  return 0;
}

°Ê²¼¤Î¤è¤¦¤Ë¸Æ¤Ó½Ð¤·¤¿¤È¤­¤Î·ë²Ì¤Ç¤¹¡¥

./test_conf_parser_ov2 -r "bar" --float 2.14 -i 100
value of required: bar
value of string: another
value of no-short: 1
value of int: 100
value of float: 2.140000

6.1 ¥³¥ó¥Õ¥£¥°¥ì¡¼¥·¥ç¥ó¥Õ¥¡¥¤¥ë¤Î¹½Ê¸²òÀÏ´ï¤Î¾ÜºÙ

À¸À®¤µ¤ì¤ë¥³¥ó¥Õ¥£¥°¥ì¡¼¥·¥ç¥ó¥Õ¥¡¥¤¥ë¤Î¹½Ê¸²òÀÏ´Ø¿ô¤Ï¡¤¥³¥ó¥Õ¥£¥°¥ì¡¼ ¥·¥ç¥ó¥Õ¥¡¥¤¥ë¤Î¤½¤ì¤¾¤ì¤Î¹Ô¤òÆÉ¤ß¹þ¤à¤È¤­¡¤Äê¿ô CONFIG_FILE_LINE_SIZE¤ò»ÈÍѤ·¤Æ¤¤¤Þ¤¹¡¥¤³¤ÎÄê¿ô¤Î¥Ç¥Õ¥©¥ë¥È¤Ï 2048¤ËÀßÄꤵ¤ì¤Æ¤¤¤Æ¡¤¤Û¤È¤ó¤É¤Î¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Ç½½Ê¬¤Ç¤·¤ç¤¦¡¥ ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Ç¡¤¤½¤ì°Ê¾å¤ÎŤµ¤Î¹Ô¤¬¤¢¤ë¥³¥ó¥Õ¥£¥°¥ì¡¼¥·¥ç¥ó¥Õ¥¡¥¤ ¥ë¤ò»ÈÍѤ·¤Æ¤¤¤ë¾ì¹ç¡¤gcc¤Î-D¥³¥Þ¥ó¥É¥é¥¤¥ó¥ª¥×¥·¥ç¥ó¤òÍÑ ¤¤¤Æ¡¤¤³¤ÎÄê¿ô¤ÎÃͤòÌÀ¼¨Åª¤Ë»ØÄꤷ¤Æ¡¤C¥Õ¥¡¥¤¥ë¤Î¥³¥ó¥Ñ¥¤¥ë¤ò¤¹¤ë¤³¤È¤¬ ²Äǽ¤Ç¤¹¡¥


Next: , Previous: Group options, Up: Top   [Contents][Index]