Sponsored Content
Top Forums Programming Combine GetOpt And Variadic Function In C Post 303031030 by Corona688 on Wednesday 20th of February 2019 02:41:27 PM
Old 02-20-2019
main and getopt aren't variadic, they're array-based.

So one solution could be converting the variadic arguments into an array.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

void argfunction(char *howmany, ...);

int main(void) {
        argfunction("abc", "atext", "btext", "ctext");
        return(0);
}

void argfunction(char *howmany, ...)
{
        int n=0;
        // Never forget the NULLs, they convert mysterious "why" errors
        //  into "oh, I forgot to assign X" errors
        char *host=NULL, *user=NULL, *pass=NULL, *port=NULL;
        int argc=0;
        char *argv[16];
        va_list ap; // IIRC ap should always be the very last variable declared in a function.
        va_start(ap, howmany);

        argv[0]=""; // just tradition, argv[0] in main() is not an argument

        // checks howmany[0], assigns argv[1].
        // checks howmany[1], assigs argv[2], etc.
        while((argc<(16-2))&&howmany[argc++]) argv[argc]=va_arg(ap, char *);

        argv[argc]=NULL;         // Also traditional that argv[argc] be NULL.

        va_end(ap);

        printf("Howmany is: %s\n", howmany);
        printf("Arguments are:\n");
        for(n=1; n<argc; n++)
                printf("\t%d\t%s\n", n, argv[n]);

        optind=1; // This is global, so reset it

        // You now have argv, argc, and optind just like main to feed getopt with.
}


Last edited by Corona688; 02-20-2019 at 04:07 PM..
This User Gave Thanks to Corona688 For This Post:
 

10 More Discussions You Might Find Interesting

1. Programming

question about getopt()

I'm using getopt() to get command line options.One the optons accepts and argument.The argument is and offset.I was wondering how can I scecify that it's argument is of the type off_t.I've something like this "offset=(off_t)optarg" and it don't work. (1 Reply)
Discussion started by: angelfly
1 Replies

2. Shell Programming and Scripting

getopt help

scriptname i have made a script to perform so tasks and i managed to complete the tasks for all the options the problem i am facing is that i can run the scripts individually but i would like to make it such that it can accept multiple options and give me the appropriate output e.g.... (1 Reply)
Discussion started by: problems
1 Replies

3. Shell Programming and Scripting

getopt

#!/bin/sh set -- `getopt "abco:" "$@"` a= b= c= o= while : do case "$1" in -a) a=1;; -b) b=1;; -c) c=1;; -o) shift; o="$1";; --) break;; esac shift done shift # get rid of -- # rest of script... # e.g. ls -l $@ (6 Replies)
Discussion started by: Hitori
6 Replies

4. Shell Programming and Scripting

getopt help

I m trying to use getopt This is my script, but it doesn't take argument in variable, Please help. set - - `getopt mscl: $*` if then echo "Exiting...." exit 2 fi for i in $* do case $i in -m) MAIL="$i"; shift;; -s) SCRIPT=$OPTARG; shift;; -c) COB=$OPTARG; shift;;... (2 Replies)
Discussion started by: darshakraut
2 Replies

5. Shell Programming and Scripting

getopt help

:) Can anybody help me about how to use getopt in shell scripting. (3 Replies)
Discussion started by: darshakraut
3 Replies

6. Solaris

use of getopt command

Hi All, Could anyone tell me how to use getopt command.....? Thanks, Pintu (2 Replies)
Discussion started by: pintupatro
2 Replies

7. Shell Programming and Scripting

Help with getopt

Hi, I want to use the getopt function to parse some arguments for a script. while getopts "i:f:r:" OPTION do case $OPTION in i) iter=$OPTARG;; f) frame=$OPTARG;; r) roi=$OPTARG;; ?) echo Usage: ...... exit 2;; esac done However, I... (5 Replies)
Discussion started by: giorgos193
5 Replies

8. HP-UX

Variadic macros compilation error HP C on HP-UX

I have a macro defined like this: #define MM7_RETURN(errorNr, ...) \ { \ if(errorNr != MM7_RS_NoError) \ { ... (1 Reply)
Discussion started by: Marzullo
1 Replies

9. Shell Programming and Scripting

getopt in CSH

I am struggling to understand how getopt can be used in a csh script. can anybody post a csh script using getopt. Please! (4 Replies)
Discussion started by: animesharma
4 Replies

10. Shell Programming and Scripting

Getopt Help

Hi All, An old work friend wrote a script which I've been trying to understand how a section of it currently works and work out how i can add some command line switches which i can use later in the script to append the output depending on the command line arguements. Currently it works by... (1 Reply)
Discussion started by: mutley2202
1 Replies
AppConfig::Getopt(3)					User Contributed Perl Documentation				      AppConfig::Getopt(3)

NAME
AppConfig::Getopt - Perl5 module for processing command line arguments via delegation to Getopt::Long. SYNOPSIS
use AppConfig::Getopt; my $state = AppConfig::State->new(\%cfg); my $getopt = AppConfig::Getopt->new($state); $getopt->parse(@args); # read args OVERVIEW
AppConfig::Getopt is a Perl5 module which delegates to Johan Vroman's Getopt::Long module to parse command line arguments and update values in an AppConfig::State object accordingly. AppConfig::Getopt is distributed as part of the AppConfig bundle. DESCRIPTION
USING THE AppConfig::Getopt MODULE To import and use the AppConfig::Getopt module the following line should appear in your Perl script: use AppConfig::Getopt; AppConfig::Getopt is used automatically if you use the AppConfig module and create an AppConfig::Getopt object through the getopt() method. AppConfig::Getopt is implemented using object-oriented methods. A new AppConfig::Getopt object is created and initialised using the new() method. This returns a reference to a new AppConfig::Getopt object. A reference to an AppConfig::State object should be passed in as the first parameter: my $state = AppConfig::State->new(); my $getopt = AppConfig::Getopt->new($state); This will create and return a reference to a new AppConfig::Getopt object. PARSING COMMAND LINE ARGUMENTS The "parse()" method is used to read a list of command line arguments and update the state accordingly. The first (non-list reference) parameters may contain a number of configuration strings to pass to Getopt::Long::Configure. A reference to a list of arguments may additionally be passed or @ARGV is used by default. $getopt->parse(); # uses @ARGV $getopt->parse(@myargs); $getopt->parse(qw(auto_abbrev debug)); # uses @ARGV $getopt->parse(qw(debug), @myargs); See Getopt::Long for details of the configuartion options available. A Getopt::Long specification string is constructed for each variable defined in the AppConfig::State. This consists of the name, any aliases and the ARGS value for the variable. These specification string are then passed to Getopt::Long, the arguments are parsed and the values in the AppConfig::State updated. See AppConfig for information about using the AppConfig::Getopt module via the getopt() method. AUTHOR
Andy Wardley, <abw@wardley.org> COPYRIGHT
Copyright (C) 1997-2007 Andy Wardley. All Rights Reserved. Copyright (C) 1997,1998 Canon Research Centre Europe Ltd. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. ACKNOWLEDGMENTS
Many thanks are due to Johan Vromans for the Getopt::Long module. He was kind enough to offer assistance and access to early releases of his code to enable this module to be written. SEE ALSO
AppConfig, AppConfig::State, AppConfig::Args, Getopt::Long perl v5.12.1 2007-05-30 AppConfig::Getopt(3)
All times are GMT -4. The time now is 04:40 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy