glib 中 G_OPTION_ARG_FILENAME 和 G_OPTION_ARG_STRING 的区别

Difference between G_OPTION_ARG_FILENAME and G_OPTION_ARG_STRING in glib

我注意到 glib 中的 command line options parser 有一个字符串和文件名类型。但我假设文件名只是字符串。

G_OPTION_ARG_FILENAME 和 G_OPTION_ARG_STRING 之间有什么实际区别,还是 G_OPTION_ARG_FILENAME 只是为了更具描述性?

G_OPTION_ARG_STRING 更具体地说,是以当前区域设置编码的字符串。 G_OPTION_ARG_FILENAME 是在系统的文件名编码中编码的字符串。这些编码在现代 Linux 系统上通常都是 UTF-8,但在某些系统上它们可能不同。

如果您查看 GLib reference manual,您会发现以下内容:

G_OPTION_ARG_STRING   the option takes a string argument.
G_OPTION_ARG_FILENAME   the option takes a filename as argument.

字符串和文件名的编码可能不同,因此有两个选项。 Glib 对字符串使用 UTF-8。文件名的编码取决于操作系统,但通常也是 UTF-8;

By default, GLib assumes that file names on disk are in UTF-8 encoding. This is a valid assumption for file systems which were created relatively recently: most applications use UTF-8 encoding for their strings, and that is also what they use for the file names they create. However, older file systems may still contain file names created in "older" encodings, such as ISO-8859-1.

引文也来自GLib reference manual