繁体   English   中英

如何处理命令行应用程序用户输入中的冒号?

[英]How to handle colons in command-line application user inputs?

var name: String?
print("What is the name?")

if let input = readLine() {
    name = input
}

if let name = name {
    print("You input: \(name)")
}

我有一个命令行应用程序,要求用户输入一个可能包含冒号的名称。 在 macOS zsh 终端中,当用户输入冒号时,它会自动完成看起来像字典的内容:

acidgate % What is the name? // prompt user for input
acidgate % [Acid Gate: 2020] // the user's input is autocompleted with brackets

这就是我想要的样子:

acidgate % What is the name?
acidgate % Acid Gate: 2020 // no autocomplete

如果我可以在我的应用程序中禁用此自动完成功能,那将是理想的。 如果没有,我该如何处理响应? 因为如果用户在为他自动完成后手动删除括号,字符串会有奇怪的伪像:

You input: Acid Gate: 2020� // printing the user's input

但是,如果用户不删除方括号并输入[Acid Gate: 2020] ,则打印控制台似乎会显示一个干净的字符串:

You input: Acid Gate: 2020

看起来oh-my-zsh_swift完成方案正在被调用。

您应该能够使用不识别冒号的更简单版本覆盖 omz 完成,例如:

compdef '_files -g "*"' swift

更多信息: https : //superuser.com/questions/686267/disabling-oh-my-zsh-svn-completion

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM