Umama-at-Bluchip/Quick-UI
0
1// This is an example of using tokens to add a custom behaviour.2//3// This adds a option order check so that --some-unstable-option4// may only be used after --enable-experimental-options5//6// Note: this is not a common behaviour, the order of different options7// does not usually matter.8 9import { parseArgs } from '../index.js';10 11function findTokenIndex(tokens, target) {12 return tokens.findIndex((token) => token.kind === 'option' &&13 token.name === target14 );15}16 17const experimentalName = 'enable-experimental-options';18const unstableName = 'some-unstable-option';19 20const options = {21 [experimentalName]: { type: 'boolean' },22 [unstableName]: { type: 'boolean' },23};24 25const { values, tokens } = parseArgs({ options, tokens: true });26 27const experimentalIndex = findTokenIndex(tokens, experimentalName);28const unstableIndex = findTokenIndex(tokens, unstableName);29if (unstableIndex !== -1 &&30 ((experimentalIndex === -1) || (unstableIndex < experimentalIndex))) {31 throw new Error(`'--${experimentalName}' must be specified before '--${unstableName}'`);32}33 34console.log(values);35 36/* eslint-disable max-len */37// Try the following:38// node ordered-options.mjs39// node ordered-options.mjs --some-unstable-option40// node ordered-options.mjs --some-unstable-option --enable-experimental-options41// node ordered-options.mjs --enable-experimental-options --some-unstable-option42 