处理命令行参数的设计模式是什么 [英] What is the design pattern for processing command line arguments

查看:32
本文介绍了处理命令行参数的设计模式是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您正在编写可从命令行执行的程序,您通常希望为用户提供多个选项或标志,以及可能不止一个参数.我已经多次遇到这种情况,但是是否有某种设计模式可以循环遍历 args 并调用适当的处理程序函数?

If you are writing a program that is executable from the command line, you often want to offer the user several options or flags, along with possibly more than one argument. I have stumbled my way through this many times, but is there some sort of design pattern for looping through args and calling the appropriate handler functions?

考虑:

myprogram -f filename -d directory -r regex

在使用您的语言的任何内置程序检索参数后,您如何组织处理程序函数?(欢迎提供特定语言的答案,如果这有助于您阐明答案)

How do you organize the handler functions after you retrieve the arguments using whatever built-ins for your language? (language-specific answers welcomed, if that helps you articulate an answer)

推荐答案

我不知道任何记录在案的模式"进行处理.

I don't know of any documented "patterns" for processing.

我相信用于处理参数的最古老的库/API 之一是 getopt.谷歌搜索getopt"会显示大量手册页和实现链接.

I believe one of the oldest libraries/APIs for handling arguments is getopt. Googling "getopt" shows lots of man pages and links to implementations.

通常,我的应用程序中有一个首选项或设置服务,参数处理器知道如何与之通信.然后将参数转换为该服务中应用程序然后查询的内容.这可以像设置字典一样简单(例如名为filename"的字符串设置).

Generally, I have a preferences or settings service in my application that the argument processor knows how to communicate with. Arguments are then translated into something in this service that the application than then query. This could be as simple as a dictionary of settings (like a string setting named "filename").

这篇关于处理命令行参数的设计模式是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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