forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathargumentsHelper.ts
More file actions
86 lines (83 loc) · 3.45 KB
/
argumentsHelper.ts
File metadata and controls
86 lines (83 loc) · 3.45 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { traceWarn } from '../../../logging';
export function getPositionalArguments(
args: string[],
optionsWithArguments: string[] = [],
optionsWithoutArguments: string[] = [],
): string[] {
const nonPositionalIndexes: number[] = [];
args.forEach((arg, index) => {
if (optionsWithoutArguments.indexOf(arg) !== -1) {
nonPositionalIndexes.push(index);
} else if (optionsWithArguments.indexOf(arg) !== -1) {
nonPositionalIndexes.push(index);
// Cuz the next item is the value.
nonPositionalIndexes.push(index + 1);
} else if (optionsWithArguments.findIndex((item) => arg.startsWith(`${item}=`)) !== -1) {
nonPositionalIndexes.push(index);
} else if (arg.startsWith('-')) {
// Ok this is an unknown option, lets treat this as one without values.
traceWarn(
`Unknown command line option passed into args parser for tests '${arg}'. Please report on https://github.com/Microsoft/vscode-python/issues/new`,
);
nonPositionalIndexes.push(index);
} else if (arg.indexOf('=') > 0) {
// Ok this is an unknown option with a value
traceWarn(
`Unknown command line option passed into args parser for tests '${arg}'. Please report on https://github.com/Microsoft/vscode-python/issues/new`,
);
nonPositionalIndexes.push(index);
}
});
return args.filter((_, index) => nonPositionalIndexes.indexOf(index) === -1);
}
export function filterArguments(
args: string[],
optionsWithArguments: string[] = [],
optionsWithoutArguments: string[] = [],
): string[] {
let ignoreIndex = -1;
return args.filter((arg, index) => {
if (ignoreIndex === index) {
return false;
}
// Options can use wild cards (with trailing '*')
if (
optionsWithoutArguments.indexOf(arg) >= 0 ||
optionsWithoutArguments.filter((option) => option.endsWith('*') && arg.startsWith(option.slice(0, -1)))
.length > 0
) {
return false;
}
// Ignore args that match exactly.
if (optionsWithArguments.indexOf(arg) >= 0) {
ignoreIndex = index + 1;
return false;
}
// Ignore args that match exactly with wild cards & do not have inline values.
if (optionsWithArguments.filter((option) => arg.startsWith(`${option}=`)).length > 0) {
return false;
}
// Ignore args that match a wild card (ending with *) and no inline values.
// Eg. arg='--log-cli-level' and optionsArguments=['--log-*']
if (
arg.indexOf('=') === -1 &&
optionsWithoutArguments.filter((option) => option.endsWith('*') && arg.startsWith(option.slice(0, -1)))
.length > 0
) {
ignoreIndex = index + 1;
return false;
}
// Ignore args that match a wild card (ending with *) and have inline values.
// Eg. arg='--log-cli-level=XYZ' and optionsArguments=['--log-*']
if (
arg.indexOf('=') >= 0 &&
optionsWithoutArguments.filter((option) => option.endsWith('*') && arg.startsWith(option.slice(0, -1)))
.length > 0
) {
return false;
}
return true;
});
}