-
Notifications
You must be signed in to change notification settings - Fork 1
/
parseArgs.js
44 lines (33 loc) · 1.2 KB
/
parseArgs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import {
split, take, T, equals, tail, compose, reduce, cond, objOf, head, mergeWith,
drop, concat, flip, append, map,
} from 'ramda';
// need to make it an array so we can use concat
// We use concat because user can inputs multiple flags at once
// eg. 'ls -al' -> ['a', 'l']
const toArray = flip(append)([]);
const parseFlag = compose(objOf('flags'), split(''), tail);
const parseOption = compose(objOf('fulls'), toArray, drop(2));
const parseTarget = compose(objOf('targets'), toArray);
const classifyArg = cond([
[compose(equals('--'), take(2)), parseOption],
[compose(equals('-'), head), parseFlag],
[T, parseTarget],
]);
const mergeArg = mergeWith(concat);
const formatArgs = reduce((accum, current) =>
mergeArg(accum, classifyArg(current)),
{fulls: [], flags: [], targets: []}
);
const getArgs = compose(tail, split(' '));
const sortArgs = map(args => args.sort());
/**
* Parse raw command and returns an args object
* 'ls -a -l --lmao' -> {targets: [], flags: ['a', 'l'], fulls: ['lmao']}
*
* @param {String} Command - raw command from user
*
* @returns {obj} - {fulls: [], flags: [], targets: []}
*/
const parseArgs = compose(sortArgs, formatArgs, getArgs);
export default parseArgs;