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.
Related
Lesson 13: Testing Your Larzscript Codeassert_eq, assert, and report - what's next after building a CLI tool.→Lesson 11: Gas-Metered Functionsgas N and --ledger - what's next after paywalls and subscriptions.→Lesson 7: Modules & the Package Ecosystemimport, alias, and call through it - exactly how args and cli get pulled in here.→Build a CLI ToolThe cookbook recipe this lesson's second example is drawn from.→Parse Command-Line ArgumentsThe cookbook recipe this lesson's first example is drawn from.→