scot_test.c 5.12 KB
/*
 * san_test.c: A small app to test the functionality of san, my
 *    Swiss Army Nife tool-lib and to show how to use it.
 *    Actually only focused on cmdla which parses commandline
 *    arguments.
 *
 * Copyright (C) 2006 Georg Steffers. All rights reserved.
 *
 * This software is licensed under the terms of the GNU Genral Public
 * License (GPL). Please see gpl.txt for details or refer to 
 * http://www.gnu.org/licenses/gpl.html
 *
 * Author: Georg Steffers <georg@steffers.org>
 *
 * 01/14/2006: Georg Steffers - First implemented
 * 01/15/2006: Georg Steffers - V0.1 ready. Modified much and many
 *                              now long arguments are supported as
 *                              well as arguments with am optional
 *                              parameter.
 * 01/23/2006: Georg Steffers - add support for gettext.
 * 01/24/2006: Georg Steffers - renamed to san_test.c as it should reflect
 *                              the whole lib as it grows.
 */

#include <stdio.h>

#include <scot/cmdla.h>
#include <scot_common.h>
//#include "../config.h"

int 
switch_cb (void *arg)
{
   int *sw = (int *) arg;

   * sw = 1;
}

int
main (int argc, char *argv[])
{
   char  *cval = NULL;
   char  *string = NULL;
   char  *base = NULL;
   char  *home = NULL;
   int   ival = 0;
   float fval = 0.0;
   int   sw1 = 0, sw2 = 0;

   char  *locale;
   char  domain[1024];

   char  about[] = 
      N_("CMDLA_TEST is used to demonstrate the usage of CMDLA.\n"
      "CMDLA stands for CoMmanD Line Argument and it is some functions that,\n"
      "as the name suggested, makes it convinient to parse command line\n"
      "parameters.\n\n"
      "To every parameter there has to be assigned a callback funktion.If\n"
      "one only wants to map options into string, int or float variables,\n"
      "then one can use the predefind get_cmdlap_cb callback.\n"
      "There are two kinds of options that CMDLA processes: switches, which\n"
      "Don't have an additional value, and parameters, which may or may not\n"
      "have an additional value added.\n"
      "Parameters can be set up, so that they need to have an addition value.\n"
      "In addition to parameter parsing and calling appropriate callbacks,\n"
      "CMDLA creates a help output by some hints given to it.\n"
      "All one has to do is fill a structure for the switches you have and\n"
      "one for the parameters, feed them to process_cmd_line and you're "
      "ready.");

   char  copyright[] = 
      N_("Copyright (C) 2006 by Georg Steffers <georg@steffers.org>\n"
      "This software is licensed under the terms of the GNU General Public\n"
      "Licence (GPL). Please refer http://www.gnu.org/licenses/gpl.html\n"
      "for details.");

   
   char  usage_add[] = N_("infile outfile");

   /* an entry with the pointer to the cb-funktion as NULL is end sign */
   struct cmdlap_cbt cmdlap_cbs[]  = {
      {
         'c', N_("cval"), CMDLA_REQ_ARG,
         get_cmdlap_cb, CMDLA_TYPE_STRING, &cval,
         N_("takes a required string argument")
      },
      {
         'i', N_("ival"), CMDLA_OPT_ARG,
         get_cmdlap_cb, CMDLA_TYPE_INT, &ival,
         N_("takes an optional integer argument")
      },
      {
         'f', N_("fval"), CMDLA_REQ_ARG,
         get_cmdlap_cb, CMDLA_TYPE_FLOAT, &fval,
         N_("takes a required float argument")
      },
      {
         0, N_("string"), CMDLA_REQ_ARG, 
         get_cmdlap_cb, CMDLA_TYPE_STRING, &string,
         N_("another required string argument")
      },
      {
         0, N_("base"), CMDLA_REQ_ARG, 
         get_cmdlap_cb, CMDLA_TYPE_STRING, &base,
         N_("and again a req. string argument")
      },
      {
         0, N_("home"), CMDLA_REQ_ARG, 
         get_cmdlap_cb, CMDLA_TYPE_STRING, &home,
         N_("oh, a last required string argument")
      },
      CMDLAP_CBT_END
   };

   /* an entry with the pointer to the cb-funktion as NULL is end sign */
   struct cmdlas_cbt cmdlas_cbs[]  = {
      {
         's', N_("switch"),
         switch_cb, &sw1,
         N_("a switch takes nor argument")
      },
      {
         'S', NULL,
         switch_cb, &sw2,
         N_("another switch")
      },
      CMDLAS_CBT_END
   };

   locale = setlocale (LC_ALL, "");

   if (locale == NULL)
      printf ("uable to set locale\n");
   else
      printf ("use current locale: %s\n", locale);

   printf ("LOCALEDIR: %s\n", LOCALEDIR);
   printf ("PACKAGE: %s\n", PACKAGE);

   snprintf (domain, 1024, "%s_test", PACKAGE);

   bindtextdomain (domain, LOCALEDIR);
   textdomain ("scot_test");

   process_cmd_line (
         argc, argv, about, copyright, usage_add, cmdlap_cbs, cmdlas_cbs);

   printf("CVAL: %s\n", cval);
   printf("STRING: %s\n", string);
   printf("BASE: %s\n", base);
   printf("HOME: %s\n", home);
   printf("IVAL: %d\n", ival);
   printf("FVAL: %f\n", fval);
   printf(
         "SWITCH1 %scalled\n", 
         sw1 == 0 ?
         "not " :
         "");
   printf(
         "SWITCH2 %scalled\n", 
         sw2 == 0 ?
         "not " :
         "");

   if (optind < argc) 
   {
      printf ("non-option ARGV-elements: ");

      while (optind < argc)
         printf ("%s ", argv[optind++]);

      printf ("\n");
   }

   return 0;
}