MCPcopy
hub / github.com/shelljs/shelljs / parseOptions

Function parseOptions

src/common.js:185–254  ·  view source on GitHub ↗
(opt, map, errorOptions)

Source from the content-addressed store, hash-verified

183// Throws an error when passed a string that does not start with '-':
184// parseOptions('a', {'a':'alice'}); // throws
185function parseOptions(opt, map, errorOptions) {
186 errorOptions = errorOptions || {};
187 // Validate input
188 if (typeof opt !== 'string' && !isObject(opt)) {
189 throw new TypeError('options must be strings or key-value pairs');
190 } else if (!isObject(map)) {
191 throw new TypeError('parseOptions() internal error: map must be an object');
192 } else if (!isObject(errorOptions)) {
193 throw new TypeError(
194 'parseOptions() internal error: errorOptions must be object',
195 );
196 }
197
198 if (opt === '--') {
199 // This means there are no options.
200 return {};
201 }
202
203 // All options are false by default
204 var options = {};
205 Object.keys(map).forEach(function (letter) {
206 var optName = map[letter];
207 if (optName[0] !== '!') {
208 options[optName] = false;
209 }
210 });
211
212 if (opt === '') return options; // defaults
213
214 if (typeof opt === 'string') {
215 if (opt[0] !== '-') {
216 throw new Error("Options string must start with a '-'");
217 }
218
219 // e.g. chars = ['R', 'f']
220 var chars = opt.slice(1).split('');
221
222 chars.forEach(function (c) {
223 if (c in map) {
224 var optionName = map[c];
225 if (optionName[0] === '!') {
226 options[optionName.slice(1)] = false;
227 } else {
228 options[optionName] = true;
229 }
230 } else {
231 error('option not recognized: ' + c, errorOptions);
232 }
233 });
234 } else { // opt is an Object
235 Object.keys(opt).forEach(function (key) {
236 if (key[0] === '-') {
237 // key is a string of the form '-r', '-d', etc.
238 var c = key[1];
239 if (c in map) {
240 var optionName = map[c];
241 options[optionName] = opt[key]; // assign the given value
242 } else {

Callers 1

wrapFunction · 0.85

Calls 2

isObjectFunction · 0.85
errorFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…