python-archieve-projects/WechatBot/node_modules/cmd-ts/dist/cjs/flag.js

157 lines
6.1 KiB
JavaScript
Executable File

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.flag = exports.fullFlag = exports.boolean = void 0;
const findOption_1 = require("./newparser/findOption");
const type_1 = require("./type");
const chalk_1 = __importDefault(require("chalk"));
const Result = __importStar(require("./Result"));
const types_1 = require("./types");
/**
* A decoder from `string` to `boolean`
* works for `true` and `false` only.
*/
exports.boolean = {
async from(str) {
if (str === 'true')
return true;
if (str === 'false')
return false;
throw new Error(`expected value to be either "true" or "false". got: "${str}"`);
},
displayName: 'true/false',
defaultValue: () => false,
};
function fullFlag(config) {
var _a;
const decoder = type_1.extendType(exports.boolean, config.type);
return {
description: (_a = config.description) !== null && _a !== void 0 ? _a : config.type.description,
helpTopics() {
var _a, _b, _c, _d;
let usage = `--${config.long}`;
if (config.short) {
usage += `, -${config.short}`;
}
const defaults = [];
if (config.env) {
const env = process.env[config.env] === undefined
? ''
: `=${chalk_1.default.italic(process.env[config.env])}`;
defaults.push(`env: ${config.env}${env}`);
}
try {
const defaultValueFn = (_a = config.defaultValue) !== null && _a !== void 0 ? _a : config.type.defaultValue;
const defaultValueIsSerializable = (_b = config.defaultValueIsSerializable) !== null && _b !== void 0 ? _b : config.type.defaultValueIsSerializable;
if (defaultValueFn && defaultValueIsSerializable) {
const defaultValue = defaultValueFn();
defaults.push('default: ' + chalk_1.default.italic(defaultValue));
}
}
catch (e) { }
return [
{
category: 'flags',
usage,
defaults,
description: (_d = (_c = config.description) !== null && _c !== void 0 ? _c : config.type.description) !== null && _d !== void 0 ? _d : '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 = findOption_1.findOption(nodes, {
longNames: [config.long],
shortNames: config.short ? [config.short] : [],
}).filter((x) => !visitedNodes.has(x));
options.forEach((opt) => visitedNodes.add(opt));
if (options.length > 1) {
return Result.err({
errors: [
{
nodes: options,
message: 'Expected 1 occurence, got ' + options.length,
},
],
});
}
const valueFromEnv = config.env ? process.env[config.env] : undefined;
let rawValue;
let envPrefix = '';
if (options.length === 0 && valueFromEnv !== undefined) {
rawValue = valueFromEnv;
envPrefix = `env[${chalk_1.default.italic(config.env)}]: `;
}
else if (options.length === 0 &&
typeof config.type.defaultValue === 'function') {
try {
return Result.ok(config.type.defaultValue());
}
catch (e) {
const message = `Default value not found for '--${config.long}': ${e.message}`;
return Result.err({
errors: [{ message, nodes: [] }],
});
}
}
else if (options.length === 1) {
rawValue = (_b = (_a = options[0].value) === null || _a === void 0 ? void 0 : _a.node.raw) !== null && _b !== void 0 ? _b : 'true';
}
else {
return Result.err({
errors: [
{ nodes: [], message: `No value provided for --${config.long}` },
],
});
}
const decoded = await Result.safeAsync(decoder.from(rawValue));
if (Result.isErr(decoded)) {
return Result.err({
errors: [
{
nodes: options,
message: envPrefix + decoded.error.message,
},
],
});
}
return decoded;
},
};
}
exports.fullFlag = fullFlag;
function flag(config) {
return fullFlag({
type: types_1.boolean,
...config,
});
}
exports.flag = flag;
//# sourceMappingURL=flag.js.map