123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- 'use strict';
- const {
- ArrayPrototypeFind,
- ObjectEntries,
- ObjectPrototypeHasOwnProperty: ObjectHasOwn,
- StringPrototypeCharAt,
- StringPrototypeIncludes,
- StringPrototypeStartsWith,
- } = require('./internal/primordials');
- const {
- validateObject,
- } = require('./internal/validators');
- function objectGetOwn(obj, prop) {
- if (ObjectHasOwn(obj, prop))
- return obj[prop];
- }
- function optionsGetOwn(options, longOption, prop) {
- if (ObjectHasOwn(options, longOption))
- return objectGetOwn(options[longOption], prop);
- }
- function isOptionValue(value) {
- if (value == null) return false;
-
-
- return true;
- }
- function isOptionLikeValue(value) {
- if (value == null) return false;
- return value.length > 1 && StringPrototypeCharAt(value, 0) === '-';
- }
- function isLoneShortOption(arg) {
- return arg.length === 2 &&
- StringPrototypeCharAt(arg, 0) === '-' &&
- StringPrototypeCharAt(arg, 1) !== '-';
- }
- function isLoneLongOption(arg) {
- return arg.length > 2 &&
- StringPrototypeStartsWith(arg, '--') &&
- !StringPrototypeIncludes(arg, '=', 3);
- }
- function isLongOptionAndValue(arg) {
- return arg.length > 2 &&
- StringPrototypeStartsWith(arg, '--') &&
- StringPrototypeIncludes(arg, '=', 3);
- }
- function isShortOptionGroup(arg, options) {
- if (arg.length <= 2) return false;
- if (StringPrototypeCharAt(arg, 0) !== '-') return false;
- if (StringPrototypeCharAt(arg, 1) === '-') return false;
- const firstShort = StringPrototypeCharAt(arg, 1);
- const longOption = findLongOptionForShort(firstShort, options);
- return optionsGetOwn(options, longOption, 'type') !== 'string';
- }
- function isShortOptionAndValue(arg, options) {
- validateObject(options, 'options');
- if (arg.length <= 2) return false;
- if (StringPrototypeCharAt(arg, 0) !== '-') return false;
- if (StringPrototypeCharAt(arg, 1) === '-') return false;
- const shortOption = StringPrototypeCharAt(arg, 1);
- const longOption = findLongOptionForShort(shortOption, options);
- return optionsGetOwn(options, longOption, 'type') === 'string';
- }
- function findLongOptionForShort(shortOption, options) {
- validateObject(options, 'options');
- const longOptionEntry = ArrayPrototypeFind(
- ObjectEntries(options),
- ({ 1: optionConfig }) => objectGetOwn(optionConfig, 'short') === shortOption
- );
- return longOptionEntry?.[0] ?? shortOption;
- }
- function useDefaultValueOption(longOption, optionConfig, values) {
- return objectGetOwn(optionConfig, 'default') !== undefined &&
- values[longOption] === undefined;
- }
- module.exports = {
- findLongOptionForShort,
- isLoneLongOption,
- isLoneShortOption,
- isLongOptionAndValue,
- isOptionValue,
- isOptionLikeValue,
- isShortOptionAndValue,
- isShortOptionGroup,
- useDefaultValueOption,
- objectGetOwn,
- optionsGetOwn,
- };
|