Skip to content

Commit 2344315

Browse files
feat(cli): let embedders reuse parsed arguments (#43941)
Co-authored-by: microsoft-playwright-automation[bot] <203992400+microsoft-playwright-automation[bot]@users.noreply.github.com>
1 parent ae56e26 commit 2344315

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

  • .claude/skills/playwright-dev
  • packages/playwright-core/src/tools/cli-client

‎.claude/skills/playwright-dev/tools.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,14 @@ test('my-command', async ({ cli, server }) => {
312312

313313
---
314314

315+
### Embedding the CLI Client
316+
317+
The `playwright-core/lib/tools/cli-client/program` entry point exports `parseArgs(argv)`
318+
and `program(options?)`. An embedder can parse once, inspect `args._[0]`, then call
319+
`program({ args, embedderVersion })`. `parseArgs` uses the CLI's boolean options and normalizes
320+
`-s` to `session` and `-g` to `global`. Without `args`, `program` parses
321+
`process.argv.slice(2)` as usual. `program` can mutate `args`, so inspect it before the call.
322+
315323
## Adding Config Options
316324

317325
When you need to add a new config option, update these files in order:

‎packages/playwright-core/src/tools/cli-client/program.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,8 @@ const booleanOptions: (keyof (GlobalOptions & OpenOptions & AttachOptions & { al
7373
'version',
7474
];
7575

76-
export async function program(options?: { embedderVersion?: string}) {
77-
const clientInfo = createClientInfo();
76+
export function parseArgs(argv: string[]): MinimistArgs {
7877
const help = require(libPath('tools', 'cli-client', 'help.json'));
79-
80-
const argv = process.argv.slice(2);
8178
const boolean = [...help.booleanOptions, ...booleanOptions];
8279
const args: MinimistArgs = minimist(argv, { boolean, string: ['_'] });
8380
// Normalize -s alias to --session
@@ -90,6 +87,13 @@ export async function program(options?: { embedderVersion?: string}) {
9087
args.global = true;
9188
delete args.g;
9289
}
90+
return args;
91+
}
92+
93+
export async function program(options?: { args?: MinimistArgs, embedderVersion?: string }) {
94+
const clientInfo = createClientInfo();
95+
const help = require(libPath('tools', 'cli-client', 'help.json'));
96+
const args = options?.args ?? parseArgs(process.argv.slice(2));
9397

9498
const output: Output = args.json ? new JsonOutput() : new TextOutput();
9599
const commandName = args._?.[0];

0 commit comments

Comments
 (0)