读取命令行参数

From Yefu's notes
Jump to: navigation, search

以下摘自我以前的代码:

#include <stdio.h>
#include <getopt.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <unistd.h>
#include "ezshell.h"

/*
*   function
*   @name:usage
*   @description: Print the usage of ezshell
*/
void usage()
{
    printf("usage: ezshell [-circular] [-nowait] commandfile\n");
}



int main(int argc, char  **argv)
{         
    int c=-1,i,j;
    char* inputfile;
    char PName[256];
    static int circular=0;
    static int nowait=0;
    int **pipePool; 
    int count;

    //  Read the input parameters from the command line
    //If -circular is set, circular is set to 1.
    //If -nowait is set, variable nowait will be st to 1.

    while(1)
    {
        static struct option long_options[] =
        {
            {"circular",    no_argument,    0,  'c'},
            {"nowait",  no_argument,0,  'n'},
            {0,0,0,0} 
        };

        int option_index = 0;
        c = getopt_long_only (argc, argv, "cn", long_options, &option_index);

        if(c==-1) /*No more arguments. */
            break;
        switch (c)
        {
            case 'c':
                circular=1;
                break;
            case 'n':
                nowait=1;
                break;
            default:
                usage();
                exit(1);
        };

    };
    if(optind<argc)
    {
        inputfile=argv[optind];
    }
    else //The user did not input the file name
    {
        usage();
        exit(1);
    }
//以下省略

<comments/>