Larzscript Course · Lesson 12 of 15

Building a Real CLI Tool

Larz builds a real command-line tool: args parses raw arguments into positionals and flags, and cli names a tool, adds a subcommand, and runs it.

parsing arguments

args.parse splits raw arguments into positionals and flags, straight from the command line:

import "args" as args_

let p = args_.parse(["build", "--target=release", "--verbose"])
print(p["positional"])
print(p["flags"]["target"])
print(args_.flag(p, "verbose", false))
Output
[build]
release
true

a real command

cli.new names your tool, cli.command adds a subcommand, and cli.run picks the matching command from the arguments and calls it:

import "cli" as cli

let app = cli.new("greet", "a tiny greeter")
cli.command(app, "hello", "say hello: hello NAME", fn(a) {
  print("hello, " + a["positional"][0] + "!")
})
cli.run(app, ["hello", "Ada"])
Output
hello, Ada!

Every snippet on this page is run through the real larzscript interpreter, not hand-typed โ€” what you see in the Output blocks is the actual output, for everything shown: parsing real arguments and running a real subcommand.

← Lesson 11Course syllabusGitHub