CLI sprintf Formatter
raw JSON →cli-sprintf-format is a utility package that provides an enhanced `sprintf`-like formatting function specifically tailored for command-line interface (CLI) applications. It builds upon Node.js's `util.formatWithOptions`, offering more robust type resolution, error handling, and intelligent color support. The package automatically detects terminal color capabilities via `supports-color` and applies coloring not only to inspected objects but also to primitive values, including colored JSON output. The current stable version is 1.1.1, released in late 2021, suggesting a relatively stable but infrequent release cadence focused on maintenance and bug fixes. Key differentiators include its improved object inspection depth (defaulting to 4, configurable via `FORMAT_INSPECT_DEPTH`) and specialized handling of `%s` and `%#s` format specifiers for string output, allowing for colored and inline display or raw string output, respectively.
Common errors
error TypeError: cliFormat is not a function ↓
import cliFormat from 'cli-sprintf-format';. For CommonJS, use const cliFormat = require('cli-sprintf-format');. error Unexpected output format for strings (e.g., extra quotes, colors, newlines removed) ↓
%#s instead of %s. Warnings
gotcha When formatting strings with `%s`, cli-sprintf-format passes them through Node.js's `util.inspect` formatter. This results in colored output (if supported) and makes multiline strings appear inline. If you need the raw string output without this processing, use `%#s` instead. ↓
gotcha Object inspection depth for `%o` and `%O` specifiers defaults to `4`. For deeply nested objects, this might truncate output. The depth can be overridden globally via the `FORMAT_INSPECT_DEPTH` environment variable. ↓
breaking A bug fix in v1.1.1 ensured consistent literal output in Node.js v12+ versions. While a 'fix', applications relying on the previous (buggy) behavior in Node.js v12 or higher might see subtle changes in string literal representation. ↓
Install
npm install cli-sprintf-format yarn add cli-sprintf-format pnpm add cli-sprintf-format Imports
- cliFormat wrong
const cliFormat = require('cli-sprintf-format').default;correctimport cliFormat from 'cli-sprintf-format'; - cliFormat (CJS)
const cliFormat = require('cli-sprintf-format');
Quickstart
const cliFormat = require("cli-sprintf-format");
// Basic string and number formatting
console.log(cliFormat("User '%s' has %d unread messages.", "Alice", 5));
// Formatting with an object, showing default inspection depth and coloring
const userProfile = { id: 101, name: "Bob", email: "bob@example.com", settings: { theme: 'dark', notifications: true } };
console.log(cliFormat("Profile details: %o", userProfile));
// Formatting with JSON, which will also be colored if supported
const data = { status: 'success', items: [{ id: 1, value: 'test' }] };
console.log(cliFormat("API Response: %j", data));
// Using environment variable to control inspection depth (run with: FORMAT_INSPECT_DEPTH=2 node your-script.js)
console.log(cliFormat("Deep object (depth 2 via env): %o", { a: { b: { c: { d: 1 } } } }));