|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +import { inject, injectable } from 'inversify'; |
| 7 | +import { ILogger } from '../../common/types'; |
| 8 | +import { IServiceContainer } from '../../ioc/types'; |
| 9 | +import { IArgumentsHelper } from '../types'; |
| 10 | + |
| 11 | +@injectable() |
| 12 | +export class ArgumentsHelper implements IArgumentsHelper { |
| 13 | + private readonly logger: ILogger; |
| 14 | + constructor(@inject(IServiceContainer) serviceContainer: IServiceContainer) { |
| 15 | + this.logger = serviceContainer.get<ILogger>(ILogger); |
| 16 | + } |
| 17 | + public getOptionValues(args: string[], option: string): string | string[] | undefined { |
| 18 | + const values: string[] = []; |
| 19 | + let returnNextValue = false; |
| 20 | + for (const arg of args) { |
| 21 | + if (returnNextValue) { |
| 22 | + values.push(arg); |
| 23 | + returnNextValue = false; |
| 24 | + continue; |
| 25 | + } |
| 26 | + if (arg.startsWith(`${option}=`)) { |
| 27 | + values.push(arg.substring(`${option}=`.length)); |
| 28 | + continue; |
| 29 | + } |
| 30 | + if (arg === option) { |
| 31 | + returnNextValue = true; |
| 32 | + } |
| 33 | + } |
| 34 | + switch (values.length) { |
| 35 | + case 0: { |
| 36 | + return; |
| 37 | + } |
| 38 | + case 1: { |
| 39 | + return values[0]; |
| 40 | + } |
| 41 | + default: { |
| 42 | + return values; |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + public getPositionalArguments(args: string[], optionsWithArguments: string[] = [], optionsWithoutArguments: string[] = []): string[] { |
| 47 | + let lastIndexOfOption = -1; |
| 48 | + args.forEach((arg, index) => { |
| 49 | + if (optionsWithoutArguments.indexOf(arg) !== -1) { |
| 50 | + lastIndexOfOption = index; |
| 51 | + return; |
| 52 | + } else if (optionsWithArguments.indexOf(arg) !== -1) { |
| 53 | + // Cuz the next item is the value. |
| 54 | + lastIndexOfOption = index + 1; |
| 55 | + } else if (optionsWithArguments.findIndex(item => arg.startsWith(`${item}=`)) !== -1) { |
| 56 | + lastIndexOfOption = index; |
| 57 | + return; |
| 58 | + } else if (arg.startsWith('-')) { |
| 59 | + // Ok this is an unknown option, lets treat this as one without values. |
| 60 | + this.logger.logWarning(`Unknown command line option passed into args parser for tests '${arg}'. Please report on https://github.com/Microsoft/vscode-python/issues/new`); |
| 61 | + lastIndexOfOption = index; |
| 62 | + return; |
| 63 | + } else if (args.indexOf('=') > 0) { |
| 64 | + // Ok this is an unknown option with a value |
| 65 | + this.logger.logWarning(`Unknown command line option passed into args parser for tests '${arg}'. Please report on https://github.com/Microsoft/vscode-python/issues/new`); |
| 66 | + lastIndexOfOption = index; |
| 67 | + } |
| 68 | + }); |
| 69 | + return args.slice(lastIndexOfOption + 1); |
| 70 | + } |
| 71 | + public filterArguments(args: string[], optionsWithArguments: string[] = [], optionsWithoutArguments: string[] = []): string[] { |
| 72 | + let ignoreIndex = -1; |
| 73 | + return args.filter((arg, index) => { |
| 74 | + if (ignoreIndex === index) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + // Options can use willd cards (with trailing '*') |
| 78 | + if (optionsWithoutArguments.indexOf(arg) >= 0 || |
| 79 | + optionsWithoutArguments.filter(option => option.endsWith('*') && arg.startsWith(option.slice(0, -1))).length > 0) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + // Ignore args that match exactly. |
| 83 | + if (optionsWithArguments.indexOf(arg) >= 0) { |
| 84 | + ignoreIndex = index + 1; |
| 85 | + return false; |
| 86 | + } |
| 87 | + // Ignore args that match exactly with wild cards & do not have inline values. |
| 88 | + if (optionsWithArguments.filter(option => arg.startsWith(`${option}=`)).length > 0) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + // Ignore args that match a wild card (ending with *) and no ineline values. |
| 92 | + // Eg. arg='--log-cli-level' and optionsArguments=['--log-*'] |
| 93 | + if (arg.indexOf('=') === -1 && optionsWithoutArguments.filter(option => option.endsWith('*') && arg.startsWith(option.slice(0, -1))).length > 0) { |
| 94 | + ignoreIndex = index + 1; |
| 95 | + return false; |
| 96 | + } |
| 97 | + // Ignore args that match a wild card (ending with *) and have ineline values. |
| 98 | + // Eg. arg='--log-cli-level=XYZ' and optionsArguments=['--log-*'] |
| 99 | + if (arg.indexOf('=') >= 0 && optionsWithoutArguments.filter(option => option.endsWith('*') && arg.startsWith(option.slice(0, -1))).length > 0) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + return true; |
| 103 | + }); |
| 104 | + } |
| 105 | +} |
0 commit comments