cmd-ts
Version:
> 💻 A type-driven command line argument parser, with awesome error reporting 🤤
98 lines • 3.77 kB
JavaScript
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 (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.multiflag = void 0;
const findOption_1 = require("./newparser/findOption");
const flag_1 = require("./flag");
const Result = __importStar(require("./Result"));
/**
* Like `option`, but can accept multiple options, and expects a decoder from a list of strings.
* An error will highlight all option occurences.
*/
function multiflag(config) {
return {
helpTopics() {
var _a;
let usage = `--${config.long}`;
if (config.short) {
usage += `, -${config.short}`;
}
return [
{
category: 'flags',
usage,
defaults: [],
description: (_a = config.description) !== null && _a !== void 0 ? _a : 'self explanatory',
},
];
},
register(opts) {
opts.forceFlagLongNames.add(config.long);
if (config.short) {
opts.forceFlagShortNames.add(config.short);
}
},
async parse({ nodes, visitedNodes, }) {
var _a, _b;
const options = (0, findOption_1.findOption)(nodes, {
longNames: [config.long],
shortNames: config.short ? [config.short] : [],
}).filter(x => !visitedNodes.has(x));
for (const option of options) {
visitedNodes.add(option);
}
const optionValues = [];
const errors = [];
for (const option of options) {
const decoded = await Result.safeAsync(flag_1.boolean.from((_b = (_a = option.value) === null || _a === void 0 ? void 0 : _a.node.raw) !== null && _b !== void 0 ? _b : 'true'));
if (Result.isErr(decoded)) {
errors.push({ nodes: [option], message: decoded.error.message });
}
else {
optionValues.push(decoded.value);
}
}
if (errors.length > 0) {
return Result.err({
errors,
});
}
const multiDecoded = await Result.safeAsync(config.type.from(optionValues));
if (Result.isErr(multiDecoded)) {
return Result.err({
errors: [
{
nodes: options,
message: multiDecoded.error.message,
},
],
});
}
return multiDecoded;
},
};
}
exports.multiflag = multiflag;
//# sourceMappingURL=multiflag.js.map
;