forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargumentsHelper.ts
More file actions
124 lines (121 loc) · 4.82 KB
/
argumentsHelper.ts
File metadata and controls
124 lines (121 loc) · 4.82 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { injectable } from 'inversify';
import { traceWarning } from '../../common/logger';
import { IArgumentsHelper } from '../types';
@injectable()
export class ArgumentsHelper implements IArgumentsHelper {
public getOptionValues(args: string[], option: string): string | string[] | undefined {
const values: string[] = [];
let returnNextValue = false;
for (const arg of args) {
if (returnNextValue) {
values.push(arg);
returnNextValue = false;
continue;
}
if (arg.startsWith(`${option}=`)) {
values.push(arg.substring(`${option}=`.length));
continue;
}
if (arg === option) {
returnNextValue = true;
}
}
switch (values.length) {
case 0: {
return;
}
case 1: {
return values[0];
}
default: {
return values;
}
}
}
public getPositionalArguments(
args: string[],
optionsWithArguments: string[] = [],
optionsWithoutArguments: string[] = []
): string[] {
const nonPositionalIndexes: number[] = [];
args.forEach((arg, index) => {
if (optionsWithoutArguments.indexOf(arg) !== -1) {
nonPositionalIndexes.push(index);
return;
} 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);
return;
} else if (arg.startsWith('-')) {
// Ok this is an unknown option, lets treat this as one without values.
traceWarning(
`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;
} else if (arg.indexOf('=') > 0) {
// Ok this is an unknown option with a value
traceWarning(
`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);
}
public filterArguments(
args: string[],
optionsWithArguments: string[] = [],
optionsWithoutArguments: string[] = []
): string[] {
let ignoreIndex = -1;
return args.filter((arg, index) => {
if (ignoreIndex === index) {
return false;
}
// Options can use willd 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 ineline 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 ineline 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;
});
}
}