Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: support multiple words #318

Merged
merged 2 commits into from Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
@@ -1,5 +1,5 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks.git
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.4.0
hooks:
- id: check-merge-conflict
Expand All @@ -11,7 +11,7 @@ repos:
exclude: '.bumpversion.cfg'
- id: requirements-txt-fixer
- id: trailing-whitespace
- repo: https://gitlab.com/pycqa/flake8
- repo: https://github.com/pycqa/flake8
rev: 3.8.4
hooks:
- id: flake8
Expand Down
23 changes: 13 additions & 10 deletions pypinyin/runner.py
Expand Up @@ -77,7 +77,7 @@ def get_parser():
parser.add_argument('-m', '--heteronym', help='enable heteronym',
action='store_true')
# 要查询的汉字
parser.add_argument('hans', help='chinese string')
parser.add_argument('hans', nargs='+', help='chinese string')
return parser


Expand All @@ -98,7 +98,9 @@ def main():
parser = get_parser()
options = parser.parse_args(args)
if PY2:
hans = options.hans.decode(sys.stdin.encoding or 'utf-8')
hans = [
han.decode(sys.stdin.encoding or 'utf-8') for han in options.hans
]
else:
hans = options.hans
func = getattr(pypinyin, options.func)
Expand All @@ -121,20 +123,21 @@ def main():
# 不输出任何字符,防止污染命令行命令的输出结果
# 其实主要是为了干掉 jieba 内的 print 语句 ;)
sys.stdout = sys.stderr = NullWriter()
result = func(hans, style=style, **kwargs)
results = [func(han, style=style, **kwargs) for han in hans]
# 恢复默认
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__

if not result:
print('')
elif result and isinstance(result, (list, tuple)):
if isinstance(result[0], (list, tuple)):
print(' '.join([','.join(s) for s in result]))
for result in results:
if not result:
print('')
elif result and isinstance(result, (list, tuple)):
if isinstance(result[0], (list, tuple)):
print(' '.join([','.join(s) for s in result]))
else:
print(result)
else:
print(result)
else:
print(result)


if __name__ == '__main__':
Expand Down