A simple example on using LibExarg

Introduction

You may find interesting the following example which makes use of LibExArg. This example install 3 arguments to be accepted as valid command line options and the write down to the terminal the result of those command used by the user.

Once the arguments are installed, the user can execute

//  LibExploreArguments: a library to parse command line options
//  Copyright (C) 2005 Advanced Software Production Line, S.L.
// 
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public License
//  as published by the Free Software Foundation; either version 2.1 of
//  the License, or (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of 
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
//  GNU Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this program; if not, write to the Free
//  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
//  02111-1307 USA
//  
//  You may find a copy of the license under this software is released
//  at COPYING file. This is LGPL software: you are wellcome to
//  develop propietary applications using this library withtout any
//  royalty or fee but returning back any change, improvement or
//  addition in the form of source code, project image, documentation
//  patches, etc. 
//
//  You can also contact us to negociate other license term you may be
//  interested different from LGPL. Contact us at:
//          
//      Postal address:
//         Advanced Software Production Line, S.L.
//         C/ Dr. Michavila NÂș 14
//         Coslada 28820 Madrid
//         Spain
//
//      Email address:
//         info@aspl.es - http://fact.aspl.es
//
//

#include <exarg.h>

#define HELP_HEADER "test-exarg: a simple program to test libexarg library\n\
Copyright (C) 2005 Advanced Software Production Line\n\n"

#define USAGE_HEADER "Brief: You can use any of the following option from the usage.\n\
Once the option is used, the program will parse it and will notify that options was\n\
read. For those arguments which receives values, that will also showed.\n\n"

#define POST_MESSAGE "\nIf you have any question, path to report or problems about using\n\
this library feel free to contact us at aspl-fact-devel@lists.aspl.es\n"

gint main (gint argc, gchar ** argv)
{
        GList * list_aux;

        exarg_install_arg ("version", "v",      EXARG_NONE,   "Show the program version.");
        exarg_install_arg ("load-library", "l", EXARG_STRING, "Load a library.");
        exarg_install_arg ("how-many-foo", "f", EXARG_INT,    "How many foos to frobnicate.");

        exarg_add_usage_header  (USAGE_HEADER);
        exarg_add_help_header   (HELP_HEADER);
        exarg_post_usage_header (POST_MESSAGE);
        exarg_post_help_header  (POST_MESSAGE);
        

        exarg_parse (argc, argv);
        
        // check argument status
        if (exarg_is_defined ("version")) 
                g_print ("argument --version have been invoked.\n");

        if (exarg_is_defined ("load-library")) {
                g_print ("argument --load-library have been invoked:\n");
                g_print ("     value is: %s\n", exarg_get_string ("load-library"));
        }
        if (exarg_is_defined ("how-many-foo")) {
                g_print ("argument --how-many-foo have been invoked: \n");
                g_print ("     value is %d\n", exarg_get_int ("how-many-foo"));
        }

        // check param list status
        for (list_aux = g_list_first (exarg_get_params ()); 
             list_aux; list_aux = g_list_next (list_aux)) {
                g_print ("param received: %s\n", (gchar *) list_aux->data);
        }

        // end libexarg (once called you must not call exarg anymore until
        // current program ends)
        exarg_end ();
                
        return 0;
}