1

Hello I'm new with C and I'm trying to start this lab where the command syntax is to first list any command line flags then list one or more file names.

I'm having trouble organizing how I want to scan the input arguments and differentiating them between flags and file names.

I thought of doing a loop to see if the argument is a flag or file name. But I'm unsure how to begin implementing it. Since the first 4 arguments can be potential flags in any order then anything after is the file name. But it is also possible for no flags to be given and just start with filenames. I don't know at all how to initalize this loop to go through each argument 1 by 1. Can anyone help please?

Example of possible command line arguments:

wc fcopy.c head.c (no flags given just file names)
wc -l -c -w -L fcopy.c head.c a.exe (flags given and multiple files)
wc -l -w -c -L fcopy.c (flags given and 1 file name)
Mat
  • 202,337
  • 40
  • 393
  • 406
V Or
  • 141
  • 5
  • 16

4 Answers4

2

You can use getopt()

Check these questions:
Parsing command-line arguments?
Handling command line options before and after an argument in C
Argument-parsing helpers for C/UNIX
Getopt shift optarg

Community
  • 1
  • 1
LihO
  • 41,190
  • 11
  • 99
  • 167
2

Check out getopt, it allows you to parse command line arguments and their flags:

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main (int argc, char **argv)
{
  int aflag = 0;
  int bflag = 0;
  char *cvalue = NULL;
  int index;
  int c;

  opterr = 0;

  while ( (c = getopt (argc, argv, "abc:")) != -1 )
    switch (c) {
      case 'a':
        aflag = 1;
        break;
      case 'b':
        bflag = 1;
        break;
      case 'c':
        cvalue = optarg;
        break;
      case '?':
        if (optopt == 'c')
          fprintf (stderr, "Option -%c requires an argument.\n", optopt);
        else if (isprint (optopt))
          fprintf (stderr, "Unknown option `-%c'.\n", optopt);
        else
          fprintf (stderr,
                   "Unknown option character `\\x%x'.\n",
                   optopt);
        return 1;
      default:
        abort ();
      }

  printf ("aflag = %d, bflag = %d, cvalue = %s\n",
          aflag, bflag, cvalue);

  for (index = optind; index < argc; index++)
    printf ("Non-option argument %s\n", argv[index]);
  return 0;
}

Example of Parsing Arguments with getopt

  • Im trying to make sense of this and in the while loop "abc :" what exactly is that saying? and to say their are 4 cases for the 4 different flags would I make a flag for when the argument is a file i need to open? – V Or Sep 18 '12 at 03:30
  • "abc" are the flags you want `getopt` to look for, `:` means there will be an argument after that , `c`, flag. Eg. `foo.exe -a -b -c hello`. –  Sep 18 '12 at 03:44
  • is it possible to use getopt to open text files or at least recognize that it isn't a flag and perform a file open method? – V Or Sep 18 '12 at 03:46
2

Reading command line args one by one is pretty simple:

int main( int argc, // Number of strings in array argv char *argv[])

The two basic args to a C function are argc (int number of args), and argv (an array of strings for the args)

The first string is always the name of the program that is running, after that is the args that were passed over the command line.

if(argc > 1)
    for( count = 1 count < argc; count++ )
        printf("%s", argv[count]);

Will display to the screen each of the arguments passed in past the name of the calling program. I assume your flags are passed with a "-" as the first character? If so, you can check the contents of the first character in each string for a '-' to tell if its a flag or not.

Mike
  • 47,263
  • 29
  • 113
  • 177
  • looking at this it appears to be really easy to follow along. But how will the for loop know when it reached the end of the arguments? – V Or Sep 18 '12 at 03:49
  • argc = # of arguments. So we set the for loop iterator to 1 (so it skips just the program name) and increment it until its 1 less than argc. So let's say you pass in "-n filename.c" then argc will be set to 3, and argv[0]="yourprgmname.dmg" argv[1]="-n" and argv[2]="filename.c" – Mike Sep 18 '12 at 11:32