Building a Small CLI App
Keep parsing and I/O at the edge, with a pure core that is easy to test.
Argparse
std/argparse provides a Cli record plus flag, option, parse, and accessor helpers.
argparse.brp
cli: Cli = { name = "app", version = "0.1", description = "demo", params = [] }Pure Core
Put deterministic word-count logic in pure functions.
core.brp
pure func count_words(text: String) -> Int:
text.words().length()Impure Shell
Read files, parse args, and print in main.
shell.brp
func main(args: List[String]) -> Int:
print(count_words("one two").to_string())
0Tests
Test the pure core without invoking the CLI.
test.brp
func test_count_words() -> Bool:
count_words("one two") == 2Example
wordcount.brp
import:
argparse:
Cli,
ParseOutcome(Parsed, HelpRequested, VersionRequested, ParseFailed),
format_error,
get_int_or,
option_int,
parse,
pure func count_words(text: String, min_len: Int) -> Int:
var count: Int = 0
for word in text.words():
if word.length() >= min_len:
count += 1
count
func main(args: List[String]) -> Int:
cli: Cli = {
name = "wordcount",
version = "0.1",
description = "count words",
params = [option_int("min-length", "--min-length", "-m", "minimum length", "N")],
}
match parse(cli, args):
Parsed(parsed):
min_len: Int = get_int_or(parsed, "min-length", 1)
print(count_words("one three seven", min_len).to_string())
0
ParseFailed(err):
eprintln(format_error(err))
1
HelpRequested(text):
print(text)
0
VersionRequested(text):
print(text)
0
Try It
terminal
blorp run wordcount.brp -- --min-length 4