LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-01-2011, 06:40 AM   #1
jkeertir
Member
 
Registered: Mar 2008
Posts: 70

Rep: Reputation: 15
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
 
Old 04-01-2011, 08:09 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,479

Rep: Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020
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.

Last edited by tronayne; 04-01-2011 at 08:56 AM. Reason: forgot to include strings
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
getopt() problem thtr2k Programming 1 04-27-2007 09:06 PM
problem with getopt in C language shifter Programming 3 04-02-2007 12:23 AM
getopt in unix hari_s_82 Linux - Newbie 2 10-01-2004 05:19 AM
getopt(3) Berhanie Programming 8 06-15-2004 10:50 PM
Getopt() newbie... AMMullan Programming 6 02-11-2004 02:02 PM


All times are GMT -5. The time now is 01:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration