1

我正在尝试让以下代码与命令一起使用

rectangle –area –length 12 –breadth 34

但我得到了错误

rectangle: invalid option -- r

使用短参数选项

rectangle -a -l 12 -b 34

我得到正确答案

Area: 408

编码:

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

/** Program to calculate the area and perimeter of 
 * a rectangle using command line arguments
 */
void print_usage() {
    printf("Usage: rectangle [ap] -l num -b num\n");
}

int main(int argc, char *argv[]) {
    int opt= 0;
    int area = -1, perimeter = -1, breadth = -1, length =-1;

    //Specifying the expected options
    //The two options l and b expect numbers as argument
    static struct option long_options[] = {
        {"area",      no_argument,       0,  'a' },
        {"perimeter", no_argument,       0,  'p' },
        {"length",    required_argument, 0,  'l' },
        {"breadth",   required_argument, 0,  'b' },
        {0,           0,                 0,  0   }
    };

    int long_index =0;
    while ((opt = getopt_long(argc, argv,"apl:b:", 
                   long_options, &long_index )) != -1) {
        switch (opt) {
             case 'a' : area = 0;
                 break;
             case 'p' : perimeter = 0;
                 break;
             case 'l' : length = atoi(optarg); 
                 break;
             case 'b' : breadth = atoi(optarg);
                 break;
             default: print_usage(); 
                 exit(EXIT_FAILURE);
        }
    }
    if (length == -1 || breadth ==-1) {
        print_usage();
        exit(EXIT_FAILURE);
    }

    // Calculate the area
    if (area == 0) {
        area = length * breadth;
        printf("Area: %d\n",area);
    }

    // Calculate the perimeter
    if (perimeter == 0) {
        perimeter = 2 * (length + breadth);
        printf("Perimeter: %d\n",perimeter);
    }
    return 0;
}
4

1 回答 1

2

长选项必须以--非单个-.

所以

rectangle –-area –-length 12 –-breadth 34

应该管用。

更新

或者,可以使用以下形式:

rectangle –-area –-length=12 –-breadth=34

您可能希望通过更改来更正使用声明:

printf("Usage: rectangle [ap] -l num -b num\n");

printf("Usage: rectangle [-a|--area -p|--perimeter] -l|--length num -b|--breadth num\n");
于 2014-04-07T11:43:33.460 回答