Base CLI Schema
raw JSON →This package provides a schema definition and normalization utility specifically designed for the `base-cli` plugin. Its primary function is to process and normalize command-line argument values (`argv`) before they are consumed by `cli.process()` within a `base` application. It allows for defining custom fields with specified types (e.g., 'string') and provides a `normalize` function that applies these definitions, including custom normalizer functions, to an `argv` object. The package is currently at version 0.1.19 and was last published in May 2016. Given its age and low version number, the release cadence is effectively inactive, indicating it is no longer actively maintained. Key differentiators include its tight integration with the `base` framework and its focused purpose of defining and applying schemas for CLI arguments, offering a structured approach to argument normalization within the `base` ecosystem, unlike more general-purpose validation libraries.
Common errors
error ReferenceError: require is not defined in ES module scope ↓
.js without type: "module") or use a tool like createRequire from the module module if you must mix ESM and CJS (e.g., import { createRequire } from 'module'; const require = createRequire(import.meta.url); const cliSchema = require('base-cli-schema');). Ideally, migrate to an ESM-compatible alternative. error TypeError: app.use is not a function ↓
const Base = require('base'); and const cli = require('base-cli'); are present, and that app.use(cli()); is called on your new Base() instance before using cliSchema. Warnings
breaking This package, including its core dependency `base`, is CommonJS-only. It does not support ES Modules (ESM) and will cause runtime errors if directly imported or `require`d within an ESM context without specific transpilation or loader configurations. ↓
gotcha The package is effectively abandoned, with its last publish in 2016. This means there will be no new features, bug fixes, or security updates. It may not be compatible with newer Node.js versions or ecosystem changes. ↓
gotcha Heavy reliance on the `base` framework, which itself is an older, less commonly used architecture in modern Node.js development. Understanding `base`'s plugin system is crucial for effective use of `base-cli-schema`. ↓
Install
npm install base-cli-schema yarn add base-cli-schema pnpm add base-cli-schema Imports
- cliSchema wrong
import cliSchema from 'base-cli-schema';correctconst cliSchema = require('base-cli-schema'); - Base wrong
import Base from 'base';correctconst Base = require('base'); - minimist wrong
import minimist from 'minimist';correctconst minimist = require('minimist');
Quickstart
const argv = require('minimist')(process.argv.slice(2));
const Base = require('base');
const cliSchema = require('base-cli-schema');
const cli = require('base-cli'); // Assuming base-cli is also required for app.use(cli())
const app = new Base();
app.use(cli()); // Attach the cli plugin to the base application
const schema = cliSchema(app);
// Define a custom field for demonstration
schema.field('foo', 'string', {
normalize: function(val, key, config, schema) {
// Example normalization: always return 'bar' for 'foo'
return val ? val.toUpperCase() : 'bar';
}
});
// Simulate argv input, e.g., 'node your-script.js --foo=test --other=value'
const mockArgv = { foo: 'hello', other: 'world' };
const obj = schema.normalize(mockArgv);
console.log(obj);
// Expected output: { foo: 'HELLO', other: 'world' } (if --foo=hello was passed)
// or { foo: 'BAR' } if --foo was not passed and 'bar' is default/fallback