arguments and options and optarg in C

简介:
用C写一个命令行工具的话, 可能会经常用到argument和options.
例如 : 
1. ps -ewf 这里-ewf就是option.
2. ps -eo rs,cmd 这里 rs 和 cmd则是option -o 的 optarg
3. ls /root 这里/root则是ls命令的argument.

在C里面怎么应用呢?
使用到的是unistd.h, 
SYNOPSIS
       #include <unistd.h>

       int getopt(int argc, char * const argv[], const char *optstring);
       extern char *optarg;
       extern int optind, opterr, optopt;

getopt 用于获取定义好的option 字符, 带冒号表示这个option有optarg. 在命令中调用option使用一个横杠.
optarg 是个字符指针.
optind 表示The variable optind is the index of the next element of the argv[] vector to be processed.

举个例子 : 
[root@db-172-16-3-150 zzz]# cat n.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>

int main(int argc, char * argv[]) {
  int digoal;
  char *equipment="";
  char ch;
  int i;
  while((ch = getopt(argc, argv, "de:")) != EOF) {
    switch(ch) {
      case 'd':
        digoal = 1;
        break;
      case 'e':
        equipment = optarg;
        break;
      default:
        fprintf(stderr, "Unknown option: %s\n", optarg);
        return 1;
    }
  }
  if (digoal)
    fprintf(stdout, "yes -d used\n");
  if (*equipment)
    fprintf(stdout, "equipment is:%s\n", equipment);
  for(i=optind;i<argc;i++) {
    fprintf(stdout, "your argument %i is:%s\n", i, argv[i]);
  }
  fprintf(stdout, "your argument %i is:%s\n", 0, argv[0]);  //argv[0] 指命令本身.
  return 0;
}

结果
[root@db-172-16-3-150 zzz]# gcc -O3 -Wall -Wextra -Werror -g ./n.c -o n && ./n -d -e tank -- -1 a b c d e
yes -d used
equipment is:tank
your argument 5 is:-1
your argument 6 is:a
your argument 7 is:b
your argument 8 is:c
your argument 9 is:d
your argument 10 is:e
your argument 0 is:./n


注意如果把两个杠杠去掉会报错, 因为getopt()会一直检索, 直到遇到-- 结束. 它会吧-1 当成option.
[root@db-172-16-3-150 zzz]# gcc -O3 -Wall -Wextra -Werror -g ./n.c -o n && ./n -d -e tank -1 a b c d e
./n: invalid option -- 1
Unknown option: (null)


把-1 放在-e后面是没问题的, 因为-e 后面被任务是optarg.
[root@db-172-16-3-150 zzz]# gcc -O3 -Wall -Wextra -Werror -g ./n.c -o n && ./n -d -e -1 a b c d e
yes -d used
equipment is:-1
your argument 4 is:a
your argument 5 is:b
your argument 6 is:c
your argument 7 is:d
your argument 8 is:e
your argument 0 is:./n

空格是各个选项的分隔符 : 
[root@db-172-16-3-150 zzz]# gcc -O3 -Wall -Wextra -Werror -g ./n.c -o n && ./n -d -e a,b,c,d a b c d e
yes -d used
equipment is:a,b,c,d
your argument 4 is:a
your argument 5 is:b
your argument 6 is:c
your argument 7 is:d
your argument 8 is:e
your argument 0 is:./n

目录
相关文章
|
Java 计算机视觉
实现邮箱验证(邮箱验证码登录)
我们要实现web或者Java的发送邮箱验证码到邮箱上进行验证。当然我们需要做一下前提的准备,也就是先要导我们的jar包,然后再进行下一步的操作。
|
11月前
|
负载均衡 监控 Dubbo
Dubbo 原理和机制详解(非常全面)
本文详细解析了 Dubbo 的核心功能、组件、架构设计及调用流程,涵盖远程方法调用、智能容错、负载均衡、服务注册与发现等内容。欢迎留言交流。关注【mikechen的互联网架构】,10年+BAT架构经验倾囊相授。
Dubbo 原理和机制详解(非常全面)
|
Web App开发 Android开发 内存技术
|
4天前
|
存储 关系型数据库 分布式数据库
PostgreSQL 18 发布,快来 PolarDB 尝鲜!
PostgreSQL 18 发布,PolarDB for PostgreSQL 全面兼容。新版本支持异步I/O、UUIDv7、虚拟生成列、逻辑复制增强及OAuth认证,显著提升性能与安全。PolarDB-PG 18 支持存算分离架构,融合海量弹性存储与极致计算性能,搭配丰富插件生态,为企业提供高效、稳定、灵活的云数据库解决方案,助力企业数字化转型如虎添翼!
|
15天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1313 5
|
2天前
|
监控 JavaScript Java
基于大模型技术的反欺诈知识问答系统
随着互联网与金融科技发展,网络欺诈频发,构建高效反欺诈平台成为迫切需求。本文基于Java、Vue.js、Spring Boot与MySQL技术,设计实现集欺诈识别、宣传教育、用户互动于一体的反欺诈系统,提升公众防范意识,助力企业合规与用户权益保护。
|
14天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
1356 87
|
2天前
|
JavaScript Java 大数据
基于JavaWeb的销售管理系统设计系统
本系统基于Java、MySQL、Spring Boot与Vue.js技术,构建高效、可扩展的销售管理平台,实现客户、订单、数据可视化等全流程自动化管理,提升企业运营效率与决策能力。