-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathCommandLine.cs
109 lines (96 loc) · 3.7 KB
/
CommandLine.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
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
//-----------------------------------------------------------------------
// <copyright file="CommandLine.cs" company="Akka.NET Project">
// Copyright (C) 2009-2021 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2021 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using Akka.Configuration;
namespace Akka.Remote.TestKit
{
//TODO: Needs some work
/// <summary>
/// Command line argument parser for individual node tests during a <see cref="MultiNodeSpec"/>.
///
/// Parses arguments from <see cref="Environment.GetCommandLineArgs"/> using the same conventions as canonical Akka.
///
/// For example (from the Akka.NodeTestRunner source):
/// <code>
/// var nodeIndex = CommandLine.GetInt32("multinode.index");
/// var assemblyName = CommandLine.GetProperty("multinode.test-assembly");
/// var typeName = CommandLine.GetProperty("multinode.test-class");
/// var testName = CommandLine.GetProperty("multinode.test-method");
/// </code>
/// </summary>
public static class CommandLine
{
private static readonly StringDictionary Values = new StringDictionary();
static CommandLine()
{
Initialize(Environment.GetCommandLineArgs());
}
public static void Initialize(string[] args)
{
// Detect and fix PowerShell command line input.
// PowerShell splits command line arguments on '.'
var fixedArgs = new List<string>();
for (var i = 1; i < args.Length - 1; ++i)
{
if (args[i].Equals("-Dmultinode") && args[i + 1].StartsWith("."))
{
fixedArgs.Add(args[i] + args[i+1]);
++i;
}
}
if(fixedArgs.Count == 0)
fixedArgs.AddRange(args);
foreach (var arg in fixedArgs)
{
if (!arg.StartsWith("-D"))
{
var a = arg.Trim().ToLowerInvariant();
if (a.Equals("-h") || a.Equals("--help"))
{
ShowHelp = true;
return;
}
if (a.Equals("-v") || a.Equals("--version"))
{
ShowVersion = true;
return;
}
continue;
}
var tokens = arg.Substring(2).Split('=');
if (tokens.Length == 2)
{
Values[tokens[0]] = tokens[1].Trim().Trim('"');
}
else
{
throw new ConfigurationException($"Command line parameter '{arg}' should follow the pattern [-Dmultinode.<key>=<value>].");
}
}
}
public static bool ShowHelp { get; private set; }
public static bool ShowVersion { get; private set; }
public static string GetProperty(string key)
{
return Values[key];
}
public static string GetPropertyOrDefault(string key, string defaultStr)
{
return Values.ContainsKey(key) ? Values[key] : defaultStr;
}
public static int GetInt32(string key)
{
return Convert.ToInt32(GetProperty(key));
}
public static int GetInt32OrDefault(string key, int defaultInt)
{
return Values.ContainsKey(key) ? GetInt32(key) : defaultInt;
}
}
}