Skip to content

Argument Parsing

Some plugins have an arg object in their implementation. It’s a way for plugins to customize how command-line arguments are parsed for their tool’s executables. Argument parsing in plugins help Knip identify dependencies and entry files from scripts.

Knip parses these arguments with a built-in parser. The options below customize how a tool’s scripts are parsed. Here’s an example:

const args = {
args: (args) => args // modify/filter the array of raw args
config: true, // shorthand to add `--config [config file]` (and shortcut `-c`) as a `config` file
fromArgs: ['exec'], // re-parse what's behind `exec` as a new script
};
export default {
title: 'my-plugin',
args
}

Also see type definitions and examples in existing plugins.

Define aliases.

Example:

{
require: ['r'];
}

Also see nodeImportArgs.

Modify or filter arguments before parsing. For edge cases preprocessing is useful, e.g. if the parser has trouble parsing or to modify/discard arguments.

Example:

{
args: (args: string[]) => args.filter(arg => arg !== 'omit');
}

Executables for the dependency.

Example:

{
binaries: ['tsc'];
}

Default: plugin name, e.g. for the ESLint plugin the value is ["eslint"]

Mark arguments as boolean. By default, arguments are expected to have string values. Set to true to have only string arguments take a value, like Node.js does.

Define arguments that contain the configuration file path. Usually you’ll want to set aliases too. Use true for shorthand to set alias + string + config.

Example:

{
config: true;
}

The tsup plugin has this. Now tsup --config tsup.client.json will have tsup.client.json go through resolveConfig (also -c alias).

Example:

{
config: ['p'];
}

This will mark e.g. tsc -p tsconfig.lib.json as a configuration file and it will be handled by resolveConfig of the (typescript) plugin.

Parse return value as a new script. Can be a an array of strings, or function that returns an array of strings and those values will be parsed separately. A function can also return a string to have it parsed as shell script text, for tools that run the command in a shell (e.g. env-cmd --use-shell).

Example:

{
fromArgs: ['exec'];
}

Then this script:

Terminal window
nodemon --exec "node index.js"

Will have "node index.js" being parsed as a new script.

Set to true as a shorthand for this alias:

{
import: ['r', 'experimental-loader', 'require', 'loader']
}

Example:

{
nodeImportArgs: true;
}

Set to true to use the first positional argument as an entry point, or use a function that returns the positional arguments to use as entry points.

Example:

{
positional: true;
}

The tsx plugin has this and "tsx script.ts" as a script will result in the script.ts file being an entry point.

List of arguments to resolve to a dependency or entry file path.

Example:

{
resolve: ['plugin'];
}

Now for a script like "program --plugin package" this will result in "package" being resolved as a dependency.

Return inputs from parsed arguments

{
resolveInputs: (parsed: ParsedArgs) =>
parsed['flag'] ? [toDependency('package')] : [];
}

Set to true to stop parsing options at the first positional argument, like Node.js does with node [options] script.js [arguments]. The arguments after it belong to the script.

Mark arguments as string. This is the default, but number-looking arguments are returned as numbers.

ISC License © 2026Lars Kappert