无论位置如何,如何解析配置文件中的参数?

发布于 2024-11-03 19:05:55 字数 561 浏览 2 评论 0原文

例如,配置文件可以有

CFLAGS = "xyz"
CXXFLAGS = "xyz"

OR

CXXFLAGS = "xyz"
CFLAGS = "xyz"

我能想到的最佳实现是将参数分开并将左侧输入开关

for line in file
    x = line.split("=")
    switch(x[0])
        case CFLAGS
             do cflags
        case CXXFLAGS
             do cxxflags

但是比我有更多经验的人是如何做到的?我知道可能有一些开源程序可以做到这一点,但我什至不知道在哪里查找他们的源代码。 我主要使用 python 和 C 进行编程,因此两者中的实现/伪代码/whattolookup 将是首选,尽管其他语言也可以。

提前致谢。

PS 尽量避免在答案中使用任何形式的 re、regex、regexp、正则表达式或其任何派生词,除非不可避免:P。

e.g. A configuration file can have

CFLAGS = "xyz"
CXXFLAGS = "xyz"

OR

CXXFLAGS = "xyz"
CFLAGS = "xyz"

Best implementation I could think of would be to just split the argument and feed the left side into a switch

for line in file
    x = line.split("=")
    switch(x[0])
        case CFLAGS
             do cflags
        case CXXFLAGS
             do cxxflags

But how do people who have way more experience than me do it? I know theres probably some open source programs who do this but I wouldn't even know where to look in their source for this.
I program mainly in python and C so implementations/pseudocode/whattolookup in both would be preferred although other languages are fine also.

Thanks in advance.

P.S. try to avoid saying any form of re, regex, regexp, regular expressions, or any derivative thereof in your answers unless its unavoidable :P.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

予囚 2024-11-10 19:05:55

在 Python 中,只需使用 ConfigParser 模块即可为您解析类似 .ini 的配置文件。

自己重新实现这一点,我发现将配置数据视为一种字典很方便。这自然会转换为 Python 的字典,因此如果我将行拆分为 = 我只是继续更新:

confdict[key] = value

使用此方案,配置文件中键的顺序并不重要,就像字典本身并不重要 - 只要您可以查找键的值,你很高兴。


例如,如果您查看 ConfigParser 的内部结构(相关方法是 _read),您会发现这正是它的作用。这些选项保存在字典中(每个部分一个,因为 .ini 配置文件具有一层层次结构)。使用正则表达式从文件中解析行,并将键、值对添加到字典中,如上所述。


这是Python。在 C 中,我想有很多库可以做到这一点,但是实现你自己的库将遵循完全相同的算法。您可以对字典使用某种关联数组数据结构(哈希表、树或其他什么,并不重要),并进行相同的解析和解析。分配。

In Python just use the ConfigParser module which will parse .ini-like configuration files for you.

Re implementing this yourself, I find it convenient to view configuration data as a kind of dictionary. This naturally translates to Python's dicts, so if I split the line to <key> = <value> I just go on and update:

confdict[key] = value

With this scheme, the order of the keys in the configuration file doesn't matter, just like it doesn't matter in the dictionary itself - as long as you can lookup values for keys, you're happy.


If you look under the hood of ConfigParser, for example (the relevant method is _read), you will find this is exactly what it does. The options are kept in a dictionary (one per section, because .ini configuration files have one level of hierarchy). Lines are parsed from the file using regular expressions and key, value pairs are added to the dictionary as I described above.


This is Python. In C, I imagine there are quite a few libraries for doing this, but implementing your own would follow exactly the same algorithm. You'd use some kind of associative array data structure for the dictionary (hash table, tree, or whatever, doesn't really matter) and do the same parsing & assigning.

浊酒尽余欢 2024-11-10 19:05:55

正如Eli Bendersky 说,在 Python 中你应该只使用提供的 ConfigParser

如果您坚持自己做,我推荐他将配置存储为字典的方法。另一种方法是将选项映射到处理值并用它们执行某些操作的函数:

# Map the options to functions to handle them.
handlers = {
    'CFLAGS': some_function,
    'CXXFLAGS': other_function,
}

# Loop through each option.
for line in configuration:
    # Get the option and value.
    option, value = line.split('=')
    option = option.strip().upper()
    value = value.strip()

    # Try to find a handler and process the option.
    handler = handlers.get(option)
    if handler:
        handler(option, value)
    else:
        raise Exception('Unknown option.')

显然,处理函数必须能够接受您的 optionvalue 参数重新通过它。

As Eli Bendersky says, in Python you should just use the provided ConfigParser.

If you insist on doing it yourself, his method of storing the configuration as a dictionary is one I recommend. Another way is to map the options to functions which process the values and do something with them:

# Map the options to functions to handle them.
handlers = {
    'CFLAGS': some_function,
    'CXXFLAGS': other_function,
}

# Loop through each option.
for line in configuration:
    # Get the option and value.
    option, value = line.split('=')
    option = option.strip().upper()
    value = value.strip()

    # Try to find a handler and process the option.
    handler = handlers.get(option)
    if handler:
        handler(option, value)
    else:
        raise Exception('Unknown option.')

Obviously, the handler functions must be able to accept the option and value parameters you're passing it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文