Linux中getopt函数、optind等变量使用详解

11、getopt函数 (一) 在Linux中,用命令行执行可执行文件时可能会涉及到给其加入不同的参数的问题,例如: ./a.out -a1234 -b432 -c -d 程序会根据读取的参数执行相应的操作,在C语言中,这个功能一般是靠getopt()这个函数,结合switch语句来完成的,首先来看下面的代码: #include <stdio.h> #include <unistd.h&g... 阅读详情

最近在学习《Unix网络编程》vol2时,发现书中例子经常使用一个命令行解析getopt函数,因为函数声明比较特别,根据自己摸索,遂总结出使用方法。

1. getopt函数的声明

该函数是由Unix标准库提供的函数,查看命令man 3 getopt

#include <unistd.h>
int getopt(int argc, char * const argv[], const char *optstring);

extern char *optarg;
extern int optind, opterr, optopt;


getopt函数的参数:

参数argc和argv:

      通常是从main的参数直接传递而来,argc是参数的数量,argv是一个常量字符串数组的地址。
参数optstring:

      一个包含正确选项字符的字符串,如果一个字符后面有冒号,那么这个选项在传递参数时就需要跟着一个参数。
外部变量:

char *optarg:如果有参数,则包含当前选项参数字符串
int optind:argv的当前索引值。当getopt函数在while循环中使用时,剩下的字符串为操作数,下标从optind到argc-1。
int opterr:这个变量非零时,getopt()函数为“无效选项”和“缺少参数选项,并输出其错误信息。
int optopt:当发现无效选项字符之时,getopt()函数或返回 \’ ? \’ 字符,或返回字符 \’ : \’ ,并且optopt包含了所发现的无效选项字符。


根据下面的程序理解这四个参数:

2. getopt函数的使用

在man 3 getopt给出了该函数的例子

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

int main(int argc, char *argv[])
{
    int flags = 0, opt;
    int nsecs = 0, tfnd = 0;

    while((opt = getopt(argc, argv, "nt:")) != -1) {
            switch (opt) {
                case 'n':
                    flags = 1;
                    break;
                case 't':
                    nsecs = atoi(optarg);
                    tfnd = 1;
                    break;
                default:
                    printf("optopt = %c\n", (char)optopt);
                    printf("opterr = %d\n", opterr);
                    fprintf(stderr, "usage: %s [-t nsecs] [-n] name\n", argv[0]);
                   exit(EXIT_FAILURE);
            } 
    }
    printf("flags = %d; tfnd = %d; nsecs = %d; optind = %d\n", flags, tfnd, nsecs, optind);

    printf("optind = %d\n", optind);
    printf("argc = %d\n", argc);
#if 1 
    if(optind >= argc) {
        fprintf(stderr, "Expected argument after options\n"); 
        exit(1);
    }
#endif
    printf("name argument = %s\n", argv[optind]);
    /* Other code omitted */
    return 0;
}



getopt函数的第三个字符串参数 “nt:”:

     在man page中这样解释:-n, with no associated value; and -t val, which expects an associated value。

     中文意思为:-n 不用一个关联值;-t 需要有一个关联的value参数。


运行结果:

➜  test ./a.out -a  name    //首先 -a 是一个非法的选项
./a.out: invalid option -- 'a'  //输出错误信息,因为非法选项
optind = 2  //此时argv[optind]是我们的操作数,也就是我们传递给主函数的参数
optopt = a  //当发现无效项字符时,optopt会包含该字符,正如我们传递的‘a’这个无效项。
opterr = 1  //opterr变量非零,getopt()函数为“无效选项”和“缺少参数选项,并输出其错误信息。
usage: ./a.out [-t nsecs] [-n] name

再次运行:

➜  test ./a.out -t        //根据getopt函数的第三个参数,因为‘t’字符后有‘:’冒号,因此在‘-t’选项后需要跟随一个参数。因此此次运行的错误为“缺少参数”,如下提示:
./a.out: option requires an argument -- 't' //输出了错误信息,因为“缺少参数”
optind = 2  //argv[optind]为空,因为此次运行没有传递参数
optopt = t  //当发现无效项字符时,optopt会包含该字符,正如我们传递的‘t’缺少参数的选项。
opterr = 1  //opterr变量非零,getopt()函数为“无效选项”和“缺少参数“选项,并输出其错误信息。
usage: ./a.out [-t nsecs] [-n] name


