Skip to content

Commit

Permalink
remove duplicated logic for handling -xyz
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchambers committed Feb 22, 2023
1 parent 09a47ea commit b49bea5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 36 deletions.
6 changes: 6 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
"no-unused-vars": ["error", {"args": "none", "varsIgnorePattern": "^(conf|spec)$"}]
}
},
{
"files": ["index.js"],
"rules": {
"curly": ["off"]
}
},
{
"files": ["test/index.js"],
"rules": {
Expand Down
65 changes: 29 additions & 36 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,49 +338,42 @@ def ('parseArgs')
([$.StrMap (Handler (a)),
$.Pair (a) ($.Array ($.String)),
$.Either ($.String) ($.Pair (a) ($.Array ($.String)))])
(spec => pair => {
let conf = pair.fst;
const args = pair.snd;
for (let i = 0; i < args.length; i += 1) {
const arg = args[i];
if (arg === '-') {
(spec => ([init, args]) => {
for (let conf = init, either, idx = 0; ; idx += 1) {
if (idx === args.length) {
return Right (Pair (conf) ([]));
} else if (args[idx] === '-') {
return Left ('- is not a valid flag or option name');
} else if (arg === '--') {
return Right (Pair (conf) (args.slice (i + 1)));
} else if (arg.startsWith ('--')) {
if (!(Object.prototype.propertyIsEnumerable.call (spec, arg))) {
return Left (arg + ' is unrecognized');
} else if (spec[arg].isLeft) {
conf = spec[arg].value (conf);
} else if (i === args.length - 1) {
return Left (arg + ' requires a value');
} else if (args[idx] === '--') {
return Right (Pair (conf) (args.slice (idx + 1)));
} else if (!(args[idx].startsWith ('-'))) {
return Right (Pair (conf) (args.slice (idx)));
} else for (const name of names (args[idx])) {
if (!(Object.prototype.propertyIsEnumerable.call (spec, name))) {
return Left (name + ' is unrecognized');
} else if ((either = spec[name]).isLeft) {
conf = either.value (conf);
} else if ((idx += 1) === args.length) {
return Left (name + ' requires a value');
} else if ((either = either.value (args[idx])).isLeft) {
return either;
} else {
const e = spec[arg].value (args[i += 1]);
if (e.isLeft) return e;
conf = e.value (conf);
conf = either.value (conf);
}
} else if (arg.startsWith ('-')) {
for (let j = 1; j < arg.length; j += 1) {
const name = '-' + arg[j];
if (!(Object.prototype.propertyIsEnumerable.call (spec, name))) {
return Left (name + ' is unrecognized');
} else if (spec[name].isLeft) {
conf = spec[name].value (conf);
} else if (j < arg.length - 1 || i === args.length - 1) {
return Left (name + ' requires a value');
} else {
const e = spec[name].value (args[i += 1]);
if (e.isLeft) return e;
conf = e.value (conf);
}
}
} else {
return Right (Pair (conf) (args.slice (i)));
}
}
return Right (Pair (conf) ([]));
});

// > names ('--color')
// ['--color']
// > names ('-xyz')
// ['-x', '-y', '-z']
const names = arg => (
arg.startsWith ('--')
? [arg]
: (arg.replace (/(?:)/g, '-')).match (/-[^-]/g)
);

export {
Setter,
Validator,
Expand Down

0 comments on commit b49bea5

Please sign in to comment.