Parse Arguements : Command Line Parameters « Development « C / ANSI-C






Parse Arguements

/*
Practical C Programming, Third Edition
By Steve Oualline
Third Edition August 1997 

ISBN: 1-56592-306-5
Publisher: O'Reilly
*/
/* This program is an example of how to parse    *
 * the command line arguments.  It sets up all the  *
 * global variables for a real program, it just doesn't  *
 * have any body.          *
 **/

/* Program: Print          *
 *              *
 * Purpose:            *
 *  Formats files for printing      *
 *              *
 * Usage:            *
 *  print [options] file(s)        *
 *              *
 * Options:            *
 *  -v    Produce versbose messages  *
 *  -o<file>  Send output to a file     *
 *      (default=print.out)    *
 *  -l<lines>  Set the number of lines/page.  *
 *      (default=66).      *
 */
#include <stdio.h>
#include <stdlib.h>      

int verbose = 0;         /* verbose mode (default = false) */
char *out_file = "print.out";   /* output filename */
char *program_name;      /* name of the program (for errors) */
int line_max = 66;       /* number of lines per page */

/*
 * do_file -- dummy routine to handle a file            *
 *                                                      *
 * Parameter                                            *
 *      name -- name of the file to print               *
 */
void do_file(char *name)
{
    printf("Verbose %d Lines %d Input %s Output %s\n",
        verbose, line_max, name, out_file);
}
/*
 * usage -- tell the user how to use this program and   *
 *              exit                                    *
 */
void usage(void)
{
    fprintf(stderr,"Usage is %s [options] [file-list]\n", 
                                program_name);
    fprintf(stderr,"Options\n");
    fprintf(stderr,"  -v          verbose\n");
    fprintf(stderr,"  -l<number>  Number of lines\n");
    fprintf(stderr,"  -o<name>    Set output filename\n");
    exit (8);
}
int main(int argc, char *argv[])
{
    /* save the program name for future use */
    program_name = argv[0];

    /* 
     * loop for each option.  
     *   Stop if we run out of arguments
     *   or we get an argument without a dash.
     */
    while ((argc > 1) && (argv[1][0] == '-')) {
        /*
         * argv[1][1] is the actual option character.
         */
        switch (argv[1][1]) {
            /*
             * -v verbose 
             */
            case 'v':
                verbose = 1; 
                break;
            /*
             * -o<name>  output file
             *    [0] is the dash
             *    [1] is the "o"
             *    [2] starts the name
             */
            case 'o':
                out_file = &argv[1][2];
                break;
            /*
             * -l<number> set max number of lines
             */
            case 'l':
                line_max = atoi(&argv[1][2]);
                break;
            default:
                fprintf(stderr,"Bad option %s\n", argv[1]);
                usage();
        }
        /*
         * move the argument list up one
         * move the count down one
         */
        ++argv;
        --argc;
    }

    /*
     * At this point all the options have been processed.
     * Check to see if we have no files in the list
     * and if so, we need to process just standard in.
     */
    if (argc == 1) {
        do_file("print.in");
    } else {
        while (argc > 1) {
          do_file(argv[1]);
          ++argv;
          --argc;
        }
    }
    return (0);
}
  

           
       








Related examples in the same category

1.A program to list the command line arguments
2.Check the command line parameters
3.Process the command line input
4.Verify the user input and display file content
5.Check the command line input
6.Command line parameter: display all of them
7.Use the command line parameter
8.Check the command line parameter and use it
9.Check the command line parameter: if less than required exit