Linux下很多程序甚至那些具有图形用户界面(graphical user interface,GUI)的程序,都能接受和处理命令行选项。对于某些程序,这是与其他程序或用户进行交互的主要手段。具有可靠的复杂命令行参数处理机制,会使得您的应用程序更好、更有用。不过很多开发人员都将其宝贵的时间花在了编写自己的命令行解析器,却不使用 getopt(),而后者是一个专门设计来减轻命令行处理负担的库函数。 1、命令行参数 命令行程序设计的首要任务是解...
今天学习 getopt命令时,用到了[code]ARGS=`getopt -a -o p:P:O:t:l:h -l process:,port:,time:,output:,user:,help -- "$@"`[/code]这里的 -- "$@" 有什么作用? 因为在后面的代码中 while true: do case $1 in -a) func1;shift;; -b) func2;shift;; --) shift;break;; esac shift done 如果没 --)这里 ,这个循环跳不出来!
1. getopt的作用是用来解析命令行选项,譬如:>mysql -u lvdbing -p test -h 192.168.1.20 2. getopt提供的函数包括:Python 2.6.1+ (r261:67515, Mar 2 2009, 13:11:28) [GCC 4.3.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import getopt >>> getopt.__all__ ['getoptError', 'error', 'getopt', 'gnu_getopt'] >>> 3. 帮助文档的例子:>>> s = '--condition=foo --testing ...
linux 环境下 [code] stan@stan /temp $ ls -al echo.c -rw-r--r-- 1 stan users 300 2008-07-25 12:00 echo.c stan@stan /temp $ ls echo.c -al -rw-r--r-- 1 stan users 300 2008-07-25 12:00 echo.c [/code] 选项参数放在选项之前貌似也是可以的嘛!以前不都是要求选项参数放在选项之后,是否次序无所谓啊?这个功能是由getopt实现的吗? Thanks
char *svalue=NULL; while((opt=getopt(argc,argv," :ls: ")!=-1)) . . . case 's': svalue=optarg; break; RUN: ./a -s file1 1.问题: 如果我想-s接收的是多个不定的参数请问怎么改呢? 如: -s file1 file2 或 -s file1 file2 file3 2.在书上看 opt=getopt(argc,argv," :ls::k ")!=-1 s后两个"::"表示接收两个参数?还是两个以上的参数呢?
我在一本书上看到:如果getopt()找不到符合的参数则会印出错信息,并将全域变量optopt设为“?”字符。
但是程序显示好像不是这样:[code]#include
#include <stdio.h>
#include <stdlib.h>