Skip to content

Commit

Permalink
cli: support multiple words (#318)
Browse files Browse the repository at this point in the history
* Fix #308, support multiple words

* Use github in pre-commit
  • Loading branch information
Freed-Wu authored Mar 3, 2024
1 parent 5fd3403 commit c2ab6be
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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

0 comments on commit c2ab6be

Please sign in to comment.