~/bend-docscommunity

main.bend checks

raw source on the hub · import 0x085b03c84ca37125e38dddede7b91e55/main.bend as Main

shake: CLI argument parser for Bend 2. Describe a program with app, sub, flag, opt, many, pos and rest; parse binds argv against it, and the result is read one command at a time, as clap's: get, get_all and on read a command's own bindings, and sub_name, sub_of, at and path_of reach the subcommands selected under it. help writes the usage page for a command path. For a failed parse, help_path tells a request for help from an error, err_text writes the message and err_path names the command it failed in. check reports a spec that contradicts itself. argv is the process's arguments. This file is the interface: everything under src/ is internal.

4 imports
import Base
import ./src/cli.bend as S
import ./src/args.bend as Args
import ./src/check.bend as K

Definitions

def Cli source · line 17 · raw

Data

a program spec

def Sub source · line 21 · raw

Data

a nested command

def Arg source · line 25 · raw

Data

one argument: a flag, an option, a positional or a rest positional

def Matched source · line 30 · raw

Data

a successful parse, one command at a time: the bindings the command made, and the subcommand selected under it with its own Matched

def ParseErr source · line 34 · raw

Data

a failed parse

def SpecErr source · line 38 · raw

Data

one way a spec contradicts itself

def app source · line 42 · raw

@+name:String -> @+about:String -> @version:Maybe<&2, String> -> @args:List<&2, 0x085b03c84ca37125e38dddede7b91e55/src/cli.Arg> -> @subs:List<&2, 0x085b03c84ca37125e38dddede7b91e55/src/cli.Sub> -> 0x085b03c84ca37125e38dddede7b91e55/src/cli.Cli

a program: name, about, optional version, top-level args and commands

def sub source · line 52 · raw

@+name:String -> @+about:String -> @args:List<&2, 0x085b03c84ca37125e38dddede7b91e55/src/cli.Arg> -> @subs:List<&2, 0x085b03c84ca37125e38dddede7b91e55/src/cli.Sub> -> 0x085b03c84ca37125e38dddede7b91e55/src/cli.Sub

a nested command

def flag source · line 61 · raw

@+name:String -> @short:Maybe<&2, String> -> @long:Maybe<&2, String> -> @+help:String -> 0x085b03c84ca37125e38dddede7b91e55/src/cli.Arg

a boolean flag

def opt source · line 70 · raw

@+name:String -> @short:Maybe<&2, String> -> @long:Maybe<&2, String> -> @+help:String -> @required:Bool -> @default:Maybe<&2, String> -> @choices:List<&2, String> -> 0x085b03c84ca37125e38dddede7b91e55/src/cli.Arg

a valued option

def many source · line 83 · raw

@+name:String -> @short:Maybe<&2, String> -> @long:Maybe<&2, String> -> @+help:String -> @required:Bool -> @default:Maybe<&2, String> -> @choices:List<&2, String> -> 0x085b03c84ca37125e38dddede7b91e55/src/cli.Arg

an option that may be given more than once; get_all reads every value, in order, and get the last

def pos source · line 95 · raw

@+name:String -> @+help:String -> @required:Bool -> @default:Maybe<&2, String> -> @choices:List<&2, String> -> 0x085b03c84ca37125e38dddede7b91e55/src/cli.Arg

a positional

def rest source · line 105 · raw

@+name:String -> @+help:String -> @required:Bool -> @default:Maybe<&2, String> -> @choices:List<&2, String> -> 0x085b03c84ca37125e38dddede7b91e55/src/cli.Arg

a rest positional: every leftover word, as one name

def check source · line 119 · raw

@spec:0x085b03c84ca37125e38dddede7b91e55/src/cli.Cli -> List<&2, 0x085b03c84ca37125e38dddede7b91e55/src/check.SpecErr>

every way a spec contradicts itself: a rest positional that is not the last, a repeated argument name or spelling, a repeated subcommand name, a subcommand named help, a default outside its choices, a required positional after an optional one. Empty for a well-formed spec, which is what SPEC.md's parse guarantees are about; parse does not call it

def spec_err_text source · line 123 · raw

@err:0x085b03c84ca37125e38dddede7b91e55/src/check.SpecErr -> String

a report of check as one line

def parse source · line 127 · raw

@spec:0x085b03c84ca37125e38dddede7b91e55/src/cli.Cli -> @words:List<&2, String> -> Result<&2, &2, 0x085b03c84ca37125e38dddede7b91e55/src/cli.ParseErr, 0x085b03c84ca37125e38dddede7b91e55/src/cli.Matched>

argv against a program spec

def get source · line 131 · raw

@found:0x085b03c84ca37125e38dddede7b91e55/src/cli.Matched -> @+name:String -> Maybe<&2, String>

the value this command bound to name last, or None when it bound none

def get_all source · line 135 · raw

@found:0x085b03c84ca37125e38dddede7b91e55/src/cli.Matched -> @+name:String -> List<&2, String>

every value this command bound to name, in argv order

def on source · line 139 · raw

@found:0x085b03c84ca37125e38dddede7b91e55/src/cli.Matched -> @+name:String -> Bool

whether this command's flag named name was set

def path_of source · line 144 · raw

@found:0x085b03c84ca37125e38dddede7b91e55/src/cli.Matched -> List<&2, String>

the names of the subcommands selected below this command, in order: the whole selected path for the Matched parse answers

def sub_name source · line 148 · raw

@found:0x085b03c84ca37125e38dddede7b91e55/src/cli.Matched -> Maybe<&2, String>

the name of the subcommand selected under this command, or None

def sub_of source · line 152 · raw

@found:0x085b03c84ca37125e38dddede7b91e55/src/cli.Matched -> @+name:String -> Maybe<&2, 0x085b03c84ca37125e38dddede7b91e55/src/cli.Matched>

the Matched of subcommand name when it is the one selected, else None

def at source · line 157 · raw

@found:0x085b03c84ca37125e38dddede7b91e55/src/cli.Matched -> @path:List<&2, String> -> Maybe<&2, 0x085b03c84ca37125e38dddede7b91e55/src/cli.Matched>

the Matched of the command path names below this one, following sub_of name by name, or None where a name is not the one selected

def help source · line 162 · raw

@+spec:0x085b03c84ca37125e38dddede7b91e55/src/cli.Cli -> @path:List<&2, String> -> String

the usage page for path under spec; an unknown name in the path is skipped

def err_text source · line 167 · raw

@+spec:0x085b03c84ca37125e38dddede7b91e55/src/cli.Cli -> @err:0x085b03c84ca37125e38dddede7b91e55/src/cli.ParseErr -> String

the message for a failed parse, ending with the root usage line; empty for a request for help, which the caller answers with help

def err_path source · line 172 · raw

@err:0x085b03c84ca37125e38dddede7b91e55/src/cli.ParseErr -> List<&2, String>

the command path where the parse failed: the path selected when it did, or for a request for help the path it asks about

def help_path source · line 191 · raw

@err:0x085b03c84ca37125e38dddede7b91e55/src/cli.ParseErr -> Maybe<&2, List<&2, String>>

the command path of a request for help (help, help <path>), or None when the parse failed for another reason

def argv source · line 212 · raw

IO(List<&2, String>)

the process's arguments, each word reusable. A compiled program's runtime has already acted on --help, --gpu-build, --threads N and --gpu X and taken the first --, passing every word after it here unexamined; so the -- that parse sees is the user's second one