我们在Team Foundation Server (TFS)中有一个项目,其中有一个非英语字符(š)。当尝试编写一些与构建相关的脚本时,我们偶然发现了一个问题——我们不能将这个字母传递给命令行工具。命令提示符或其他东西会把它弄乱,tf.exe实用程序无法找到指定的项目。

我尝试了不同格式的.bat文件(ANSI, UTF-8,带BOM和不带BOM),以及用JavaScript编写脚本(本质上是Unicode) -但运气不好。如何执行程序并传递一个Unicode命令行?


当前回答

对于那些使用WSL但又不想要Cygwin或Git的额外包的人来说,wsltty是可用的,它只提供支持UTF-8的终端

其他回答

实际上,关键在于命令提示符实际上理解这些非英语字符,只是不能正确地显示它们。

当我在命令提示符中输入包含一些非英语字符的路径时,它显示为“?? ?”?????? ? ? ? ? ?”当您提交命令(cd "???????? ?????”在我的情况下),一切都按照预期工作。

Try:

chcp 65001

这会将代码页更改为UTF-8。此外,还需要使用Lucida控制台字体。

由于我还没有看到Python 2.7的完整答案,我将概述两个重要步骤和一个非常有用的可选步骤。

You need a font with Unicode support. Windows comes with Lucida Console which may be selected by right-clicking the title bar of command prompt and clicking the Defaults option. This also gives access to colours. Note that you can also change settings for command windows invoked in certain ways (e.g, open here, Visual Studio) by choosing Properties instead. You need to set the code page to cp65001, which appears to be Microsoft's attempt to offer UTF-7 and UTF-8 support to command prompt. Do this by running chcp 65001 in command prompt. Once set, it remains this way until the window is closed. You'll need to redo this every time you launch cmd.exe.

要获得更永久的解决方案,请参考超级用户上的答案。简而言之,在HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor中使用regedit创建一个REG_SZ (String)条目,并将其命名为AutoRun。将其值修改为chcp 65001。如果不想看到命令的输出消息,请使用@chcp 65001>nul。

有些程序在与这种编码进行交互时遇到麻烦,MinGW就是一个显著的例子,它在编译时出现无意义的错误消息而失败。尽管如此,这工作得非常好,并且不会对大多数程序造成错误。

我在这里看到了几个答案,但它们似乎没有解决这个问题——用户希望从命令行获得Unicode输入。

Windows使用UTF-16进行两个字节字符串的编码,因此您需要在程序中从操作系统获取这些。有两种方法可以做到这一点

1)微软有一个扩展,允许main采用一个宽字符数组: Int wmain(Int argc, wchar_t *argv[]); https://msdn.microsoft.com/en-us/library/6wd819wh.aspx

2)调用windows api获取命令行的unicode版本 wchar_t win_argv = (wchar_t)CommandLineToArgvW(GetCommandLineW(), &nargs); https://learn.microsoft.com/en-us/windows/desktop/api/shellapi/nf-shellapi-commandlinetoargvw

读这个:http://utf8everywhere.org 获取详细信息,特别是如果您支持其他操作系统。

从2019年6月开始,使用Windows 10,你将不必更改代码页。

参见“介绍Windows终端”(来自Kayla Cinnamon)和Microsoft/Terminal。 通过使用Consolas字体,将提供部分Unicode支持。

如Microsoft/Terminal issue 387中所述:

There are 87,887 ideographs currently in Unicode. You need all of them too? We need a boundary, and characters beyond that boundary should be handled by font fallback / font linking / whatever. What Consolas should cover: Characters that used as symbols that used by modern OSS programs in CLI. These characters should follow Consolas' design and metrics, and properly aligned with existing Consolas characters. What Consolas should NOT cover: Characters and punctuation of scripts that beyond Latin, Greek and Cyrillic, especially characters need complex shaping (like Arabic). These characters should be handled with font fallback.