getopt函数的使用

一个程序往往会提供很多参数,如果仅是简单的几个参数也就罢了,如果是很多参数,仅根据参数的位置来分析,程序会给人一种“很烂”的感觉,其实每种语言基本都提供类似getopt的函数的,先从php说起吧。

例:

    

        

            

        

    

            

$arr_opt = getopt("a:b:cd");

            

print_r($arr_opt);

            

说明: ‘:’ 代表该选项需要值
用法: ./cmd -a valueofa  -b valueofb -c -d

注意: 该函数在Windows下没有实现,本来该函数还提供long option呢,只是也需要编译php时c的支持,所以我还没研究过

shell中的用法

    

        

            

        

    

            

#!/bin/sh
            

            #FileName: getopt.sh
            

            #Desc: Show how to use command getopts
            

            #Date: 2008-01-05
            

            #Auth: lijunjie
            

            
            args='o:i:th'
            
            function show_help(){
                echo "Usage: [" $args "]"
                exit 1
            }
            #if no any args then show_help
            

            if [ -z $1 ];then
                show_help
            fi
            while getopts $args var
            do
                case $var in
                'o') out_file="$OPTARG";;
                'i') in_file="$OPTARG";;
                't') title="hello";;
                'h') show_help;;
                *) exit 2;;
                esac
            done
            echo out_file : $out_file
            echo in_file : $in_file
            [ ! -z $title ] && echo $title
            exit 0

            

 

 

c语言中怎么用的呢?

    

        

            

        

    

            

#include <unistd.h>
            
            int main(int ac ,char** av){
                int c;
                /* process arguments */
                while ((c = getopt(ac, av, "a:b:c")) != -1) {
                    switch (c) {
                        case 'a':
                            printf("a:%s\n",optarg);
                            break;
                        case 'b':
                            printf("b:%d\n",atoi(optarg));
                            break;
                        case 'c':
                            printf("c is set\n");
                            break;
                        default:
                            printf("%c is invalidate argument\n",&c);
                            break;
                    }
                }
                
            }

            

 

c中long option 的用法:

    

        

            

        

    

            

#include <stdio.h> /* for printf */
            #include <stdlib.h> /* for exit */
            #include <getopt.h>
            
            int main (int argc, char **argv) {
                int c;
                int digit_optind = 0;
            
                while (1) {
                    int this_option_optind = optind ? optind : 1;
                    int option_index = 0;
                    static struct option long_options[] = {
                        {"add", 1, 0, 0},
                        {"append", 0, 0, 0},
                        {"delete", 1, 0, 0},
                        {"verbose", 0, 0, 0},
                        {"create", 1, 0, 'c'},
                        {"file", 1, 0, 0},
                        {0, 0, 0, 0}
                    };
            
                    c = getopt_long (argc, argv, "abc:d:012",
                             long_options, &option_index);
                    if (c == -1)
                        break;
            
                    switch (c) {
                    case 0:
                        printf ("option %s", long_options[option_index].name);
                        if (optarg)
                            printf (" with arg %s", optarg);
                        printf ("\n");
                        break;
            
                    case '0':
                    case '1':
                    case '2':
                        if (digit_optind != 0 && digit_optind != this_option_optind)
                          printf ("digits occur in two different argv-elements.\n");
                        digit_optind = this_option_optind;
                        printf ("option %c\n", c);
                        break;
            
                    case 'a':
                        printf ("option a\n");
                        break;
            
                    case 'b':
                        printf ("option b\n");
                        break;
            
                    case 'c':
                        printf ("option c with value : %s'\n", optarg);
                        break;
            
                    case 'd':
                        printf ("option d with value : %s'\n", optarg);
                        break;
            
                    case '?':
                        break;
            
                    default:
                        printf ("getopt returned character code :%o \n", c);
                    }
                }
            
                if (optind < argc) {
                    printf ("non-option ARGV-elements: ");
                    while (optind < argc)
                        printf ("%s ", argv[optind++]);
                    printf ("\n");
                }
            
                exit (0);
            }

            

留下评论

邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据