LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   getopt() giving problem (https://www.linuxquestions.org/questions/linux-newbie-8/getopt-giving-problem-872349/)

jkeertir 04-01-2011 05:40 AM

getopt() giving problem
 
I am trying to parse command line arg using getopt().this is my code
int main(int argc, char** argv)
{
int a=0,b=0;
char *msgtype,*filename;
while ((c = getopt(argc, argv, "f:b:")) != -1) {
switch(c) {
case 'a':
printf("a is set\n");
break;
case 'b':
msg_type = optarg;
printf("msg_type is %s\n", msg_type);
break;
case 'f':
feedname = optarg;
printf("filename is %s\n", feedname);
if(strcmp(optarg,"aaa")==0)
aflag =1;
// itch();
else if(strcmp(optarg,"bbb")==0)
bflag=1;
break;
case ':':
printf("-%c without filename\n", optopt);
break;
case '?':
printf("unknown arg %c\n", optopt);
break;
}
if(itchflag)
a();
if(ouchflag)
b(msg_type);
}

}

It works fine when i run and call a() s ./a.out -b A -f aaa

but doesn't work and calls a() if ./a.out -f aaa -b A

tronayne 04-01-2011 07:09 AM

Here's a prototype C program. It doesn't actually do anything until you add code (it will compile and execute, though). Further down is how to use argument values.

Hope this helps some.
Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#ifndef TRUE
#      define  TRUE    1
#endif
#ifndef FALSE
#      define  FALSE  0
#endif

int    main    (int argc, char *argv [])
{
        int    c;                      /* general-purpose              */
        int    error = FALSE;          /* error flag                  */
        int    vopt = FALSE;          /* verbose option              */
        time_t  t0 = (time_t) 0;        /* start time                  */
        time_t  t1 = (time_t) 0;        /* finish time                  */
        FILE    *in;

        /*      process the command line arguments                      */
        while ((c = getopt (argc, argv, "?v")) != EOF) {
                switch (c) {
                case '?':
                        error = TRUE;
                        break;
                case 'v':
                        vopt = TRUE;
                        break;
                default:
                        (void) fprintf (stderr, "getopt() bug\n");
                        exit (EXIT_FAILURE);
                }
        }
        /*      any errors in the arguments, or a '?' entered...*/
        if (error) {
                (void) fprintf (stderr, "usage: %s [-v] argument...\n",
                    argv [0]);
                exit (EXIT_FAILURE);
        }
        /*      get a start time                                */
        if (time (&t0) < (time_t) 0)
                (void) fprintf (stderr,
                    "%s:\tcan't read system clock\n", argv [0]);
        /*      now process any arguments supplied...          */
        while (optind != argc) {
                (void) fprintf (stderr, "Processing %s...\n", argv [optind]);
                /*      open the input file            */
                if ((in = fopen (argv [optind], "r")) == (FILE *) NULL) {
                        (void) fprintf (stderr,
                            "%s:\tcan't open %s\n",
                            argv [0], argv [optind]);
                        exit (EXIT_FAILURE);
                }
                /*      close the input file            */
                if (fclose (in))
                        (void) fprintf (stderr,
                            "%s:\tcan't close %s\n",
                            argv [0], argv [optind]);
                optind++;
        }
        /*      get a finish time                      */
        if (time (&t1) < (time_t) 0)
                (void) fprintf (stderr,
                    "%s:\tcan't read system clock\n", argv [0]);
        if (vopt)
                (void) fprintf (stderr,
                    "%s duration %g seconds\n",
                    argv [0], difftime (t1, t0));
        exit (EXIT_SUCCESS);
}

Here's a fragment showing how to use integer arguments
Code:

int    main    (int argc, char *argv [])
{
        int    c;                      /* general-purpose              */
        int    error = FALSE;          /* error flag                  */
        int    mm = 0, dd = 0, yy = 0; /* month, day, year            */
        int    iopt = FALSE;          /* interval option              */
        int    vopt = FALSE;          /* verbose option              */
        time_t  t0 = (time_t) 0;        /* today julian date            */
        time_t  t1 = (time_t) 0;        /* unknown julian date          */
        struct  tm      *tm;            /* system time structure        */

        /*      process the command line arguments                      */
        while ((c = getopt (argc, argv, "?id:m:y:v")) != EOF) {
                switch (c) {
                case '?':
                        error = TRUE;
                        break;
                case 'd':
                        dd = (int) strtol (optarg, (char **) NULL, 10);
                        break;
                case 'i':
                        iopt = TRUE;
                        break;
                case 'm':
                        mm = (int) strtol (optarg, (char **) NULL, 10);
                        break;
                case 'y':
                        yy = (int) strtol (optarg, (char **) NULL, 10);
                        break;
                case 'v':
                        vopt = TRUE;
                        break;
                default:
                        (void) fprintf (stderr, "getopt() bug\n");
                        exit (EXIT_FAILURE);
                }
        }
        /*      any errors in the arguments, or a '?' entered...*/

And, when dealing with string argument, you need to copy them:
Code:

void    main    (int argc, char *argv [])
{
        int    err = FALSE;
        int    i, j;
        int    topt = FALSE;          /* function type option        */
        int    ropt = FALSE;          /* return type option          */
        char    rtyp [10];              /* return type description      */
        char    ttyp [10];              /* function type description    */
        time_t  now;                    /* for time and date            */
        struct  tm      *tm;            /* system time                  */

        /*      process the command line arguments      */
        while ((i = getopt (argc, argv, "?r:t:")) != EOF) {
                switch (i) {
                        case 'r':
                                ropt = TRUE;
                                (void) strcpy (rtyp, optarg);
                                break;
                        case 't':
                                topt = TRUE;
                                (void) strcpy (ttyp, optarg);
                                break;
                        case '?':
                                err = TRUE;
                                break;
                        default:
                                err = TRUE;
                                break;
                }
        }

Hope this helps some.


All times are GMT -5. The time now is 10:06 AM.