~/bend-docscommunity

src/args.bend source

src/args.bend on the hub · documented module

# src/args: bolt's command line, parsed by shake, in ruff's shape:# `bolt check [file..]`, `bolt format [--check] [file..]`, `bolt lsp`,# `bolt --version`. A rest positional on `check` and `format` takes every# leftover word. The root takes no files: `bolt` with no command prints the# usage to stderr and exits 2, as ruff does, so a lint step that forgot its# command fails instead of passing. `help` is shake's usage page, and `--help` asks for the same# page (`line`). IO.args gives the program as invoked first, then its# arguments (bend 2.0.32); `argv` drops the program. A compiled binary passes# its whole line through except its runtime options, which stand between# `+RTS` and `-RTS` (bend 2.0.38): `--threads` and `--gpu` outside that# bracket reach the program, and `--RTS` hands everything after it to the# program. `--help` reaches the program (bend 2.0.29).# `out` is every decision the line makes, as data: what to print, where, and# the exit code, or the subcommand to run and its files. main only does it.# Only shake's interface (main.bend) is read here; what `parse` answers for a# given argv is shake's guarantee, not bolt's: shake v0.6.0, pinned by hash,# proves it in its own gate, and the laws naming an argv take shake's answer as# a premise and prove what bolt does with it. Of shake's assumptions bolt# trusts one: the words a compiled bolt's argv holds are the runtime's (its# options, between `+RTS` and `-RTS`, never reach it), which no law reaches.import Baseimport shake@0.9.0.0/main.bend as Shakeimport ./version.bend as Verimport ./build_rev.bend as Revimport ./lazy/lazy.bend as Lazy# the line at the quantity the rest of bolt reads it at (IO.args gives each# argument once)def widen(ss: List<&1, String>) -> List<&2, String>:  match ss:    case Nil{}:      Nil{}    case Con{s, t}:      s <> widen(t)# the arguments of IO.args's line: the program as invoked comes first, and is# droppeddef args_of(s: List<&1, String>) -> List<&2, String>:  match s:    case Nil{}:      Nil{}    case Con{_prog, t}:      widen(t)# bolt's command line: its arguments, without the programdef argv() -> IO(List<&2, String>):  # noqa: L001 IO: reads argv  do IO<List<&2, String>>:    s : List<&1, String> <- IO.args()    return args_of(s)# whether a word names a subcommanddef is_cmd(+w: String) -> Bool:  Bool.or(String.eq(w, "check"), Bool.or(String.eq(w, "format"), String.eq(w, "lsp")))# whether `--help` stands among the words before the first `--` (after it,# every word is a file); the search stops at eitherdef asks_help(ws: List<&2, String>) -> Bool:  match ws:    case Nil{}:      False{}    case Con{+w, rest}:      Lazy.and_then(Bool.not(String.eq(w, "--")), _u => Lazy.or_else(String.eq(w, "--help"), _v => asks_help(rest)))# the command path `--help` asks about: the first word other than `--help`,# when it names a subcommand, else the root'sdef help_path(ws: List<&2, String>) -> List<&2, String>:  match ws:    case Nil{}:      Nil{}    case Con{+w, rest}:      Lazy.either(List<&2, String>, String.eq(w, "--help"), _u => help_path(rest),        _u => Bool.pick(List<&2, String>, is_cmd(w), [w], []))# the line, given whether it asks for `--help`def line.at(ask: Bool, +ws: List<&2, String>) -> List<&2, String>:  match ask:    case True{}:      "help" <> help_path(ws)    case False{}:      ws# the line shake parses: one that asks for `--help` is `help` for its command# path, the page `bolt help` prints; any other is itselfdef line(+ws: List<&2, String>) -> List<&2, String>:  line.at(asks_help(ws), ws)# the files rest, optional, last among positionalsdef spec.files(+help: String) -> Shake.Arg:  Shake.rest("files", help, False{}, None{}, [])# the release, with the short commit in parentheses when it is presentdef version.at(+rel: String, +rev: String) -> String:  Bool.pick(String, String.is_empty(rev), rel, rel ++ " (" ++ rev ++ ")")# the release, and the short commit when the build has onedef version() -> String:  version.at(Ver.text(), Rev.text())# bolt's program specdef spec() -> Shake.Cli:  Shake.app("bolt", "A linter, checker and language server for Bend 2.", Some{version()},    [Shake.flag("version", None{}, Some{"version"}, "Print the version")],    [Shake.sub("check", "Lint files", [spec.files("Files to lint")], []),     Shake.sub("format", "Format files",       [Shake.flag("check", None{}, Some{"check"}, "List the files that would change and write nothing"),        spec.files("Files to format")], []),     Shake.sub("lsp", "Serve an editor over stdio", [], [])])# argv against bolt's specdef parse(av: List<&2, String>) -> Result<&2, &2, Shake.ParseErr, Shake.Matched>:  Shake.parse(spec(), av)# usage for a command pathdef help_text(path: List<&2, String>) -> String:  Shake.help(spec(), path)# a parse error as the text the binary printsdef err_text(e: Shake.ParseErr) -> String:  Shake.err_text(spec(), e)# a subcommand; format carries whether it only checkstype Cmd is Data:  CCheck{}  CLsp{}  CFormat{check: Bool}# a subcommand by its name: `lsp`, or else `check`def command(+name: String) -> Cmd:  Bool.pick(Cmd, String.eq(name, "lsp"), CLsp{}, CCheck{})# a subcommand by its name and whether `--check` was set: `format` takes the# flag, every other name is as `command` reads itdef command.at(+name: String, check: Bool) -> Cmd:  Bool.pick(Cmd, String.eq(name, "format"), CFormat{check}, command(name))# where a line that stops puts its texttype Stream is Data:  Line{}  Page{}  Err{}# what a parsed line does: print a text and exit with a code, or run a# subcommand over files (which then exits as that command decides)type Out is Data:  Stop{to: Stream, text: String, code: U32}  Go{cmd: Cmd, paths: List<&2, String>}# the files the selected command bound: each command keeps its own# bindings, so they are read from the Matched at the end of the selected# path, which is the root's own when no subcommand was selecteddef files.at(sel: Maybe<&2, Shake.Matched>, +m: Shake.Matched) -> List<&2, String>:  match sel:    case Some{s}:      Shake.get_all(s, "files")    case None{}:      Shake.get_all(m, "files")# the files a successful parse bound, to the command it selecteddef files(+m: Shake.Matched) -> List<&2, String>:  files.at(Shake.at(m, Shake.path_of(m)), m)# whether the selected command was given `--check`: read from its own# Matched, as its files are; no command selected has no such flagdef checks.at(sel: Maybe<&2, Shake.Matched>) -> Bool:  match sel:    case Some{s}:      Shake.on(s, "check")    case None{}:      False{}# whether a successful parse set `--check` on the command it selecteddef checks(+m: Shake.Matched) -> Bool:  checks.at(Shake.at(m, Shake.path_of(m)))# a successful parse given the command path it selected: the usage on# stderr and exit 2 when it selected none, else that command over its filesdef out.go(path: List<&2, String>, +m: Shake.Matched) -> Out:  match path:    case Nil{}:      Stop{Err{}, help_text([]), 2}    case Con{c, cs}:      Go{command.at(String.join(c <> cs, "/"), checks(m)), files(m)}# a successful parse: the version, or what its command path doesdef out.run(ask: Bool, +m: Shake.Matched) -> Out:  match ask:    case True{}:      Stop{Line{}, "bolt " ++ version(), 0}    case False{}:      out.go(Shake.path_of(m), m)# a failed parse, given whether it asked for usage: that usage page, or the# errordef out.fail.at(asked: Maybe<&2, List<&2, String>>, +e: Shake.ParseErr) -> Out:  match asked:    case Some{path}:      Stop{Page{}, help_text(path), 0}    case None{}:      Stop{Err{}, err_text(e), 1}# a failed parse: help text, or an errordef out.fail(+e: Shake.ParseErr) -> Out:  out.fail.at(Shake.help_path(e), e)# a successful parse, read once for the version flagdef out.done(+m: Shake.Matched) -> Out:  out.run(Shake.on(m, "version"), m)# what a parse result doesdef out(r: Result<&2, &2, Shake.ParseErr, Shake.Matched>) -> Out:  match r:    case Fail{e}:      out.fail(e)    case Done{m}:      out.done(m)