我正在寻找一个自定义终端功能change_case
,其功能如下,可帮助我管理我的网站上的标题:
change_case [option] "string"
option:
upper - STRING TO UPPERCASE
lower - string to lowercase
sentence - Uppercase first letter of every word, others to lowercase
custom - String to Sentence Case except These Words if they appear as the 1st letter:
[in,by,with,of,a,to,is,and,the]
示例标题 -我怎样才能使登录屏幕出现而不是自动登录?
上:如何才能显示登录屏幕而不是自动登录?
降低:我怎样才能使登录屏幕出现而不是自动登录?
句子:如何才能使登录屏幕出现而不是自动登录?
风俗:如何才能使登录屏幕出现而不是自动登录?
答案1
这不太复杂:
- 将下面的脚本复制到一个空文件中,将其保存为
change_case
(无扩展名)~/bin
(您可能必须创建目录)。使脚本可执行 - 您可能必须注销/登录,特别是当目录尚不存在时(或者,运行
source ~/.profile
:) 打开终端窗口,通过运行命令进行测试:
change_case custom this is a test case to see if all in the script works
输出:
This is a Test Case to See If All in the Script Works
我用你问题中的所有选项(大写、小写、句子、自定义)对其进行了测试,所有选项都应该可以像你的例子一样工作。
剧本
#!/usr/bin/env python3
import sys
string = sys.argv[2:]
opt = sys.argv[1]
excluded = ["in","by","with","of","a","to","is","and","the"]
if opt == "lower":
print((" ").join(string).lower())
elif opt == "upper":
print((" ").join(string).upper())
elif opt == "sentence":
print((" ").join(string).title())
elif opt == "custom":
line = []
for s in string:
s = s.title() if not s in excluded else s
line.append(s)
print((" ").join(line))