Skip to content

Add provided set to cli parser #1273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cli/util/options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ interface Result {
/** Normal arguments. */
arguments: string[],
/** Trailing arguments. */
trailing: string[]
trailing: string[],
/** Provided arguments from the cli. */
provided: Set<string>
}

/** Parses the specified command line arguments according to the given configuration. */
Expand Down
11 changes: 8 additions & 3 deletions cli/util/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function parse(argv, config) {
var unknown = [];
var arguments = [];
var trailing = [];
var provided = new Set();

// make an alias map and initialize defaults
var aliases = {};
Expand Down Expand Up @@ -53,9 +54,13 @@ function parse(argv, config) {
else { arguments.push(arg); continue; } // argument
}
if (option) {
if (option.type == null || option.type === "b") options[key] = true; // flag
else {
if (option.type == null || option.type === "b") {
options[key] = true; // flag
provided.add(key);
} else {
// the argument was provided
if (i + 1 < argv.length && argv[i + 1].charCodeAt(0) != 45) { // present
provided.add(key);
switch (option.type) {
case "i": options[key] = parseInt(argv[++i], 10); break;
case "I": options[key] = (options[key] || []).concat(parseInt(argv[++i], 10)); break;
Expand All @@ -82,7 +87,7 @@ function parse(argv, config) {
}
while (i < k) trailing.push(argv[i++]); // trailing

return { options, unknown, arguments, trailing };
return { options, unknown, arguments, trailing, provided };
}

exports.parse = parse;
Expand Down