MCPcopy Create free account
hub / github.com/code-pushup/cli / yargsCli

Function yargsCli

packages/cli/src/lib/yargs-cli.ts:47–145  ·  view source on GitHub ↗
(
  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;
  },
)

Source from the content-addressed store, hash-verified

45 * yargsCli(hideBin(process.argv)).argv;
46 */
47export 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)),

Calls 11

descriptionStyleFunction · 0.85
getVersionFunction · 0.85
validatePersistFormatFunction · 0.85
formatNestedValuesFunction · 0.85
coerceArraysByOptionTypeFunction · 0.85
titleStyleFunction · 0.85
headerStyleFunction · 0.85
logErrorBeforeThrowFunction · 0.85
formatObjectValueFunction · 0.85
groupMethod · 0.80
commandMethod · 0.80

Tested by

no test coverage detected