處理命令行參數的函數--getopt_long

最後知道真相的我眼淚掉下來~~this

/**                                                                                                                                                                    
 * demonstrate the usage of getopt_long                                                                                                                                
 *                                                                                                                                                                     
 * getopt_long, optarg, optind                                                                                                                                         
 **/
#include <getopt.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>.net

const char* program_name;code

/**                                                                                                                                                                    
 * function: print_usage                                                                                                                                               
 * @stream : file stream for output                                                                                                                                     
 * @exitcode: exit code                                                                                                                                                
 *                                                                                                                                                                     
 **/
void print_usage(FILE* stream, int exitcode)
{
        fprintf(stream, "Usage: %s options [ inputfile ... ] \n", program_name);
        fprintf(stream,
                "   -h   --help              Display this usage infomation. \n"
                "   -o   --output filename   Write output to file. \n"
                "   -v   --verbose           Print verbose messages. \n");
        exit(exitcode);
}get

int main(int argc, char *argv[])
{
        int next_option;input

        const char* const short_options = "ho:v"; /* the string cannot be modified as well as the pointer */
        const struct option long_options[] = {
                {"help", 0, NULL, 'h'}, /* --help, no argument for this option, corresponds to -h option */
                {"output", 1, NULL, 'o'},
                {"verbose", 0, NULL, 'v'},
                {NULL, 0, NULL, 0},
        };string

        const char* output_filename = NULL;
        int verbose = 0;it

        /* start parsing options */io

        program_name = argv[0];
        do
        {
                next_option = getopt_long(argc, argv, short_options, long_options, NULL);
                switch (next_option)
                {
                case 'h':
                        print_usage(stdout, 0);
                        break;
                case 'o':
                        output_filename = optarg;
                        break;
                case 'v':
                        verbose = 1;
                        break;
                case '?':
                        print_usage(stderr, 1);
                case -1:        /* done with options */
                        break;
                default:        /* something unexpected */
                        abort();function

                }
        } while (next_option != -1);class

        if (verbose)
        {
                int i;
                for (i=optind; i<argc; i++)
                        printf("Argument: %s \n", argv[i]);
        }
        /* main body of the program */
        return 0;
}

root@localhost :/home/James/mypro/ALP/getopt_long# ./demo -dd
./demo: invalid option -- 'd'
Usage: ./demo options [ inputfile ... ]
   -h   --help              Display this usage infomation.
   -o   --output filename   Write output to file.
   -v   --verbose           Print verbose messages.
root@localhost :/home/James/mypro/ALP/getopt_long# ./demo -v -o put input1 input2 input3
Argument: input1
Argument: input2
Argument: input3
root@localhost :/home/James/mypro/ALP/getopt_long# ./demo -h
Usage: ./demo options [ inputfile ... ]
   -h   --help              Display this usage infomation.
   -o   --output filename   Write output to file.
   -v   --verbose           Print verbose messages.
root@localhost :/home/James/mypro/ALP/getopt_long# ./demo -m
./demo: invalid option -- 'm'
Usage: ./demo options [ inputfile ... ]
   -h   --help              Display this usage infomation.
   -o   --output filename   Write output to file.
   -v   --verbose           Print verbose messages.
root@localhost :/home/James/mypro/ALP/getopt_long# ./demo -v input1 input2 input3 Argument: input1 Argument: input2 Argument: input3

相關文章
相關標籤/搜索