Beginner
cli
Command Line Arguments
Command line arguments are often used to pass configuration options to a program.
You can get the list of command line arguments from `Deno.args`.
!--frsh-copybutton:1-->
const name = Deno.args[0];
const food = Deno.args[1];
console.log(`Hello ${name}, I like ${food}!`);
Often you want to parse command line arguments like `--foo=bar` into structured data. This can be done using `std/flags`.
!--frsh-copybutton:2-->
import { parse } from "https://deno.land/std@0.175.0/flags/mod.ts";
The `parse` function takes the argument list, and a list of options. In these options you specify the types of the accepted arguments and possibly default values. An object is returned with the parsed arguments.
!--frsh-copybutton:3-->
const flags = parse(Deno.args, {
boolean: ["help", "color"],
string: ["version"],
default: { color: true },
});
console.log("Wants help?", flags.help);
console.log("Version:", flags.version);
console.log("Wants color?:", flags.color);
The `_` field of the returned object contains all arguments that were not recognized as flags.
!--frsh-copybutton:4-->
console.log("Other:", flags._);
Run this example locally using the Deno CLI:
deno run https://byexample-wwv03xf36j0g.deno.dev/command-line-arguments.ts Deno Sushi --help --version=1.0.0 --no-color
Additional resources: