【问题标题】:Taking command line options in C在 C 中采用命令行选项
【发布时间】:2017-01-20 12:54:09
【问题描述】:

我有一个哈希表程序,我正在尝试实现命令行选项输入。默认操作是创建哈希表并读取文本文件,这是在检查选项后完成的。这些选项主要在哈希表创建之前更改其属性,例如 -f 选项指定表大小。

例如./program < words.txt -f 400

我目前正在这样处理它们:

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

    const char *optstring = "e:f:";
    char option;
    int tablesize = 100;
    int unknown_words, i;
    char word[256];
    htable h;   
    default = 1;

    while((option = getopt(argc, argv, optstring)) != EOF){
        switch (option){

            case 'e':
                default = 0;
                h = htable_new(tablesize);

                copy_in(h);

                unknown_words = find_words(h, optarg);
                printf("%d", unknown_words);
                break;

             case 'f':

                if(optarg && atoi(optarg)>0){   
                    tablesize = atoi(optarg);           
                }
                break;
        }
     }

     if(default==1){
         h = htable_new(tablesize);
         copy_in(h);
         print_stats(h);
     }
  }

我的问题是我想以任何顺序输入这些标志/选项。我有一个带有参数(第二个文本文件的名称)的选项 -e。它创建一个哈希表并读取第一个文本文件(如默认操作),然后在哈希表中搜索第二个文件中的单词并打印未知数量的单词。 我还有一个选项 -f 指定使用与默认值不同的表大小。如果使用以下命令运行,我会得到预期的行为。

./program < words.txt -f 350 -e other_words.txt 

首先找到 -f 选项,并将 tablesize 变量从其默认值更改为给定的 350。然后找到 -e 选项并使用此更新后的值执行。

但是以不同的顺序具有相同的期望行为:

./program < words.txt -e other_words.txt -f 350

-e 选项是在默认表大小上执行的,然后才找到 -f 选项,并且表大小发生了变化,然后不使用了。

我已经完成了以下工作,但似乎效率低下。本质上,如果找到 -e 选项,则遍历其余选项并首先执行它们。这意味着每个相关选项的重复代码,我很好奇如何处理这个。

 while((option = getopt(argc, argv, optstring)) != EOF){
        switch (option){

            case 'e':
                default = 0;
                for(i=optind;i<argc;i++){
                    if(strcmp(argv[i],"-t") == 0){
                        if(argv[i+1] && atoi(argv[i+1])>0){ 
                            tablesize =atoi(argv[i+1]);
                        }
                     }
                 }


                h = htable_new(tablesize);

                copy_in(h);

                unknown_words = find_words(h, optarg);
                printf("%d", unknown_words);
                break;

             case 'f':

                if(optarg && atoi(optarg)>0){   
                    tablesize = atoi(optarg);           
                }
                break;
        }
     }

【问题讨论】:

  • 您好,欢迎来到堆栈溢出。请查看minimal reproducible example,了解如何减少代码以更好地阐明您的要求。
  • 技术上没有问题。请不要让我们猜测并在所有美丽的展览之后提出一些具体的问题。
  • 如果顺序很重要,您必须首先收集所有参数,一旦获得所有参数,请运行相应的代码。如果您有选项f 设置tablesize(初始化为不可能发生的事情,例如:-1),如果您有选项e 将文件名放在某处(初始化为例如:NULL)。在选项循环检查tablesize != -1 并设置该大小后,检查filename != NULL 并处理该文件。
  • 我不知道我是否正确理解了这个问题,但为什么不去option by option并保存数据。您在 -f 选项中执行此操作。当您收到-e 时,您只需注释名称/路径,并在存储选项后,执行htable_new 和其他功能/例程。
  • 您好,在您获得帮助后,请不要破坏您的帖子。这就像在树下避难后砍倒一棵树。请允许其他未来的用户从知识中获益。回答者会付出很多努力。不要浪费他们宝贵的时间。

标签: c command-line getopt


【解决方案1】:

典型的方法是不在选项处理程序中做实际工作;而是让每个处理程序设置一个变量来跟踪指定的选项(以及它的参数是什么)。然后在您解析所有命令行选项后,可以按照您想要的任何顺序处理它们。例如,类似:

const char* filename = NULL;
int tablesize = 0;

while ((option = getopt(argc, argv, optstring)) != EOF) {
    switch (option) {
        case 'e':
            filename = optarg;
            break;

         case 'f':
            if (optarg && atoi(optarg) > 0) {
                tablesize = atoi(optarg);           
            }
            break;
    }
}

if (filename != NULL) {
    default = 0;
    h = htable_new(tablesize);

    copy_in(h);
    unknown_words = find_words(h, filename);
    printf("%d", unknown_words);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-28
    • 2017-08-31
    • 2011-03-20
    • 1970-01-01
    • 2021-12-22
    • 2014-06-28
    • 2011-08-02
    • 1970-01-01
    相关资源
    最近更新 更多