(
argv: string[],
cfg: {
usageMessage?: string;
scriptName?: string;
commands?: CommandModule[];
options?: { [key: string]: Options };
groups?: { [key: string]: string[] };
examples?: [string, string][];
middlewares?: {
middlewareFunction: MiddlewareFunction;
applyBeforeValidation?: boolean;
}[];
noExitProcess?: boolean;
},
)
| 45 | * yargsCli(hideBin(process.argv)).argv; |
| 46 | */ |
| 47 | export function yargsCli<T = unknown>( |
| 48 | argv: string[], |
| 49 | cfg: { |
| 50 | usageMessage?: string; |
| 51 | scriptName?: string; |
| 52 | commands?: CommandModule[]; |
| 53 | options?: { [key: string]: Options }; |
| 54 | groups?: { [key: string]: string[] }; |
| 55 | examples?: [string, string][]; |
| 56 | middlewares?: { |
| 57 | middlewareFunction: MiddlewareFunction; |
| 58 | applyBeforeValidation?: boolean; |
| 59 | }[]; |
| 60 | noExitProcess?: boolean; |
| 61 | }, |
| 62 | ): Argv<T> { |
| 63 | const { usageMessage, scriptName, noExitProcess } = cfg; |
| 64 | const commands = cfg.commands ?? []; |
| 65 | const middlewares = cfg.middlewares ?? []; |
| 66 | const options = cfg.options ?? {}; |
| 67 | const groups = cfg.groups ?? {}; |
| 68 | const examples = cfg.examples ?? []; |
| 69 | const cli = yargs(argv); |
| 70 | |
| 71 | // setup yargs |
| 72 | cli |
| 73 | .updateLocale(yargsDecorator) |
| 74 | // take minimum of TERMINAL_WIDTH or full width of the terminal |
| 75 | .wrap(Math.max(TERMINAL_WIDTH, cli.terminalWidth())) |
| 76 | .help('help', descriptionStyle('Show help')) |
| 77 | .alias('h', 'help') |
| 78 | .showHelpOnFail(false) |
| 79 | .version('version', ansis.dim('Show version'), getVersion()) |
| 80 | .check(args => { |
| 81 | const persist = args['persist'] as PersistConfig | undefined; |
| 82 | return persist == null || validatePersistFormat(persist); |
| 83 | }) |
| 84 | .parserConfiguration({ |
| 85 | 'strip-dashed': true, |
| 86 | } satisfies Partial<ParserConfigurationOptions>) |
| 87 | .options(formatNestedValues(options, 'describe')); |
| 88 | |
| 89 | // use last argument for non-array options |
| 90 | coerceArraysByOptionType(cli, options); |
| 91 | |
| 92 | // usage message |
| 93 | if (usageMessage) { |
| 94 | cli.usage(titleStyle(usageMessage)); |
| 95 | } |
| 96 | |
| 97 | // script name |
| 98 | if (scriptName) { |
| 99 | cli.scriptName(scriptName); |
| 100 | } |
| 101 | |
| 102 | // add examples |
| 103 | examples.forEach(([exampleName, description]) => |
| 104 | cli.example(exampleName, descriptionStyle(description)), |
no test coverage detected