Accept Input from Command Line in Bnlang
There are two layers for reading command-line arguments in Bnlang:
sys.arg(i)— the raw, low-level accessor. Good for one or two positional inputs.climodule — a declarative parser for flags, options, and positional arguments. Good when you have anything more structured.
Raw Access with sys.arg
sys.argc() returns how many arguments the script received; sys.arg(i) returns the i-th one (0-indexed). The script path itself is not counted.
// file: greet.bnl
import "sys" as sys;
var name = "Guest";
if (sys.argc() > 0) { name = sys.arg(0); }
print("Hello,", name);
bnl greet.bnl Alice
# Output: Hello, Alice
Structured Parsing with the cli Library
For anything past one or two positional inputs, declare your CLI shape with the cli library. You get flags, options-with-values, positional arguments, and an auto-generated --help string.
// file: greet.bnl
import "cli" as cli;
var p = cli.parser("greet");
p.description = "Print a greeting.";
cli.flag (p, {name: "shout", short: "s", help: "Use upper case"});
cli.option (p, {name: "lang", short: "l", help: "en or bn", default: "en"});
cli.positional(p, {name: "name", help: "Who to greet"});
var args = cli.parse(p);
var msg = "Hello, ";
if (args.lang == "bn") { msg = "হ্যালো, "; }
msg = msg + args.name + "!";
if (args.shout) { msg = msg.to_upper(); }
print(msg);
bnl greet.bnl Alice
# Hello, Alice!
bnl greet.bnl -l bn আলিস
# হ্যালো, আলিস!
bnl greet.bnl -s Alice
# HELLO, ALICE!
Showing Help
cli.usage(p) returns the auto-generated help text. Define your own --help flag and check it after parsing:
cli.flag(p, {name: "help", short: "h", help: "Show usage"});
var args = cli.parse(p);
if (args.help) {
print(cli.usage(p));
return;
}
Best Practices
- Reach for
clias soon as you have more than two inputs or any optional flags. It's much easier to maintain than handwritten parsing. - Provide sensible defaults so the script does something useful with no arguments.
- Print a usage hint and exit with a non-zero code on bad input.