TorchLean API

NN.API.CLI.Command

Command Process Boundaries #

Helpers for reporting pure parser failures through an executable's IO boundary.

def TorchLean.CLI.orThrow {α : Type} (exeName : String) :
Except String αIO α

Lift a parser result into IO, prefixing failures with the executable name.

Several of the shared parsers take exeName themselves and have already named the program in their message, takePositiveNatFlag among them. Prefixing again gives quickstart_mlp: quickstart_mlp: --steps must be > 0, so the name is only added when it is not there yet. Deciding that here rather than at every call site is what lets one parse function mix the two parser styles without the caller having to remember which is which.

Instances For
    def TorchLean.CLI.seed (exeName : String) (arguments : List String) (default : Nat := 0) :

    Parse --seed N, returning the selected seed and remaining arguments.

    Instances For
      def TorchLean.CLI.positiveNatFlag (exeName : String) (arguments : List String) (name : String) (default : Nat) :

      Parse a positive natural-number flag, using default when it is absent.

      Instances For
        def TorchLean.CLI.requireNoArgs (exeName : String) (arguments : List String) :

        Fail when command-specific arguments remain after parsing.

        Instances For

          Run an executable's entry point and report a failure the way a command-line tool should.

          Every parser above refuses bad input by throwing an IO error, which is the right thing for the parser to do. Left alone, that error escapes main and the Lean runtime prints it as uncaught exception: quickstart_mlp: --steps must be > 0, which reads like a crash in the middle of a run even though the program rejected the command line on purpose. This wrapper catches the error at the last possible moment, prints it as error: … on stderr, and returns exit status 1. The status is what the runtime would have produced anyway; only the wording changes.

          Instances For