再次运行:

➜  test ./a.out -t 123 -n     //此时,根据字符串”-nt:“应该得知,-t 后应加参数,-n不用加
flags = 1; tfnd = 1; nsecs = 123; optind = 4    //在switch语句中将flags=1,tfnd=1,因为optarg变量保存这当前选项参数的字符串,因此此时optarg保存的是‘-t’参数的参数并用atoi函数转称整数,因此nsecs=123。
optind = 4  //argv[optind]为空,因为没有向主函数传递参数
argc = 4    //一共四个字符串
Expected argument after options //该程序希望我们至少传递一个参数,因此在if语句中退出。

再次运行:

➜  test ./a.out -t 123 -n hello //‘-t’后加参数,‘-n’不用加,向main函数传递参数叫hello
flags = 1; tfnd = 1; nsecs = 123; optind = 4
optind = 4  //argv[optind] = ”hello“
argc = 5    //五个字符串
name argument = hello   //打印参数name argument= hello

其实可以传递多个参数(如果需要,因为此函数只打印第一个参数),例如:

➜  test ./a.out -t 123 -n hello world hello C
optind = 3
optind = 4
flags = 1; tfnd = 1; nsecs = 123; optind = 4
optind = 4
argc = 8
name argument = hello   


--------------------- 
原文:https://blog.csdn.net/men_wen/article/details/61934376 
 

 

深入探究Linux下getopt命令行参数解析的源码实现 Linux操作系统提供了一个强大的命令行环境,而命令行参数解析是这个环境中的基础功能之一。命令行参数允许用户在运行程序时向程序传递指令,以控制程序的行为和输出。解析这些参数,使得开发者能够根据用户的需求灵活地扩展程序功能。本章将简要介绍命令行参数解析的概念,并概述其在Linux编程中的重要性。在程序中使用getopt之前,必须定义命令行参数和选项。命令行参数通常在程序的main函数中通过argc和argv接收,而getopt需要一个选项字符串来定义期望的选项和参数。 阅读详情

相关推荐

Foxglove Studio实战:5步搞定自动驾驶数据可视化(附避坑指南)

本文详细介绍了如何使用Foxglove Studio高效搭建自动驾驶数据可视化工作流。通过五个核心步骤:环境准备、数据导入、面板搭建、交互分析与自动化集成,帮助开发者快速掌握这一专为机器人系统设计的开源平台。文章重点解析了其与ROS/ROS 2、MCAP等数据源的无缝对接,以及3D点云、多传感器融合、团队标注等核心功能,并附有实用的性能优化与避坑指南,旨在提升算法调试与团队协作效率。

d3e4f的博客 344

linux——getopt()和getopt_long()函数及optarg,optind, opterr, optopt全局变量

getopt() 函数 getopt() 头文件 #include&lt;unistd.h&gt; 定义函数 int getopt(int argc,char * const argv[ ],const char * optstring); extern char *optarg; extern int optind, opterr, optopt; getopt() 所设置的全局变量包括: o...

夜风的博客 1万+

MATLAB多项式计算

MATLAB多项式计算 在MATLAB中,n次多项式用一个长度为n+1的行向量表示(因为是n个幂次项系数加上一个常数项),缺少的幂次项系数为0。 例如 n次多项式: P(x)=anxn+an-1xn-1+an-2xn-2+…+a1x+a0 表示为: [an,an-1,an-2,…,a1,a0] 目录MATLAB多项式计算1.多项式的加减运算2.多项式的乘法运算3.多项式的除法运算4.多项式的导函数5.代数多项式求值6.矩阵多项式求值7.多项式求根 1.多项式的加减运算 事实上,多项式的加减运算就是其所对应的

路遥芝麻的博客 1万+

c语言中的getopt()、optind的使用

根据《Head First C》P152例子改编 结论: getopt()会在最后一次执行时将argv变量进行排序,将参数(“d:t”)中提到的变量排序到最前面。这样后面的argc、argv变量通过optind计算就会非常清楚,经过遍历getopt()之后的参数就是没有被指定顺序的。 #include #include void print_arg(int argc,

x_think的专栏 2099

LiveReload:释放你的F5,实时预览HTML效果

