【问题标题】:Using `click` in a shell script that has a Hy program in a here document在此处文档中包含 Hy 程序的 shell 脚本中使用 `click`
【发布时间】:2021-12-26 00:17:46
【问题描述】:

如何将以下使用点击与shell + python repl(我认为)的工作示例转换为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 - "$@" &lt;&lt;'EOF' ... EOF

【问题讨论】:

    标签: python-click hy


    【解决方案1】:

    我对@9​​87654321@ 了解不多,无法调试它,但作为Hy 开发人员,我可以验证错误消息是由click 产生的,而不是由Hy 自己在@987654323 中的命令行参数处理产生的@。要弄清楚是否需要更改 click 或 Hy 才能使这项工作正常进行,需要进行一些研究。我的猜测是 click 对 Hy 如何影响 sys.argv 或 Hy 如何部分替换 Python 解释器感到困惑。

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

    【讨论】:

    • 感谢您的提示;答案几乎是显而易见的:因为 hy 是一个 python 模块,python3hy 二进制路径添加到argv;所以虽然python3 会呈现click['-', '--name', 'shadowrylander', '--count', '3']hy 会呈现['/home/shadowrylander/.local/bin/hy', '-', '--name', 'shadowrylander', '--count', '3']。再次感谢所有帮助!
    【解决方案2】:

    我的猜测是,点击会被 Hy 如何影响 sys.argv 或 Hy 如何部分替换 Python 解释器所迷惑。

    根据Kodiologist's answer above,答案如下:

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

    这将在click 调用之前出现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-05
      • 2014-07-21
      • 2012-06-05
      • 2013-07-07
      • 1970-01-01
      • 1970-01-01
      • 2011-12-10
      相关资源
      最近更新 更多