Skip to content

Commit

Permalink
Merge 9cc8541 into 328ca43
Browse files Browse the repository at this point in the history
  • Loading branch information
wdscxsj committed Oct 24, 2017
2 parents 328ca43 + 9cc8541 commit 694810e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
34 changes: 22 additions & 12 deletions pypinyin/runner.py
Expand Up @@ -13,6 +13,16 @@
)
from pypinyin.compat import PY2

_formal_styles = ['NORMAL', 'TONE', 'TONE2', 'TONE3',
'INITIALS', 'FIRST_LETTER', 'FINALS',
'FINALS_TONE', 'FINALS_TONE2', 'FINALS_TONE3',
'BOPOMOFO', 'BOPOMOFO_FIRST', 'CYRILLIC', 'CYRILLIC_FIRST']
_layman_styles = ['zhao', 'zh4ao', 'zha4o', 'zhao4', 'zh', 'z',
'ao', 'ào', 'a4o', 'ao4']
_option_styles = _layman_styles + \
_formal_styles[len(_layman_styles) - len(_formal_styles):]
_default_style = _layman_styles[1]


class NullWriter(object):
"""数据流黑洞,类似 linux/unix 下 /dev/null 的效果。"""
Expand All @@ -25,24 +35,24 @@ def get_parser():
parser.add_argument('-V', '--version', action='version',
version='{0} {1}'.format(__title__, __version__))
# 要执行的函数名称
parser.add_argument('--func', help='function name (default: "pinyin")',
parser.add_argument('-f', '--func',
help='function name (default: "pinyin")',
choices=['pinyin', 'slug'],
default='pinyin')
# 拼音风格
parser.add_argument('--style', help='pinyin style (default: "TONE")',
choices=['NORMAL', 'TONE', 'TONE2', 'TONE3',
'INITIALS', 'FIRST_LETTER', 'FINALS',
'FINALS_TONE', 'FINALS_TONE2', 'FINALS_TONE3',
'BOPOMOFO', 'BOPOMOFO_FIRST',
'CYRILLIC', 'CYRILLIC_FIRST'], default='TONE')
parser.add_argument('--separator', help='slug separator (default: "-")',
parser.add_argument('-s', '--style',
help='pinyin style (default: "%s")' % _default_style,
choices=_option_styles, default=_default_style)
parser.add_argument('-p', '--separator',
help='slug separator (default: "-")',
default='-')
parser.add_argument('--errors', help=('how to handle none-pinyin string '
'(default: "default")'),
parser.add_argument('-e', '--errors',
help=('how to handle none-pinyin string'
' (default: "default")'),
choices=['default', 'ignore', 'replace'],
default='default')
# 输出多音字
parser.add_argument('--heteronym', help='enable heteronym',
parser.add_argument('-m', '--heteronym', help='enable heteronym',
action='store_true')
# 要查询的汉字
parser.add_argument('hans', help='chinese string')
Expand Down Expand Up @@ -71,7 +81,7 @@ def main():
else:
hans = options.hans
func = globals()[options.func]
style = globals()[options.style]
style = globals()[_formal_styles[_option_styles.index(options.style)]]
heteronym = options.heteronym
separator = options.separator
errors = options.errors
Expand Down
6 changes: 3 additions & 3 deletions tests/test_cmd.py
Expand Up @@ -9,7 +9,7 @@
def test_default():
options = get_parser().parse_args(['你好'])
assert options.func == 'pinyin'
assert options.style == 'TONE'
assert options.style == 'zh4ao'
assert options.separator == '-'
assert not options.heteronym
assert options.hans == '你好'
Expand All @@ -18,12 +18,12 @@ def test_default():

def test_custom():
options = get_parser().parse_args(['--func', 'slug',
'--style', 'NORMAL',
'--style', 'zhao',
'--separator', ' ',
'--errors', 'ignore',
'--heteronym', '你好啊'])
assert options.func == 'slug'
assert options.style == 'NORMAL'
assert options.style == 'zhao'
assert options.separator == ' '
assert options.errors == 'ignore'
assert options.heteronym
Expand Down

0 comments on commit 694810e

Please sign in to comment.