2013-08-06 16 views
10

私はCでコマンドライン引数を多く扱う小さなプログラムを作っているので、getoptを使ってそれらを並べ替えることにしました。Cでgetoptを非オプション引数で使用する

しかし、2つのオプションのない引数(ソースファイルと宛先ファイル)を必須にしたいので、フラグやその他の引数がなくても、プログラムを呼び出すときにそれらを引数として持つ必要があります。ここで

は、私はフラグで引数を処理するために持っているものの簡易版です。

while ((c = getopt(argc, argv, "i:d:btw:h:s:")) != -1) { 
    switch (c) { 
     case 'i': { 
      i = (int)atol(optarg); 
     } 
     case 'd': { 
      d = (int)atol(optarg); 
     } 
     case 'b': 
      buf = 1; 
      break; 
     case 't': 
      time = 1; 
      break; 
     case 'w': 
      w = (int)atol(optarg); 
      break; 
     case 'h': 
      h = (int)atol(optarg); 
      break; 
     case 's': 
      s = (int)atol(optarg); 
      break; 
     default: 
      break; 
    } 
} 

非オプションの引数はまた、処理されるように、私はこれを編集するにはどうすればよいですか?

また、オプションの後にまたはの前に非オプションがあるようにしたいのですが、どのように処理されるのですか?

答えて

13

getoptは、optind変数に次の引数の位置を示す変数を設定します。

if (argv[optind] == NULL || argv[optind + 1] == NULL) { 
    printf("Mandatory argument(s) missing\n"); 
    exit(1); 
} 

編集:

あなたが定期的に引数の後にオプションを使用すると、これに似た何かを行うことができます許可する場合:

この 後のオプションループに似

追加コード

while (optind < argc) { 
    if ((c = getopt(argc, argv, "i:d:btw:h:s:")) != -1) { 
    // Option argument 
    switch (c) { 
     case 'i': { 
      i = (int)atol(optarg); 
     } 
     case 'd': { 
      d = (int)atol(optarg); 
     } 
     case 'b': 
      buf = 1; 
      break; 
     case 't': 
      time = 1; 
      break; 
     case 'w': 
      w = (int)atol(optarg); 
      break; 
     case 'h': 
      h = (int)atol(optarg); 
      break; 
     case 's': 
      s = (int)atol(optarg); 
      break; 
     default: 
      break; 
    } 
    else { 
     // Regular argument 
     <code to handle the argument> 
     optind++; // Skip to the next argument 
    } 
} 
+1

OKですが、必須の引数がオプションのものよりも前に来ると、私のループは終了するので、必須のものだけが処理され、オプションのものは処理されません。どのように私はこれを修正するのですか? –

+1

オプションは引数の前に来ることが一般的です。 'man'ページで指定するだけです。 –

+2

うん、私は知っているけど、たとえばsshコマンドでは、必須のusername @ server引数の後に*または*の前に-pフラグを付けることができます。私はちょうどそれを行う方法を知りたいです –

4

本当に良い例がここにあります:GNU Libcコード:

#include <ctype.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 

int 
main (int argc, char **argv) 
{ 
int aflag = 0; 
int bflag = 0; 
char *cvalue = NULL; 
int index; 
int c; 

opterr = 0; 

while ((c = getopt (argc, argv, "abc:")) != -1) 
switch (c) 
{ 
case 'a': 
    aflag = 1; 
    break; 
case 'b': 
    bflag = 1; 
    break; 
case 'c': 
    cvalue = optarg; 
    break; 
case '?': 
    if (optopt == 'c') 
    fprintf (stderr, "Option -%c requires an argument.\n", optopt); 
    else if (isprint (optopt)) 
    fprintf (stderr, "Unknown option `-%c'.\n", optopt); 
    else 
    fprintf (stderr, 
     "Unknown option character `\\x%x'.\n", 
     optopt); 
    return 1; 
default: 
    abort(); 
} 

printf ("aflag = %d, bflag = %d, cvalue = %s\n", 
    aflag, bflag, cvalue); 

for (index = optind; index < argc; index++) 
printf ("Non-option argument %s\n", argv[index]); 
return 0; 
} 

引数の前後にオプションを付けることができます。私はコンパイルしてテストの例を実行しました:

$ ./a.out aa ff bb -a -ctestparam hello 
aflag = 1, bflag = 0, cvalue = testparam 
Non-option argument aa 
Non-option argument ff 
Non-option argument bb 
Non-option argument hello 
関連する問題