解析交互式输入的技术(如 shell)?
我正在开发一个程序,可以通过选项和输入文件直接从命令行使用,或者像 shell 一样完全交互式使用。对于初始执行,我使用 GNU 的 Getopt 来解析命令行选项。
当用于文件时,我使用 Flex 和 Bison。这极大地简化了解析,因为语法非常简单,但我不完全确定应该如何处理 shell 方面。我以前使用过 GNU 的 readline 和历史库,当我这样做时,我仅依赖于 strtok 以及与嵌套 switch 语句的许多比较。它有效,但对我来说似乎有点像黑客工作......
有没有更好的方法来解决这个问题?
对于 shell 允许的数据输入,我正在考虑将其直接传输到临时文件并再次使用 Flex 和 Bison,但是对于各种参数(例如 Getopt 现在为我解析的命令行选项)是否有更好的方法?
我正在考虑尝试回收我的 getopt 代码,因为它可以灵活地捕获所有内容,如果它不是一个选项,我可以假设它的数据并将其输出。但我希望有第二意见。
谢谢
I'm working on a program that can be used directly from the command line with options and input files, or entirely interactively like a shell. For the initial execution I'm using GNU's Getopt to parse the command line options.
When being used on a file I'm using Flex and Bison. This simplifies the parsing greatly since the grammar is very simple, but I'm not entirely sure how I should tackle the shell aspect. I have used GNU's readline and history libraries before, when then I did this I relied solely on strtok and many comparisons with nested switch statements. It worked but it seemed kind of like a hack-job too me...
Is there a better way to approach this problem?
For the data input that the shell would allow I was thinking about piping it directly to a temp file and using Flex and Bison again, but for various parameters(like the command line options that Getopt is parsing for me now) is there a better way?
I was toying around with the idea of trying to recycle my getopt code, since its flexible to capture everything and if its not a option I could assume its data and pipe it out. But I'd love a 2nd opinion.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
用Python写就可以了。使用
cmd
模块编写shell程序,并使用shlex
像shell一样解析输入。Just write it in Python. Use the
cmd
module to write the shell program and useshlex
for parsing input just like the shell.