How witr Handles Multi-Target Queries with Mixed Input Types
witr treats every query as a request for a process and accepts any combination of repeatable target flags (--pid, --port, --file, --container) together with positional name arguments, processing them sequentially with clearly labeled output blocks for each target.
The open-source tool witr (What Is Running This?) provides a unified interface for investigating process origins across different system resources. According to the pranshuparmar/witr source code, the CLI is designed to handle heterogeneous queries—mixing PIDs, ports, filenames, container IDs, and service names—in a single execution without ambiguity. This capability allows system administrators to diagnose multiple unrelated resources in one command while maintaining deterministic output order and consistent formatting.
Repeatable Target Flags and Mixed Input Types
The core flag-parsing logic in the app package enables multi-target queries with mixed input types by treating all target specifications as repeatable arguments. In cmd/witr/main.go (lines 9-19), the entry point initializes the CLI and forwards raw arguments to the internal query dispatcher:
// cmd/witr/main.go
package main
import (
"github.com/pranshuparmar/witr/internal/app"
)
func main() {
app.Execute()
}
The dispatcher iterates over the parsed list of targets and invokes the same lookup pipeline for each input type. This design allows you to combine any of the following flags in a single command:
--pid: Process IDs (repeatable)--port: Network ports (repeatable)--file: File paths or locks (repeatable)--container: Container names or IDs (repeatable)- Positional arguments : Process or service names (repeatable)
Because each target is handled independently, the tool resolves heterogeneous combinations—such as a process name, a network port, and a container ID—without cross-interference.
Sequential Processing and Output Labeling
When executing multi-target queries with mixed input types, witr processes targets in the exact order they appear on the command line. As documented in the README.md (lines 107-115) under the "Multiple Inputs" section, the tool prints a separate, clearly labeled result block for each target:
witr nginx --port 5432 --pid 1234
----- [name: nginx] -----
Target : nginx
Process : nginx (pid 2311)
...
----- [port: 5432] -----
Target : postgres
Process : postgres (pid 891)
...
----- [pid: 1234] -----
Target : node
Process : node (pid 1234)
...
Each output block is prefixed with a header identifying the target type and value (e.g., [name: nginx], [port: 5432], [pid: 1234]). This labeling ensures that results remain unambiguous even when querying disparate resource types simultaneously.
Uniform Output Formatting Across All Targets
All output format flags apply automatically to every target in a mixed query. The dispatcher passes the same configuration options to each target resolver, ensuring consistency across the result set:
--short: Condensed single-line output--tree: Process tree visualization--json: Machine-readable JSON output--env: Environment variable display--warnings: Warning inclusion--verbose: Detailed diagnostic information
For example, combining a container name, file lock, and PID with the short format flag produces consistent output for each target type:
witr --container redis --file /var/lib/dpkg/lock --pid 5678 --short
systemd (pid 1) → docker (pid 1023) → redis (pid 5678)
systemd (pid 1) → apt (pid 2034) → dpkg (pid 5679)
systemd (pid 1) → myservice (pid 5678)
For programmatic consumption, the JSON output aggregates all heterogeneous targets into a unified structure:
witr nginx --port 8080 --json
{
"results": [
{ "type": "name", "target": "nginx", "process": { ... } },
{ "type": "port", "target": "8080", "process": { ... } }
]
}
Implementation Architecture
The multi-target query capability relies on three key architectural components:
-
internal/app/*: Contains the flag parser and the dispatcher loop that iterates over each parsed target, invoking the same resolver for every input type regardless of whether it is a PID, port, filename, or container ID. -
pkg/model/*(e.g.,target.go,process.go) : Defines data structures that represent each query result uniformly, allowing the formatter to handle different target kinds with identical logic. -
internal/tui/*: Powers the interactive TUI mode, which respects the same multi-target logic when users select multiple tabs or input sources.
Summary
- witr accepts any combination of
--pid,--port,--file,--container, and positional name arguments in a single command. - Targets are processed sequentially in the order they appear on the command line.
- Each result block is prefixed with a type-specific header (e.g.,
[port: 5432]) to prevent ambiguity. - Output format flags (
--short,--json, etc.) apply uniformly to all targets in the query. - The dispatcher logic in
cmd/witr/main.goand theinternal/apppackage handles mixed input types through a unified lookup pipeline.
Frequently Asked Questions
Can I mix process names, ports, and PIDs in a single witr command?
Yes. The CLI accepts any combination of repeatable target flags (--pid, --port, --file, --container) alongside positional process name arguments. The dispatcher processes each target independently using the same resolution pipeline, allowing you to query heterogeneous resources like witr nginx --port 5432 --pid 1234 in one execution.
How does witr determine the order of results when using multiple target types?
Results appear in the exact sequence that targets are specified on the command line. The internal query dispatcher iterates over the parsed argument list and invokes the lookup routine for each target in order, printing a labeled block immediately after each resolution completes.
Do output format flags like --json apply to all targets in a mixed query?
Yes. Formatting options including --short, --tree, --json, --env, and --verbose are applied uniformly to every target in the multi-target query. When using --json, the tool aggregates all heterogeneous results into a single JSON array under the results key.
What file paths contain the core logic for handling mixed input types?
The entry point in cmd/witr/main.go (lines 9-19) initializes the CLI and calls app.Execute(), which drives the query workflow. The actual flag parsing and target iteration logic resides in the internal/app package, while data structures supporting uniform target representation are located in pkg/model (e.g., target.go).
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →