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

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

问题描述

如果你正在编写一个程序,它是在命令行中的可执行文件,你经常要为用户提供多个选项或标志,伴随着可能比一个参数多。我无意中发现我的方式,通过很多次,但有一些设计模式,通过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 spinning off the appropriate functions?

考虑:

myprogram -f filename -d directory -r regex

你如何组织code检索使用任何内置插件为您的语言参数之后? (特定语言的答案欢迎,如果这可以帮助你表达一个答案)

How do you organize the code 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的显示大量的man页面和链接的实现。

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

一般情况下,我在我的应用程序中的preferences或设置服务的论证处理器知道如何与沟通。然后参数被翻译成此服务的东西,应用比再查询。这可能是由于设置字典(如命名为文件名字符串设置)一样简单。

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天全站免登陆