Previous: slua_args_va, Up: Argument parsing


A.1.4 slua_check_n_arg

check the number of arguments passed to C from Lua.

Synopsis

     #include <luasup/luasup.h>
     
     
     
int slua_check_n_arg( SLua_ErrFunc errfunc, const char *func, int n );

Parameters

SLua_ErrFunc errfunc
Not Documented.
const char *func
the name of the lua function
int n
the number of arguments expected

Description

This routine compares the number of arguments expected (passed via the n parameter) to the actual number of arguments passed to the C function by Lua. It's a simple wrapper around lua_getparam.

Returns

If the number of arguments matches is not less than that passed via n, it returns zero. If there are fewer arguments than expected, it calls the supplied error function and returns -1.

Author

Diab Jerius

Example

     /* --8<--8<--8<--8<--
      *
      * Copyright (C) 2006 Smithsonian Astrophysical Observatory
      *
      * This file is part of luasup
      *
      * luasup is free software; you can redistribute it and/or
      * modify it under the terms of the GNU General Public License
      * as published by the Free Software Foundation; either version 2
      * of the License, or (at your option) any later version.
      *
      * luasup 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 General Public License for more details.
      *
      * You should have received a copy of the GNU General Public License
      * along with this program; if not, write to the
      *       Free Software Foundation, Inc.
      *       51 Franklin Street, Fifth Floor
      *       Boston, MA  02110-1301, USA
      *
      * -->8-->8-->8-->8-- */
     
     #include <stddef.h>
     #include <stdarg.h>
     
     #include <luasup.h>
     
     extern SLua_ErrFunc errfunc;
     
     void foo ( void )
     {
       char *name;
       double energy, base_flux;
     
       slua_args_n( errfunc, "mono flux generator", 1,
                     "name",         SLua_STRING,     &name,
                     "energy",       SLua_NUMBER,     &energy,
                     "photon flux",  SLua_NUMBER,     &base_flux,
                     NULL
                     );
     
       free( name );
     
       slua_args_n( errfunc, "mono flux generator", 4,
                     "name",         SLua_STRING,     &name,
                     "energy",       SLua_NUMBER,     &energy,
                     "photon flux",  SLua_NUMBER,     &base_flux,
                     NULL
                     );
     
       free( name );
     }