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 uses minimist for argument parsing and some options are identical (alias, boolean, string).
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 minimist has trouble parsing or to modify/discard arguments.
Example:
{ args: (args: string[]) => args.filter(arg => arg !== 'omit');}
binaries
Section titled “binaries”Executables for the dependency.
Example:
{ binaries: ['tsc'];}
Default: plugin name, e.g. for the ESLint plugin the value is ["eslint"]
boolean
Section titled “boolean”Mark arguments as boolean. By default, arguments are expected to have string values.
config
Section titled “config”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.
fromArgs
Section titled “fromArgs”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.
Example:
{ fromArgs: ['exec'];}
Then this script:
nodemon --exec "node index.js"
Will have "node index.js"
being parsed as a new script.
nodeImportArgs
Section titled “nodeImportArgs”Set to true
as a shorthand for this alias:
{ import: ['r', 'experimental-loader', 'require', 'loader']}
Example:
{ nodeImportArgs: true;}
positional
Section titled “positional”Set to true
to use the first positional argument as an entry point.
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.
resolve
Section titled “resolve”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.
resolveInputs
Section titled “resolveInputs”Return inputs from parsed arguments
{ resolveInputs: (parsed: ParsedArgs) => parsed['flag'] ? [toDependency('package')] : [];}
string
Section titled “string”Mark arguments as string. This is the default, but number-looking arguments are returned as numbers by minimist.
ISC License © 2024 Lars Kappert