一、liveReload插件开发背景Web前端开发人员在写页面时,如果修改了HTML or CSS,要想看效果通常的做法就是保存代码就得 Alt+Tab,切换到浏览器(当然,我知道很多开发人员是双屏操作不一定需要切换到浏览器),但是一定要F5刷新才能看自己刚写的代码有没有效果,没有效果就再切换到编辑器下修改,然后再切换到浏览器F5刷新看效果,还没有效果,就再继续修改,如此反复。时间久了,前端自动化...

Char_n的博客 2518

量子磁场测量技术在生物磁场成像领域的应用现状与产业趋势

首先对量子磁场测量技术的发展进行了简述,然后介绍了量子磁场测量技术在生物磁场成像领域的应用,最后分析了量子磁场测量技术的两大重要应用方向(心磁图仪和脑磁图仪)及其产业现状和未来发展趋势。美国普林斯顿大学在《自然》上公布了目前人类磁场测量灵敏度最高的基于量子磁场测量技术的零场原子磁力计。拥有巨大的发展潜力及庞大的市场空间。因此其数千万元的高昂售价及每年数百万元的超导维护成本限制了超导式心磁图仪的发展和临床应用。而基于超导式脑磁图仪的癫痫、阿尔兹海默症、抑郁症等脑疾病患者的临床研究也取得丰富的研究成果。

罗伯特技术屋 1538

函数getopt(),及其参数optind

getopt被用来解析命令行选项参数。 #include       extern char*optarg;  //选项的参数指针       extern int optind,  //下一次调用getopt的时,从optind存储的位置处重新开始检查选项。        extern intopterr,  //当opterr=0时,getopt不向stderr输出错误信息。

1254

跨模型的 API 调用与集成:使用 Spring AI 构建可移植的 API 代码,支持多个 AI 模型(如 OpenAI、Anthropic 等)的调用

Spring AI 是 Spring 生态系统中的一部分,旨在提供一种简单、可扩展的方式来集成人工智能模型。Spring AI 使得开发者可以使用现有的 Spring 体系结构方便地访问多种 AI 模型和服务,例如 OpenAI、Anthropic、HuggingFace 等。

秋元的博客 2413

C语言linux getopt_long()函数(命令行解析)(getopt、getopt_long_only)(短选项 -,长选项 --)(option结构体)(optind、optarg变量)

