Linux 命令行解析

Linux 命令行解析

1、引入

当解析C程序的命令行参数时,使用 argc 和 argv 这两个参数。 argc 表示命令行参数的数量,而 argv 是一个字符串数组,包含了这些参数的值。

#include <stdio.h>

int main(int argc, char *argv[]) {
    printf("命令行参数的数量:%d\n", argc);
    
    for (int i = 0; i < argc; i++) {
        printf("参数 %d: %s\n", i, argv[i]);
    }
    
    return 0;
}

argc 表示命令行参数的数量,而 argv 是一个字符串数组,包含了这些参数的值。通过遍历 argv 数组,可以逐个访问和处理每个参数。

2、使用

一般简单的可以直接使用即可。那么负责多变的直接用if else来区分显得乱。那我们我们可以用 Linux 命令行解析函数getopt()、getopt_long()、getopt_long_only()

  1. getopt() : 这是最基本的命令行参数解析函数,只能处理短选项(单个字符)。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char *argv[]) {
    int opt;
    char *input_file = NULL;
    char *output_file = NULL;
    int verbose = 0;
    
    while ((opt = getopt(argc, argv, "i:o:v")) != -1) {
        switch (opt) {
            case 'i':
                input_file = optarg;
                break;
            case 'o':
                output_file = optarg;
                break;
            case 'v':
                verbose = 1;
                break;
            default:
                fprintf(stderr, "Usage: %s [-i input_file] [-o output_file] [-v]\n", argv[0]);
                exit(EXIT_FAILURE);
        }
    }
    
    printf("input_file = %s\n", input_file ? input_file : "NULL");
    printf("output_file = %s\n", output_file ? output_file : "NULL");
    printf("verbose = %d\n", verbose);
    
    return 0;
}
  1. getopt_long() : 这个函数可以处理长选项(多个字符),并且支持更多的选项处理方式。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>

int main(int argc, char *argv[]) {
    int opt;
    char *input_file = NULL;
    char *output_file = NULL;
    int verbose = 0;
    
    struct option long_options[] = {
        {"input", required_argument, NULL, 'i'},
        {"output", required_argument, NULL, 'o'},
        {"verbose", no_argument, NULL, 'v'},
        {NULL, 0, NULL, 0}
    };
    
    while ((opt = getopt_long(argc, argv, "i:o:v", long_options, NULL)) != -1) {
        switch (opt) {
            case 'i':
                input_file = optarg;
                break;
            case 'o':
                output_file = optarg;
                break;
            case 'v':
                verbose = 1;
                break;
            default:
                fprintf(stderr, "Usage: %s [--input input_file] [--output output_file] [--verbose]\n", argv[0]);
                exit(EXIT_FAILURE);
        }
    }
    
    printf("input_file = %s\n", input_file ? input_file : "NULL");
    printf("output_file = %s\n", output_file ? output_file : "NULL");
    printf("verbose = %d\n", verbose);
    
    return 0;
}
  1. getopt_long_only() : 这个函数与 getopt_long() 类似,但是它可以处理短选项和长选项的混合使用。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>

int main(int argc, char *argv[]) {
    int opt;
    char *input_file = NULL;
    char *output_file = NULL;
    int verbose = 0;
    
    struct option long_options[] = {
        {"input", required_argument, NULL, 'i'},
        {"output", required_argument, NULL, 'o'},
        {"verbose", no_argument, NULL, 'v'},
        {NULL, 0, NULL, 0}
    };
    
    while ((opt = getopt_long_only(argc, argv, "i:o:v", long_options, NULL)) != -1) {
        switch (opt) {
            case 'i':
                input_file = optarg;
                break;
            case 'o':
                output_file = optarg;
                break;
            case 'v':
                verbose = 1;
                break;
            default:
                fprintf(stderr, "Usage: %s [--input input_file] [--output output_file] [--verbose]\n", argv[0]);
                exit(EXIT_FAILURE);
        }
    }
    
    printf("input_file = %s\n", input_file ? input_file : "NULL");
    printf("output_file = %s\n", output_file ? output_file : "NULL");
    printf("verbose = %d\n", verbose);
    
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

起风就扬帆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值