[{"data":1,"prerenderedAt":4},["ShallowReactive",2],{"JyqsqFlxuM":3},"# lean-argparse\n\n`lean-argparse` provides an applicative command line argument parser for Lean 4, inspired by Haskell's [`optparse-applicative`](https://hackage.haskell.org/package/optparse-applicative). In addition to the core parser, the toolkit ships usage rendering, shell completion generators, man page output, and a lint driver for keeping documentation in sync.\n\n## Highlights\n\n- Applicative combinators (`Parser`, `\u003C*>`, `map`) with built-in `Functor`/`Applicative` instances\n- Primitive parsers for options, flags, switches, positional arguments, and subcommands\n- Usage metadata with automatic help text rendering (including a default `-h/--help` entry)\n- Structured error reporting (missing arguments, invalid values, unexpected leftovers)\n- Convenience value readers plus builder-style modifiers (`OptionSpec`, `FlagSpec`, `ParserInfo`)\n- Higher-level combinators such as `many`, `some`, `choice`, `flag'`, and ready-made option helpers (`strOption`, `natOption`, …)\n- Lightweight tests expressed via `#guard`\n- Built-in helpers for shell completions, man page generation, and a `lake lint` driver\n\n## Example\n\nThe executable bundled with the project demonstrates a small CLI.\n\n```lean\nimport Argparse\n\nopen Argparse\n\nstructure Config where\n  verbose : Bool\n  count : Nat\n  name : String\n\ndef configParser : Parser Config :=\n  pure Config.mk\n    \u003C*> switch \"verbose\" (short? := some 'v') (help? := some \"Enable verbose output\")\n    \u003C*> Parser.withDefault\n          (natOption [\n            OptionSpec.long \"count\",\n            OptionSpec.short 'n',\n            OptionSpec.setMetavar \"COUNT\",\n            OptionSpec.help \"How many times to greet\",\n            OptionSpec.showDefault \"1\"\n          ])\n          1\n    \u003C*> rawArgument \"NAME\" (help? := some \"Name to greet\")\n\ndef info : ParserInfo Config :=\n  ParserInfo.build configParser [\n    ParserInfo.withProgName \"lean-argparse\",\n    ParserInfo.withProgDesc \"Demonstrates the Lean applicative argument parser\"\n  ]\n\ndef main (args : List String) : IO Unit :=\n  match Argparse.ParserInfo.exec info args with\n  | .success cfg => IO.println s!\"Hello, {cfg.name}! (count := {cfg.count}, verbose := {cfg.verbose})\"\n  | .showHelp => IO.println (Argparse.ParserInfo.renderHelp info)\n  | .failure err => do\n      IO.eprintln (Argparse.ParserInfo.renderFailure info err)\n      IO.Process.exit 1\n\ndef bashCompletionScript : String :=\n  Argparse.ParserInfo.renderBashCompletion info\n\ndef zshCompletionScript : String :=\n  Argparse.ParserInfo.renderZshCompletion info\n\ndef fishCompletionScript : String :=\n  Argparse.ParserInfo.renderFishCompletion info\n```\n\nRunning the compiled executable yields:\n\n```\n$ lake build\n$ ./build/bin/lean-argparse Alice\nHello, Alice!\n\n$ ./build/bin/lean-argparse --count 3 -v Bob\nHello, Bob! (verbose)\nHello, Bob! (verbose)\nHello, Bob! (verbose)\n\n$ ./build/bin/lean-argparse --help\nLean argparse example\nUsage: lean-argparse [--verbose] [--count COUNT] NAME\n...\n```\n\n## Development\n\n- Build the library and example executable:\n  ```sh\n  lake build\n  ```\n\n- Run the lightweight unit tests:\n  ```sh\n  lake test\n  ```\n\nTests are located under `Tests/` and use `#guard` checks; compiling the test executable verifies the expectations without shipping test modules with the library.\n\n- Run the project-wide lint driver:\n  ```sh\n  lake lint\n  ```\n\nThe lint driver re-elaborates every module to ensure public declarations carry docstrings and standard linters stay satisfied.\n\n- Generate HTML documentation (after running `lake update doc-gen4` once inside `docbuild/`):\n  ```sh\n  cd docbuild\n  DOCGEN_SRC=file lake build Argparse:docs\n  ```\n\n## Shell Completions\n\nUse the renderer that matches your shell to produce a completion script:\n\n```lean\n#eval IO.println (Argparse.ParserInfo.renderBashCompletion info)\n#eval IO.println (Argparse.ParserInfo.renderZshCompletion info)\n#eval IO.println (Argparse.ParserInfo.renderFishCompletion info)\n```\n\nRedirect the output to a file and source it (or copy it into the appropriate completion directory) to enable tab completion for options and subcommands.\n\n## Man Pages\n\nGenerate a basic troff man page directly from your parser metadata:\n\n```lean\n#eval IO.println (Argparse.ParserInfo.renderManpage info)\n\n-- With simple metadata overrides:\n#eval IO.println (Argparse.ParserInfo.renderManpage info { sectionName := \"1\", manual? := some \"Lean Tools\" })\n```\n\nPipe the output into `man -l` or write it to disk for installation alongside your executable.\n\n- `many`, `some`, `choice`, and `many1` allow repeated or alternative argument parsing without leaving the applicative world.\n- Builder helpers (e.g. `OptionSpec.long`, `FlagSpec.long`, `ParserInfo.withProgDesc`) make it easy to mirror the ergonomic modifiers from `optparse-applicative`.\n- Convenience wrappers (`strOption`, `natOption`, `intOption`, `flag'`) remove most manual record instantiations.\n- Shell completion generators (`renderBashCompletion`, `renderZshCompletion`, `renderFishCompletion`) produce scripts for popular shells.\n",1781732219450]