98 lines
2.5 KiB
TypeScript
98 lines
2.5 KiB
TypeScript
import {
|
|
ArgParser,
|
|
ParsingResult,
|
|
ParseContext,
|
|
ParsingError,
|
|
} from './argparser.ts';
|
|
import { From, OutputOf } from './from.ts';
|
|
import { findOption } from './newparser/findOption.ts';
|
|
import { ProvidesHelp, LongDoc, Descriptive, ShortDoc } from './helpdoc.ts';
|
|
import { boolean } from './flag.ts';
|
|
import { HasType } from './type.ts';
|
|
import * as Result from './Result.ts';
|
|
|
|
type MultiFlagConfig<Decoder extends From<boolean[], any>> = HasType<Decoder> &
|
|
LongDoc &
|
|
Partial<Descriptive & ShortDoc>;
|
|
|
|
/**
|
|
* Like `option`, but can accept multiple options, and expects a decoder from a list of strings.
|
|
* An error will highlight all option occurences.
|
|
*/
|
|
export function multiflag<Decoder extends From<boolean[], any>>(
|
|
config: MultiFlagConfig<Decoder>
|
|
): ArgParser<OutputOf<Decoder>> & ProvidesHelp {
|
|
return {
|
|
helpTopics() {
|
|
let usage = `--${config.long}`;
|
|
if (config.short) {
|
|
usage += `, -${config.short}`;
|
|
}
|
|
return [
|
|
{
|
|
category: 'flags',
|
|
usage,
|
|
defaults: [],
|
|
description: config.description ?? 'self explanatory',
|
|
},
|
|
];
|
|
},
|
|
register(opts) {
|
|
opts.forceFlagLongNames.add(config.long);
|
|
if (config.short) {
|
|
opts.forceFlagShortNames.add(config.short);
|
|
}
|
|
},
|
|
async parse({
|
|
nodes,
|
|
visitedNodes,
|
|
}: ParseContext): Promise<ParsingResult<OutputOf<Decoder>>> {
|
|
const options = 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: boolean[] = [];
|
|
const errors: ParsingError[] = [];
|
|
|
|
for (const option of options) {
|
|
const decoded = await Result.safeAsync(
|
|
boolean.from(option.value?.node.raw ?? '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;
|
|
},
|
|
};
|
|
}
|