forked from microsoft/component-detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArgumentHelper.cs
50 lines (38 loc) · 1.39 KB
/
ArgumentHelper.cs
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
namespace Microsoft.ComponentDetection.Orchestrator;
using System.Collections.Generic;
using System.Linq;
using CommandLine;
using Microsoft.ComponentDetection.Orchestrator.Commands;
public class ArgumentHelper : IArgumentHelper
{
private readonly IEnumerable<ScanSettings> argumentSets;
public ArgumentHelper(IEnumerable<ScanSettings> argumentSets) => this.argumentSets = argumentSets;
public static IDictionary<string, string> GetDetectorArgs(IEnumerable<string> detectorArgsList)
{
var detectorArgs = new Dictionary<string, string>();
foreach (var arg in detectorArgsList)
{
var keyValue = arg.Split('=');
if (keyValue.Length != 2)
{
continue;
}
detectorArgs.Add(keyValue[0], keyValue[1]);
}
return detectorArgs;
}
public ParserResult<object> ParseArguments(string[] args)
{
return Parser.Default.ParseArguments(args, this.argumentSets.Select(x => x.GetType()).ToArray());
}
public ParserResult<T> ParseArguments<T>(string[] args, bool ignoreInvalidArgs = false)
{
var p = new Parser(x =>
{
x.IgnoreUnknownArguments = ignoreInvalidArgs;
// This is not the main argument dispatch, so we don't want console output.
x.HelpWriter = null;
});
return p.ParseArguments<T>(args);
}
}