Skip to content
Prev Previous commit
Next Next commit
chore: add more comments
  • Loading branch information
Jessica Nahulan authored and Jessica Nahulan committed Sep 28, 2021
commit 8f2935360d8bb73b61ea11c995b7784ec05d3c98
11 changes: 7 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,26 @@ const parseArgs = (
arg = arg.replace(/^-+/, '')

if (arg.includes('=')) {
//withValue equals(=) case
const argParts = arg.split('=')

result.args[argParts[0]] = true
//If withValue option isn't specified, set value as undefined
//If withValue option is specified, take 2nd part after '=' as value, else set value as undefined
const val = options.withValue && options.withValue.includes(argParts[0]) ? argParts[1] : undefined
//Append value to previous arg values array for case of multiples option, else add to empty array
result.values[argParts[0]] = [...(options.multiples && options.multiples.includes(argParts[0]) && result.values[argParts[0]] ? result.values[argParts[0]] : []), val]
Comment thread
JessNah marked this conversation as resolved.
Outdated
} else if (pos + 1 < argv.length) {
} else if (pos + 1 < argv.length && !argv[pos+1].startsWith('-')) {
//withValue option should also support setting values when '=' isn't used
//ie. both --foo=bar and --foo bar should work
//ie. both --foo=b and --foo b should work

result.args[arg] = true
//If withValue option isn't specified, set value as undefined
//If withValue option is specified, take next position arguement as value and then increment pos so that we don't re-evaluate that arg, else set value as undefined
//ie. --foo b --bar c, after setting b as the value for foo, evaluate --bar next and skip 'b'
const val = options.withValue && options.withValue.includes(arg) ? argv[++pos] : undefined
//Append value to previous arg values array for case of multiples option, else add to empty array
result.values[arg] = [...(options.multiples && options.multiples.includes(arg) && result.values[arg] ? result.values[arg] : []), val]
Comment thread
ljharb marked this conversation as resolved.
Outdated
} else {
//cases when an arg is specified without a value, example '--foo --bar' <- 'foo' and 'bar' args should be set to true and have value as undefined
result.args[arg] = true
//Append undefined to previous arg values array for case of multiples option, else add to empty array
result.values[arg] = [...(options.multiples && options.multiples.includes(arg) && result.values[arg] ? result.values[arg] : []), undefined]
Comment thread
ljharb marked this conversation as resolved.
Outdated
Expand Down