何时将 -M、-A 或 -X 与 Clojure CLI 一起使用?

Jp_*_*Jp_ 6 clojure

我总是将 -A 与 clojure 工具一起使用,但有些警告说我应该使用 -M 代替,我找到了这个 doc,但还没有得出何时使用每个工具的结论。

Sea*_*eld 11

Clojure CLI 仍在不断发展,因此该-X选项相对较新,并且该-M选项的含义在同一时间范围内发生了变化。

您可以在此处查看 CLI 版本和简短的发行说明:https : //clojure.org/releases/tools

直到 2020 年年中,你都用上-A了。-M简单地运行:main-opts- 它不尊重任何类路径选项或解析选项。

在 1.10.1.697 版本(2020 年 9 月 25 日)中,-X引入了该选项以允许执行特定的 Clojure 函数,将数据的哈希映射作为单个参数传递。该版本还扩展了-M选项的行为以尊重:extra-paths:extra-deps运行:main-opts- 有效地-M等效于-A.

这些选项的工作方式和 CLI 的整体行为发生了相当多的变化,直到大约一个月后 1.10.1.727(2020 年 10 月 21 日)才稳定下来。在此期间,-A运行选项的行为:main-opts实际上已被弃用:如果您现在使用-A运行:main-opts,您将收到一个警告,您应该-M改用它。

几个基于 Clojure CLI 的社区工具,deps.edn在他们的 README 中有说明,你需要至少使用 1.10.1.727 版本才能利用它们的功能。版本 1.10.3.814 是当前版本(截至 2021 年 3 月 16 日)。随着 CLI 添加新功能(并且可能很快会进行另一轮更改),保持最新状态是值得的。

所有这些的 TL; DR 是:

  • 使用-M运行clojure.main任何:main-opts-这包括-m识别命名空间,其-main功能应该被执行,并且还-e计算表达式。请注意,这-main是一个使用零个或多个String参数调用的可变参数函数。
  • 使用-X运行特定的功能,以一个单一的散列映射作为其参数,经由命令行中的键/值对传递,除了:exec-argsdeps.edn。请注意,-X接受 EDN 值,这意味着需要在命令行上仔细引用字符串:'"A string"'-- 双引号用于 EDN 字符串,单引号用于确保值按原样通过 shell 传递。在 Windows 上,通过cmd.exe或 Powershell,引用比这更复杂(在 Linux 和 macOS 上使用 Clojure CLI 要容易得多,因此对于 Windows,值得考虑 WSL2)。
  • 使用-A启动REPL。现在,这意味着您需要小心混合:main-opts启动 REPL 时使用的别名(因为它-A仍在运行,:main-opts但在某些时候它会停止这样做)。

现在另一个有用的选项是-P-- prepare -- 您可以在clojure(before -X, -M, or -A)之后立即添加它,它让 CLI 计算和解析deps.edn文件和提供的别名中的所有依赖项(并下载 Maven/Clojars JAR 和clone git deps),但在实际执行函数之前停止(特定函数 for -Xclojure.mainfor-M或 REPL for -A)。

关于版本的最后说明:尽管 CLI 版本号以 Clojure 版本号为前缀,但它们并没有直接联系。您可以使用任何版本的 CLI 运行任何版本的 Clojure(回到 1.0.0)。对于CLI的版本号是x.y.z.commits这里x.y.z默认Clojure的版本,如果你不通过别名或通过项目的覆盖它,你会得到deps.edn的文件。


Ala*_*son 3

文字--help澄清了一些事情:

> clojure --help
Version: 1.10.3.814

You use the Clojure tools ('clj' or 'clojure') to run Clojure programs
on the JVM, e.g. to start a REPL or invoke a specific function with data.
The Clojure tools will configure the JVM process by defining a classpath
(of desired libraries), an execution environment (JVM options) and
specifying a main class and args. 

Using a deps.edn file (or files), you tell Clojure where your source code
resides and what libraries you need. Clojure will then calculate the full
set of required libraries and a classpath, caching expensive parts of this
process for better performance.

The internal steps of the Clojure tools, as well as the Clojure functions
you intend to run, are parameterized by data structures, often maps. Shell
command lines are not optimized for passing nested data, so instead you
will put the data structures in your deps.edn file and refer to them on the
command line via 'aliases' - keywords that name data structures.

'clj' and 'clojure' differ in that 'clj' has extra support for use as a REPL
in a terminal, and should be preferred unless you don't want that support,
then use 'clojure'.

Usage:
  Start a REPL   clj     [clj-opt*] [-Aaliases] [init-opt*]
  Exec function  clojure [clj-opt*] -X[aliases] [a/fn] [kpath v]*
  Run main       clojure [clj-opt*] -M[aliases] [init-opt*] [main-opt] [arg*]
  Prepare        clojure [clj-opt*] -P [other exec opts]

exec-opts:
 -Aaliases      Use concatenated aliases to modify classpath
 -X[aliases]    Use concatenated aliases to modify classpath or supply exec fn/args
 -M[aliases]    Use concatenated aliases to modify classpath or supply main opts
 -P             Prepare deps - download libs, cache classpath, but don't exec

clj-opts:
 -Jopt          Pass opt through in java_opts, ex: -J-Xmx512m
 -Sdeps EDN     Deps data to use as the last deps file to be merged
 -Spath         Compute classpath and echo to stdout only
 -Spom          Generate (or update) pom.xml with deps and paths
 -Stree         Print dependency tree
 -Scp CP        Do NOT compute or cache classpath, use this one instead
 -Srepro        Ignore the ~/.clojure/deps.edn config file
 -Sforce        Force recomputation of the classpath (don't use the cache)
 -Sverbose      Print important path info to console
 -Sdescribe     Print environment and command parsing info as data
 -Sthreads      Set specific number of download threads
 -Strace        Write a trace.edn file that traces deps expansion
 --             Stop parsing dep options and pass remaining arguments to clojure.main
 --version      Print the version to stdout and exit
 -version       Print the version to stderr and exit

init-opt:
 -i, --init path     Load a file or resource
 -e, --eval string   Eval exprs in string; print non-nil values
 --report target     Report uncaught exception to "file" (default), "stderr", or "none"

main-opt:
 -m, --main ns-name  Call the -main function from namespace w/args
 -r, --repl          Run a repl
 path                Run a script from a file or resource
 -                   Run a script from standard input
 -h, -?, --help      Print this help message and exit

Programs provided by :deps alias:
 -X:deps mvn-install       Install a maven jar to the local repository cache
 -X:deps git-resolve-tags  Resolve git coord tags to shas and update deps.edn

For more info, see:
 https://clojure.org/guides/deps_and_cli
 https://clojure.org/reference/repl_and_main
Run Code Online (Sandbox Code Playgroud)

因此-A(“别名”)是一个包罗万象的东西,可以合法替代任何更专业的标志。我通常使用-A这样我就不必记住其他哪些有什么用途。


您还应该在 Deps/CLI 上看到本指南

  • 版本 1.10.1.561 太旧了,根本与这个问题无关。那里的帮助甚至没有提到-X。 (2认同)