Lean 4 (Meta)programming Cookbook

Simple Argument Parsing🔗

For many tools, you just need to check for specific flags or a single input file. Pattern matching on the list of strings is the most idiomatic way to do this.

def parseArgs (args : List String) : IO Unit := do match args with | [] | ["--help"] | ["-h"] => IO.println "Usage: mytool [OPTIONS] [FILE]\n" IO.println "Options:" IO.println " -h, --help Show this help" IO.println " -v, --version Show version" | ["--version"] | ["-v"] => IO.println "mytool version 1.0.0" | [filename] => IO.println s!"Processing file: {filename}" | _ => IO.eprintln "Error: Unknown or too many arguments. Use --help for usage." IO.Process.exit 1