日期:2014-05-16  浏览次数:20732 次

基于Linux,为一个较大的应用软件实现一个c/c++命令行的界面 。。。
在Linux系统,有一个较大的应用软件,   现在要用c/c++为这个软件实现一个命令行的界面。   比如这个应用软件的名字叫   A,   在一般的shell下,   我输入   "A   Cmd1   argument1   argument2   ...   argumentN ",   系统运行命令后退出。这里cmd1会有100多个命令,他们属于多个不同的分类,比如系统用户管理类,系统网络管理类   等等。  

对于这个命令行的界面,当然还有一些别的要求,比如要支持命令和命令行参数的替换,   比如   "A   shutdown   arg1   arg2   ... "   和   "A   stop   arg1   arg2   ... "   一样,   有一些命令要支持setuid   。。。

会有100多个命令,   可能会有新的命令加入,所以要求这个命令行的界面地设计要容易扩展和修改。

大家对于设计这样一个c/c++命令行的界面有啥好的建议。不是要你们的code,   只要建议想法就可以了。

谢谢。

------解决方案--------------------
分析好,设计好,编写好,调试好
------解决方案--------------------
分析,模块化设计,编码,调试,发布!
------解决方案--------------------
linux c的man函数可以带参数的
读入参数 然后 判断
进行处理
apue上面有很多例子
------解决方案--------------------
一般用getopt库来解析命令行。有短格式和长格式,也可以两者都支持,比如:
A -a 123 -b 234
或者
A --arg1 123 --arg2 234

man 3 getopt
可以的到用法,里面有一个详细的可以运行的例子

------解决方案--------------------
顺便把man里面的例子贴给你,用getopt的话,参数顺序无所谓:

#include <stdio.h> /* for printf */
#include <stdlib.h> /* for exit */
#include <getopt.h>

int
main (int argc, char **argv) {
int c;
int digit_optind = 0;

while (1) {
int this_option_optind = optind ? optind : 1;
int option_index = 0;
static struct option long_options[] = {
{ "add ", 1, 0, 0},
{ "append ", 0, 0, 0},
{ "delete ", 1, 0, 0},
{ "verbose ", 0, 0, 0},
{ "create ", 1, 0, 'c '},
{ "file ", 1, 0, 0},
{0, 0, 0, 0}
};

c = getopt_long (argc, argv, "abc:d:012 ",
long_options, &option_index);
if (c == -1)
break;

switch (c) {
case 0:
printf ( "option %s ", long_options[option_index].name);
if (optarg)
printf ( " with arg %s ", optarg);
printf ( "\n ");
break;

case '0 ':
case '1 ':
case '2 ':
if (digit_optind != 0 && digit_optind != this_option_optind)
printf ( "digits occur in two different argv-elements.\n ");
digit_optind = this_option_optind;
printf ( "option %c\n ", c);
break;

case 'a ':
printf ( "option a\n ");
break;

case 'b ':
printf ( "option b\n ");
break;

case 'c ':
printf ( "option c with value '%s '\n ", optarg);
break;