argv

argv(): Source<unknown>

Turn the command line of craft run <file> into one exchange. The route runs its full chain, the CLI prints the result and the process exits with the exit codes of craft exec, with one difference: an .authorize() refusal exits 1, not 4, because the door admits no principal.

import { argv, craft, direct } from "@routecraft/routecraft";
import { z } from "zod";

const WorkItemQuery = z.object({
  id: z.string().describe("The work item id"),
  expand: z.string().optional().describe("Relations to include"),
});

export default craft()
  .id("work-item")
  .title("Fetch a work item")
  .input({ body: WorkItemQuery })
  .from(argv(), direct())
  .transform((query) => ({ id: query.id }));
craft run work-item.ts --id=12345 --format=json
echo '{"id":"12345"}' | craft run work-item.ts
craft run work-item.ts --help

The same route keeps direct() beside argv(), so craft run and craft exec both reach it.

Input

The arguments after the file become the body with the grammar craft exec uses, through the same parser:

  • --field=value and --field value set a field.
  • A bare --flag is true.
  • A repeated key becomes an array.
  • Nothing is coerced: every value is the string the shell passed, except a bare flag. For a number or a nested value, declare a coercing schema in .input() (such as z.coerce.number()) or pipe JSON.

With no arguments, JSON piped on stdin is the body, read to its end as craft exec reads it; a pipe that never closes keeps the run waiting. With flags, stdin is not read at all, so the flags are the whole body. Stdin is read only by an argv() route that was given no flags, so a file without argv(), such as a stdio mcp() server, keeps its stdin under craft run. A file with argv() is one-shot under craft run: the CLI stops the context after the one exchange, so a stdio server that has to stay up belongs in a file of its own, or under craft start, where argv() is inert.

Several routes in one file

A file can hold several argv() routes. Name the one to run first, before its flags, as craft exec names its route:

craft run tools.ts work-item --id=12345
echo '{"id":"12345"}' | craft run tools.ts work-item
craft run tools.ts --help            # lists the argv() routes
craft run tools.ts work-item --help  # that route's input fields

With one argv() route the name is optional. With several it is required to run one: a missing name, or one no argv() route carries, is a usage error that lists the routes, and nothing starts. --help without a name is not an error; it lists the routes. One command line still runs one route; the other argv() routes stay inert for that run.

Reserved flags

Craft's own options go before the file. After it, only two flags belong to the CLI:

FlagMeaning
--helpPrint the route's id, title, description and the fields of its .input() body (type, whether required, description), or the list of argv() routes when the file has several and none is named. The context never starts, so no route in the file runs.
--format <format>pretty (default), json or raw, the same renderer as craft exec. The default comes from the settings file.

A route whose .input() body declares a field named help or format is refused with a usage error before anything starts, because those flags never reach it. The check and the --help fields read the schema's JSON Schema rendering; a schema library without one gets neither.

Flag values come from outside the route's code. Pass one to shell() wrapped in untrusted(), and keep secrets off the command line, where process listings and shell history keep them: pipe them as JSON on stdin instead.

No principal

A command line proves no identity, so argv() puts no principal on the exchange. A route without .authorize() runs. A route with .authorize() fails with RC5012 (exit code 1) unless a plugin supplies a principal on the way in: a mutate hook in the beforeAuth slot runs before the authorize position and can set one through authorityOf(). The door admitted no principal, so when .authorize() refuses the one a plugin supplied, for example for a missing scope, that is a route failure (exit code 1) and not a refusal of the caller. Token verification on argv() itself is not offered yet.

Scope that hook with sources, or it supplies the same principal to every other door on the route. With the selector, a route declared .from(argv(), direct()) runs as the operator from the command line and still refuses a direct() call that carries no principal:

import { authorityOf, definePlugin, HeadersKeys } from "@routecraft/routecraft";

export const cliOperator = definePlugin({
  id: "app.cli-operator",
  hooks: {
    beforeAuth: {
      id: "operator",
      phase: "mutate",
      sources: ["routecraft.adapter.argv"],
      run: (exchange) => ({
        headers: {
          [HeadersKeys.AUTH_PRINCIPAL]: authorityOf(exchange).mint({
            kind: "custom",
            scheme: "local",
            subject: process.env["USER"] ?? "operator",
            scopes: ["boards:read"],
          }),
        },
      }),
    },
  },
});

sources selects by the door an exchange came through and proves nothing about who is at it: anyone who can run craft run on the machine is that operator.

Output and exit codes

The final body goes to stdout. Logs go to stderr under craft run, so craft run work-item.ts --id=1 --format raw | jq reads only the result.

ExitWhen
0The route completed, or it deferred: the Deferred acknowledgement is printed with its deferral id and resume token. The token resumes only through a durable deferral store and a resume route that outlive the process
1The route failed, a filter dropped the exchange, or the route never read the command line (disabled, or it failed to start)
2A usage error: a malformed command line, a missing or unknown route name, stdin that is not JSON, a reserved field, or an .input() failure (the schema issues are printed)
4A plugin validate hook or the route's own .validate() step refused the caller, except an invalid refusal, which exits 2

After the one exchange the source completes and the CLI stops the context, so the process exits even when direct() or mcp() would hold the route open.

Under craft start

argv() is inert: craft start hands no command line to the context, so the source emits nothing and the route answers only through its other sources. Declare it beside direct() and mcp() without branching.

Runners other than the CLI

The CLI hands the command line over in the context store under RUNNER_INVOCATION before context.start(), as a RunnerInvocation: the id of the route to run, its arguments with the runner's own flags taken out, a lazy stdin reader, and a settle(outcome) callback that receives one RunnerOutcome. A runner of your own sets the same key to drive an argv() route. Before it starts the context it calls argvRouteIds(context) to find the routes and pick one, and describeArgvRoute(context, id) to answer its own help flag and check its reserved flag names against the route's input fields.


CLI

craft run, its options, and the settings file.

direct

The in-process door craft exec dispatches through.

Authorize

How .authorize() checks a principal, and why argv() routes have none.