zod-opts
Version:
node.js CLI option parser / validator using Zod
129 lines (128 loc) • 4.36 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.Command = void 0;
exports.command = command;
const helper = __importStar(require("./parser_helper"));
const util = __importStar(require("./util"));
class Command {
constructor({ name, description, options, positionalArgs, validation, handler, action, } = {}) {
this._options = {};
this._positionalArgs = [];
this._name = name;
this._description = description;
if (options !== undefined) {
this._options = options;
}
if (positionalArgs !== undefined) {
this._positionalArgs = positionalArgs;
}
this._validation = validation;
this._handler = handler;
this._action = action;
}
description(description) {
this._description = description;
return this;
}
options(options) {
util.validateParamOptionsAndPositionalArguments(options, this._positionalArgs);
return new Command({
...this._currentState(),
options,
});
}
args(positionalArgs) {
util.validateParamOptionsAndPositionalArguments(this._options, positionalArgs);
return new Command({
...this._currentState(),
positionalArgs: positionalArgs,
});
}
validation(validation) {
this._validation = validation;
return this;
}
action(action) {
this._action = action;
return this;
}
toInternalCommand() {
this._validateMultipleCommands();
if (this._name === undefined) {
throw new Error("name is required for command");
}
return {
name: this._name,
description: this._description,
options: helper.generateInternalOptions(this._options),
positionalArgs: helper.generateInternalPositionalArguments(this._positionalArgs),
};
}
_currentState() {
return {
name: this._name,
description: this._description,
options: this._options,
positionalArgs: this._positionalArgs,
validation: this._validation,
handler: this._handler,
action: this._action,
};
}
_validateMultipleCommands() {
if (this._action === undefined) {
throw new Error("action is required for command");
}
}
_toParseCommand() {
this._validateMultipleCommands();
const action = this._action;
if (action === undefined) {
throw new Error("action is required for command");
}
const shape = helper.generateZodShape(this._options, this._positionalArgs);
return {
shape,
internalCommand: this.toInternalCommand(),
action,
validation: this._validation,
};
}
}
exports.Command = Command;
function command(name) {
return new Command({ name });
}