src/args.bend

shake@0.4.0.0 · 26 lines · 0 definitions

1# shake/args: the process's arguments, each word reusable, internal (main.bend
2# exports it). `IO.args` answers a `&1` list that starts with the program as
3# invoked (bend 2.0.32 and later); the rest of a program reads the line more
4# than once, so `words` drops that head and copies the rest.
5import Base
6
7# a &1 list copied to &2
8def copy(xs: List<&1, String>) -> List<&2, String>:
9  match xs:
10    case Nil{}:
11      Nil{}
12    case Con{+h, t}:
13      h <> copy(t)
14
15# the arguments of an IO.args line: every word after the program name, copied
16def words(xs: List<&1, String>) -> List<&2, String>:
17  match xs:
18    case Nil{}:
19      Nil{}
20    case Con{_h, t}:
21      copy(t)
22
23# the process's arguments, without the program name
24def argv() -> IO(List<&2, String>):  # noqa: L001 IO: reads argv (SHAKE-TRUST-4)
25  IO.bind(List<&1, String>, List<&2, String>, IO.args(),
26    xs => IO.pure(List<&2, String>, words(xs)))