Accept Input from Command Line in Bnlang
In Bnlang, you can accept input from the command line using the built‑in process.argv
array.
It contains all arguments passed when running the program. The first two elements are the runtime and script path, and the rest are user inputs.
Example: Reading Arguments
// file: greet.bnl
const args = process.argv.slice(2);
const name = args[0] || "Guest";
console.log("Hello,", name);
bnl greet.bnl Alice
# Output: Hello, Alice
Example: Accept Multiple Inputs
// file: sum.bnl
const args = process.argv.slice(2);
const a = Number(args[0]);
const b = Number(args[1]);
console.log("Sum:", a + b);
bnl sum.bnl 5 7
# Output: Sum: 12
Best Practices
- Always validate and sanitize input values.
- Provide default values to handle missing arguments.
- Use libraries for advanced parsing (e.g., options, flags).
- Print usage help when invalid inputs are given.