-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlag.cs
120 lines (99 loc) · 3.41 KB
/
Flag.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
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
namespace SafeFolder;
/*
* Borrowed from UniChain CLI:
* https://github.com/Agentew04/UniChain
*/
public class Flag {
/// <summary>
/// The simplified flag name
/// </summary>
public string Simplified { get; init; }
/// <summary>
/// The full flag name
/// </summary>
public string Full { get; init; }
/// <summary>
/// If the flag has a value associated with it
/// </summary>
public bool HasValue { get; init; }
/// <summary>
/// When the Flag has value, if the value can be empty(<see cref="string.Empty"></see>)
/// </summary>
public bool CanBeEmpty { get; init; }
/// <summary>
/// The value of the flag, null if <see cref="HasValue"/> is false
/// </summary>
public string? Value { get; set; }
public Flag(string fullname, string simpleName, bool hasValue = false, bool canBeEmpty = false, string? value = null) {
Simplified = simpleName;
Full = fullname;
HasValue = hasValue;
CanBeEmpty = canBeEmpty;
Value = value;
}
public static bool TryGetFlagValue(string[] args, Flag flag, out string value) {
value = "";
if (!flag.HasValue)
return false;
if (args.Length == 0)
return false;
if (!args.Any(x => x=="--"+flag.Full || x=="-"+flag.Simplified))
return false;
int flagIndex = -1;
for (var i = 0; i < args.Length; i++) {
if (args[i] != "-"+flag.Simplified && args[i] != "--"+flag.Full) continue;
flagIndex = i;
break;
}
if (flagIndex == args.Length - 1)
return false;
// flag not found
if (flagIndex == -1)
return false;
value = args[flagIndex + 1];
// check if value is not other flag( has no value)
return !value.StartsWith("-") && !value.StartsWith("--");
}
public static List<string> GetStrayArgs(string[] args, IEnumerable<Flag> flags) {
List<string> argsList = args.ToList();
List<string> strays = new();
for (var i = 0; i < argsList.Count; i++) {
// check if current is flag
string arg = argsList[i];
if (IsFlag(arg)) {
Flag? flag = flags.FirstOrDefault(x => arg == "--"+x.Full || arg == "-"+x.Simplified);
if (flag is null)
throw new FlagException($"Flag '{arg}' does not exist!");
if (flag.HasValue) {
argsList.RemoveAt(i+1);
}
argsList.RemoveAt(i);
i--;
}
else {
strays.Add(argsList[i]);
}
}
return strays;
}
public static bool HasFlag(string[] args, Flag flag) {
if (args.Length == 0)
return false;
return args.Any(x => IsFlag(x) && x == "--"+flag.Full || x=="-"+flag.Simplified);
}
private static bool IsFlag(string arg) {
return arg.StartsWith("-");
}
public Flag Clone() {
return new Flag(Full, Simplified, HasValue, CanBeEmpty, Value);
}
}
[Serializable]
public class FlagException : Exception {
protected FlagException(SerializationInfo info, StreamingContext ctx) : base(info, ctx){}
public FlagException(string message) : base(message){}
}