jackwener/OpenCLI · error
Target tab required. Pass it as an argument or --tab <target
Error message
Target tab required. Pass it as an argument or --tab <targetId>.
What it means
`browser tab select` needs to know which tab to select: either a targetId positional argument or the --tab option. When resolveBrowserTabTarget finds neither (and no previously remembered default applies), the CLI throws this instructive error.
Source
Thrown at src/cli.ts:1215
.description('Create a new tab and print its target ID')
.action(browserAction(async (page, url?: string) => {
if (!page.newTab) {
throw new Error('This browser session does not support creating tabs');
}
const createdPage = await page.newTab(url);
console.log(JSON.stringify({
page: createdPage,
url: url ?? null,
}, null, 2));
}));
addBrowserTabOption(browserTab.command('select')
.argument('[targetId]', 'Target tab/page identity returned by "browser open", "browser tab new", or "browser tab list"')
.description('Select a tab by target ID and make it the default browser tab'))
.action(browserAction(async (page, targetId?: string, opts?: { tab?: string } | Command) => {
const resolvedTarget = resolveBrowserTabTarget(targetId, opts);
if (!resolvedTarget) {
throw new Error('Target tab required. Pass it as an argument or --tab <targetId>.');
}
await page.selectTab(resolvedTarget);
saveBrowserTargetState(resolvedTarget, getPageScope(page));
console.log(JSON.stringify({ selected: resolvedTarget }, null, 2));
}));
addBrowserTabOption(browserTab.command('close')
.argument('[targetId]', 'Target tab/page identity returned by "browser open", "browser tab new", or "browser tab list"')
.description('Close a tab by target ID'))
.action(browserAction(async (page, targetId?: string, opts?: { tab?: string } | Command) => {
const resolvedTarget = resolveBrowserTabTarget(targetId, opts);
if (!page.closeTab) {
throw new Error('This browser session does not support closing tabs');
}
if (!resolvedTarget) {
throw new Error('Target tab required. Pass it as an argument or --tab <targetId>.');
}
const validatedTarget = await resolveBrowserTargetInSession(page, resolvedTarget, {View on GitHub (pinned to 49907e53dc)
Solutions
- Pass the target ID: opencli browser <session> tab select <targetId>
- Or use the flag: opencli browser <session> tab select --tab <targetId>
- Get valid target IDs from `browser open` output, `browser tab new`, or `browser tab list`
- Check resolveBrowserTabTarget's precedence (positional vs --tab vs remembered state) if scripting
Example fix
// before opencli browser my-session tab select // after opencli browser my-session tab select --tab 1A2B3C4D5E6F
Defensive patterns
Strategy: validation
Validate before calling
const targetId = positional ?? opts.tab;
if (!targetId) throw new Error('tab select requires a targetId positional or --tab flag'); Type guard
function hasTabTarget(t: string | undefined, o?: { tab?: string }): t is string {
return Boolean(t ?? o?.tab);
} Try / catch
try {
await run(['opencli', 'browser', session, 'tab', 'select', targetId]);
} catch (e) {
if (String((e as Error).message).startsWith('Target tab required')) {
console.error('Get a targetId from `browser open`, `browser tab new`, or `browser tab list`.');
}
throw e;
} Prevention
- Capture the targetId printed by `browser open` / `browser tab new` into a variable and reuse it
- Run `browser tab list` to discover IDs when unsure
- Always pass --tab explicitly in non-interactive scripts
- Don't rely on remembered default targets across separate CLI invocations
When it happens
Trigger: Running `opencli browser <session> tab select` with no positional targetId and no --tab <targetId> flag.
Common situations: Omitting the argument in scripts; assuming the CLI auto-picks a tab; forgetting that `browser open` output's target ID must be passed along; stale remembered-tab state cleared between runs.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- keyword must not be empty
- <from> station must not be empty
- <to> station must not be empty
- who 不能为空
- key is required
AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29).
Data as JSON: /api/errors/84b7b63220895844.
Report an issue: GitHub.