【问题标题】:How to pass command line arguments to Deno?如何将命令行参数传递给 Deno?
【发布时间】:2020-09-02 01:41:07
【问题描述】:
我有一个Deno 应用程序,我希望将一些命令行参数传递给它。我搜索了manual,但一无所获。
我尝试使用 Node.js 中使用的相同命令,假设它们可能会为 std 库共享一些命令,但效果不佳。
var args = process.argv.slice(2);
// Uncaught ReferenceError: process is not defined
有什么建议吗?
【问题讨论】:
标签:
command-line-arguments
deno
cmdline-args
【解决方案1】:
您可以使用Deno.args 访问参数,它将包含传递给该脚本的参数数组。
// deno run args.js one two three
console.log(Deno.args); // ['one, 'two', 'three']
如果你想解析这些参数,你可以使用std/flags,它会解析类似于minimist的参数
import { parse } from "https://deno.land/std/flags/mod.ts";
console.log(parse(Deno.args))
如果你调用它:
deno run args.js -h 1 -w on
你会得到
{ _: [], h: 1, w: "on" }
【解决方案3】:
您可以使用Deno.args 访问 Deno 中的命令行参数。
要尝试它,请创建一个文件 test.ts :
console.log(Deno.args);
然后使用deno run test.ts firstArgument secondArgument 运行它
它会返回一个传入参数的数组:
$ deno run test.ts firstArgument secondArgument
[ "firstArgument", "secondArgument" ]