是否有“getopt"的实现?德尔福? [英] Is there an implementation of "getopt" for Delphi?

查看:20
本文介绍了是否有“getopt"的实现?德尔福?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C/C++ 中使用 getopt() 解析命令行参数并不容易.

It doesn't get much easier than using getopt() to parse command line parameters in C/C++.

Delphi 有类似的东西吗?或者理想情况下,使用相同的语法?我知道 Delphi 支持 FindCmdLineSwitch 和 ParamStr(),但它们仍然需要一些额外的解析.

Is there anything similar for Delphi? Or ideally, with the same syntax? I know Delphi supports FindCmdLineSwitch and ParamStr(), but those still require some additional parsing.

我想要像 C 中的 getopt() 一样工作的东西.可以轻松实现基本切换开关以及在切换后捕获值的东西.请参阅下面的一些示例 C 代码以了解我在说什么:

I want something that works like getopt() in C. Something that easily allows basic toggle switches, as well as capturing a value after a switch. See below for some example C code to see what I'm talking about:

void print_help()
{
        printf("usage:
") ;
        printf("		-i set input file
") ;
        printf("		-o set output file
") ;
        printf("		-c set config file
") ;
        printf("		-h print this help information
") ;
        printf("		-v print version
") ;
}
 char* input_file = NULL ;
        char *query=NULL;
          char opt_char=0;
        while ((opt_char = getopt(argc, argv, "i:q:vh")) != -1)
        {
                switch(opt_char)
                {
                        case 'h':
                                print_help();
                                exit(-1);
                                break;
                        case 'v':
                                print_version() ;
                                exit(-1) ;
                                break ;
                        case 'i':
                                input_file= optarg ;
                                break ;
                        case 'q':
                                query= optarg ;
                                break ;
                        default:
                                print_help();
                                exit(-1);
                                break;
                }
        }

推荐答案

有一个实现TGetOpt,号称

There is an implementation TGetOpt, claiming to

为 Delphi 实现一个 getopt 变体.它几乎与 POSIX 兼容,支持长选项、必需、可选和无参数

implement a getopt variant for Delphi. It is nearly POSIX compatible, supporting long options, required, optional and no arguments

您可以在这里找到它.

这篇关于是否有“getopt"的实现?德尔福?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