# core/resolvers/invocation.resolver.ts

> 33 lines of code and 5 definitions.

Tree: Argv Contract
Language: typescript
Layer: infrastructure
Canonical: https://banes-lab.com/anatomy/argv#file-argv-core-resolvers-invocation-resolver-ts
Source text: https://banes-lab.com/source/argv/core/resolvers/invocation.resolver.ts.txt

Listed in [core/resolvers](https://banes-lab.com/api/source/argv/core/resolvers.md).

## Definitions

- `resolveArgv` (lexical_declaration, line 25, exported)
- `ArgvRefusedError` (class_declaration, line 7, exported)
- `constructor` (method_definition, line 8, exported)
- `argvOf` (lexical_declaration, line 14, exported)
- `outcome` (lexical_declaration, line 26, exported)

## Contained in

- [core/resolvers](https://banes-lab.com/anatomy/argv/folder-argv-core-resolvers.md)

## Uses

- [configuration/strings/invocation.strings.ts](https://banes-lab.com/source/argv/configuration/strings/invocation.strings.ts.md)

## Linked from

- [configuration/strings](https://banes-lab.com/anatomy/argv/folder-argv-configuration-strings.md)

## Source

```typescript
import { ARGV_OFFSET, EXIT_CLEAN, EXIT_REFUSED } from "#configuration/constants/invocation.constants";
import type { ArgvSpec, ParsedArgv } from "#types/invocation.types";
import { HELP_UNANSWERED, refusedLine } from "#configuration/strings/invocation.strings";
import { parseArgv } from "#core/converters/invocation.converter";
import process from "node:process";

export class ArgvRefusedError extends Error {
    public constructor(reason: string) {
        super(reason);
        this.name = "ArgvRefusedError";
    }
}

export const argvOf = function argvOf(spec: ArgvSpec, words: readonly string[]): ParsedArgv {
    const outcome = parseArgv(spec, words);
    if (outcome.kind === "help") {
        throw new ArgvRefusedError(HELP_UNANSWERED);
    }
    if (outcome.kind === "refused") {
        throw new ArgvRefusedError(outcome.reason);
    }
    return outcome.argv;
};

export const resolveArgv = function resolveArgv(spec: ArgvSpec, argv?: readonly string[]): ParsedArgv {
    const outcome = parseArgv(spec, argv ?? process.argv.slice(ARGV_OFFSET));
    if (outcome.kind === "help") {
        process.stdout.write(outcome.usage);
        process.exit(EXIT_CLEAN);
    }
    if (outcome.kind === "refused") {
        process.stderr.write(refusedLine(outcome.reason, outcome.usage));
        process.exit(EXIT_REFUSED);
    }
    return outcome.argv;
};
```