参考文章:浅谈linux的命令行解析参数之getopt_long函数 文章目录前言一、关于命令行参数二、getopt_long函数参数以及返回值介绍(以上三个函数都适用):1、argc和argv和main函数的两个参数一致。2、optstring: 表示短选项字符串。3、longopts:表示长选项结构体。结构如下:4、longindex:longindex非空,它指向的变量将记录当前找到参数符合longopts里的第几个元素的描述,即是longopts的下标值。5、全局变量:6、返回值:三、测试(自行测试

Dontla的博客 1万+

手把手教您国内安装 Claude Code 并配置自定义大模型(包含New-API中转和可视化管理工具的使用)

国内免魔法畅用 Claude Code!手把手教你 Windows 安装、配置文件写法、对接阿里百炼 / DeepSeek / 智谱✨,附完整参数、正确命令、三方工具管理方案,新手一次成功!✅

XiaoqiangClub的博客 3805

基于大语言模型构建中医古籍智能问答系统:从数据清洗到模型微调全流程解析

大语言模型作为当前人工智能领域的核心技术,通过海量数据预训练掌握了强大的语言理解和生成能力。其原理在于基于Transformer架构,通过自注意力机制捕捉文本中的长距离依赖关系。这项技术的核心价值在于能够将非结构化的自然语言转化为可计算、可推理的知识表示,从而在众多垂直领域实现智能化应用。在医疗健康、文化传承等场景中,领域大模型通过针对性的微调,能够深入理解专业术语和逻辑范式,成为高效的智能助手。本文以中医古籍智能化处理为例,探讨如何通过构建高质量领域语料库、进行有效的继续预训练和指令微调,解决专业领域知识

weixin_42573647的博客 328

LINUX C 使用getopt函数以及其相关变量opterr optopt optind optarg

头文件 unistd.h. 函数及功能 int getopt (int argc , char *const * argv , const char * options ) 该函数从argv和argcgetopt参数指定的参数列表中获取下一个选项参数。通常这些值直接来自接收到的参数 。options参数是一个字符串,它指定对该程序有效的选项字符。此字符串中的选项字符可以后跟冒号 (’:’) 表示它需要一个必需的参数。如果选项字符后跟两个冒号 (’::’),它的参数是可选的;这是一个 GNU 扩展。getop

nb_zsy的博客 890

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

命令行选项参数解析-getopt函数

Parse command-line options NAMEgetopt, getopt_long, getopt_long_only, optarg, optind, opterr, optopt - Parse command-line optionsSYNOPSIS#includeint getopt(int argc, char * const argv[],const char *o

ty_laurel的博客 1173

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Linux下getopt()函数的简单使用

最近在弄Linux C编程,本科的时候没好好学啊,希望学弟学妹们引以为鉴。 好了,虽然啰嗦了点,但确实是忠告。步入正题: 我们的主角----getopt()函数。 英雄不问出处,getopt()函数的出处就是unistd.h头文件(哈哈),写代码的时候千万不要忘记把他老人家include上。 再来看一下这家伙的原型(不是六耳猕猴): int getopt(int argc,char * const argv[ ],const char * optstring); 前两个参数大家不会陌生,没.

u011029104的专栏 193

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

getopt()函数

???? 为什么会记录这个函数,因为这个函数还蛮有趣的,主要是我之前想实现下ping程序,需要用到这个函数。 该函数的作用是解析命令行参数 #include <unistd.h> int getopt(int argc, char * const argv[], const char *optstring); extern char *optarg; extern int optind, opterr, optopt; 描述 函数的作用是:解析命令行参数。它的参数argc和argv是程序调用时

weixin_44374594的博客 544

C语言中getopt()函数

在Linux中,用命令行执行可执行文件时可能会涉及到给其加入不同的参数的问题,例如: ./a.out -a1234 -b432 -c -d 程序会根据读取的参数执行相应的操作,在C语言中,这个功能一般是靠getopt()这个函数,结合switch语句来完成的,首先来看下面的代码: #include #include int main(int argc,ch

come on! 1132

getopt()和getopt_long()用法

参考链接: http://home.eeworld.com.cn/home.php?mod=space&do=blog&id=43897&uid=101752 http://blog.csdn.net/cashey1991/article/details/7942809 如果在LINUX环境下面你想做与用户交互的程序开发的话,这个函数我想会对你有很大的帮助!~ g...

weixin_30894583的博客 67

Linux下getopt()函数的简单使用-命令行参数处理

经常要写点Linux下命令行的测试程序,都需要对命令行参数做一些处理,以前都是自己来写参数的处理,不只每次写的都不一样,而且每次还浪费时间去做参数的判断,而且写出来的代码很难看,最近在看老大写的一个测试程序,发觉里面用到了getopt或getopt_long这个函数来处理,不只代码清晰,而且非常容易的就能实现人们常用的那种短横线带参数的命令行参数处理模式,在网上查了一些资料,发现介绍还是很多,特别介绍出来给大家,让大家不再纠结于命令行的处理,而更多的去专注功能的实现。 下面这篇文章讲的比较详细:...

咖啡走糖 210

linux/Unix系统调用之getopt()

getopt()解析命令行参数  使用前提: #include&lt;unistd.h&gt; extern char *optarg; extern int optind,opterr,optopt; //函数的定义 int getopt(int argc,char*const argv[],const char *optstring);   函数说明:        get...

王小多的博客 494

linux 参数解析,Linux 命令行参数解析

在linux中,经常需要各种命令,通常情况下都会带各种参数,而这些参数是如何解析的呢?通常使用GNU C提供的函数getopt、getopt_long、getopt_long_only函数来解析命令行参数。使用他们需要引用头文件getopt.h。1. getopt()函数getopt()用来解析命令行选项参数的,但是只能解析短选项: -d 100,不能解析长选项:--prefix;其原型:int ...

weixin_36257615的博客 774

C语言主函数命令行解析 api

带长解析命令参数函数 getopt_long我们经常在执行一些可执行文件或命令时常常会指定一些参数用来指定该命令执行某一些特殊操作,例如,我们执行ls,我们可以在后面加-l或-a,也可以-la,或者一般执行某个命令时都可以加--help来打印一些帮助信息,那么这些都是怎么做到的呢,我们的程序要怎么去处理这些命令选项呢?//用来保存选项的参数extern intoptind;//用来记录下一个检索位置extern intopterr;

jiu_yue_ya的博客 185
上一篇: thrift应用举例(c/c++作为服务端、java作为客户端)
下一篇: Lex+YACC or Flex+Bison
zzhongcy
博客等级 码龄18年 1254粉丝 362原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值