@fontoxml/fontoxml-development-tools
Version:
Development tools for Fonto.
115 lines (97 loc) • 2.57 kB
JavaScript
import symbols from './symbols.js';
import VariableSyntaxPart from './VariableSyntaxPart.js';
export default class Option extends VariableSyntaxPart {
constructor(name) {
super(name);
this.useDefaultIfFlagMissing = false;
}
[symbols.isMatchForPart](value) {
// return true if value is a (grouped) short, or long notation
return value.indexOf('-') !== 0
? false
: (this.short && value.charAt(1) === this.short) ||
value === `--${this.name}`;
}
[symbols.updateTiersAfterMatch](tiers) {
// do not change tiers because options are always recognizable, and may occur in input again
return tiers;
}
[symbols.spliceInputFromParts](parts, _tiers) {
// if this is a short notation (for one or more flags)
if (this.short && parts[0].charAt(1) === this.short) {
// remove the flag signifier from group
parts[0] = `-${parts[0].substr(2)}`; // replace(this.short, '');
// if the group is not empty, stop parsing this option
if (parts[0] !== '-') {
return undefined;
}
}
// Stop caring about the flag signifier
parts.shift();
// if value is a dash, stop parsing
if (parts[0] === '-') {
parts.shift();
return undefined;
}
// use next input part if it is not another option
if (parts[0] && parts[0].charAt(0) !== '-') {
return parts.shift();
}
return undefined;
}
[symbols.applyDefault](value, isUndefined) {
if (this.required && isUndefined) {
return undefined;
}
if (value === undefined) {
if (this.useDefaultIfFlagMissing || !isUndefined) {
return this.cloneDefault() || true;
}
}
return value;
}
[symbols.createContributionToRequestObject](
_accumulated,
value,
_isUndefined
) {
return {
options: {
[this.name]: value,
},
};
}
getType() {
return 'option';
}
/**
*
* @param value
* @param {boolean} [useDefaultIfFlagMissing] - Ignored when option is also required -- will fail validation
* @return {Option}
*/
setDefault(value, useDefaultIfFlagMissing) {
this.default = value;
this.useDefaultIfFlagMissing = !!useDefaultIfFlagMissing;
return this;
}
/**
* Set a one-letter alias for a flag.
* @param {string} short
* @return {Option}
*/
setShort(short) {
this.short = short;
return this;
}
/**
* Allows this option to still be parsed when an IsolatedOption is also used.
*
* @param {boolean} isExemptFromIsolatedOption
* @return {Option}
*/
setIsExemptFromIsolatedOption(isExemptFromIsolatedOption) {
this.isExemptFromIsolatedOption = !!isExemptFromIsolatedOption;
return this;
}
}