0

如何将以下使用 click with shell + python repl(I think) 的工作示例转换为hy

python3 - "$@" <<'EOF'
import click

@click.command()
@click.option('--count', default=1, help='Number of greetings.')
@click.option('--name', prompt='Your name',
              help='The person to greet.')
def hello(count, name):
    """Simple program that greets NAME for a total of COUNT times."""
    for x in range(count):
        click.echo('Hello %s!' % name)

if __name__ == '__main__':
    hello()
EOF

当我将以下hy示例与 一起使用时./test.sh --name shadowrylander --count 3,我得到:

Usage: hy [OPTIONS]
Try 'hy --help' for help.

Error: Got unexpected extra argument (-)

当我应该得到:

Hello shadowrylander!
Hello shadowrylander!
Hello shadowrylander!
hy - "$@" <<'EOF'
(import click)

#@(
    (.command click)
    (.option click "--count" :default 1 :help "Number of greetings")
    (.option click "--name" :prompt "Your name" :help "The person to greet.")
    (defn hello
    [count name]
    """Simple program that greets NAME for a total of COUNT times."""
    (for [x (range count)]
        (.echo click "Hello %s" % name)))
)

(if (= __name__ "__main__") (hello))
EOF

通常我可以hy - "$@" <<'EOF' ... EOF毫无问题地使用。

4

2 回答 2

1

我对调试的了解还不够click,但是作为 Hy 开发人员,我可以验证错误消息是click由 Hy 自己的命令行参数处理产生的,而不是由hy.cmdline. 需要进行一些挖掘才能确定是否click需要更改 Hy 或 Hy 才能使这项工作正常进行。我的猜测是,clickHy 如何影响sys.argv或 Hy 如何部分取代 Python 解释器让我感到困惑。

您的 Hy 程序不太正确,因为它试图%用作中缀。.echo表格必须(.echo click (% "Hello %s" name))是. 通过此更改,以及保存test.hy并运行的 Hy 代码hy test.hy --name shadowrylander --count 3(而不是使用 shell 脚本作为中介),它可以按预期工作。

于 2021-12-26T16:44:16.620 回答
0

我的猜测是,Hy 如何影响 sys.argv 或 Hy 如何部分替换 Python 解释器让 click 感到困惑。

根据上面Kodiologist的回答,答案如下:

(import [sys [argv]])
(del (cut argv 0 1))

这将在click通话之前出现。

于 2021-12-26T19:38:25.870 回答