Repository navigation
Expand file tree
/
Copy pathParserExtensions.cs
More file actions
177 lines (138 loc) · 7.66 KB
/
Copy pathParserExtensions.cs
File metadata and controls
177 lines (138 loc) · 7.66 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using CommandLine;
// Include this file in your project.
namespace gitRelease
{
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public sealed class ChildVerbsAttribute : Attribute
{
public Type[] Types { get; }
public ChildVerbsAttribute(params Type[] types)
{
Types = types;
}
}
/// <summary>
/// Extension methods to allow multi level verb parsing.
/// </summary>
public static class ParserVerbExtensions
{
public static ParserResult<object> ParseVerbs(this Parser parser, IEnumerable<string> args, params Type[] types)
{
if (parser == null) throw new ArgumentNullException(nameof(parser));
var argsArray = args as string[] ?? args.ToArray();
if (argsArray.Length == 0 || argsArray[0].StartsWith("-"))
{
return parser.ParseArguments(argsArray, types);
}
var verb = argsArray[0];
foreach (var type in types)
{
var verbAttribute = type.GetCustomAttribute<VerbAttribute>();
if (verbAttribute == null || verbAttribute.Name != verb)
{
continue;
}
var subVerbsAttribute = type.GetCustomAttribute<ChildVerbsAttribute>();
if (subVerbsAttribute != null)
{
return ParseVerbs(parser, argsArray.Skip(1).ToArray(), subVerbsAttribute.Types);
}
break;
}
return parser.ParseArguments(argsArray, types);
}
public static ParserResult<object> ParseVerbs<T1, T2>(this Parser parser, IEnumerable<string> args)
{
if (parser == null) throw new ArgumentNullException(nameof(parser));
return parser.ParseVerbs(args, typeof(T1), typeof(T2));
}
public static ParserResult<object> ParseVerbs<T1, T2, T3>(this Parser parser, IEnumerable<string> args)
{
if (parser == null) throw new ArgumentNullException(nameof(parser));
return parser.ParseVerbs(args, typeof(T1), typeof(T2), typeof(T3));
}
public static ParserResult<object> ParseVerbs<T1, T2, T3, T4>(this Parser parser, IEnumerable<string> args)
{
if (parser == null) throw new ArgumentNullException(nameof(parser));
return parser.ParseVerbs(args, typeof(T1), typeof(T2), typeof(T3), typeof(T4));
}
public static ParserResult<object> ParseVerbs<T1, T2, T3, T4, T5>(this Parser parser,
IEnumerable<string> args)
{
if (parser == null) throw new ArgumentNullException(nameof(parser));
return parser.ParseVerbs(args, typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5));
}
public static ParserResult<object> ParseVerbs<T1, T2, T3, T4, T5, T6>(this Parser parser,
IEnumerable<string> args)
{
if (parser == null) throw new ArgumentNullException(nameof(parser));
return parser.ParseVerbs(args, typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6));
}
public static ParserResult<object> ParseVerbs<T1, T2, T3, T4, T5, T6, T7>(this Parser parser,
IEnumerable<string> args)
{
if (parser == null) throw new ArgumentNullException(nameof(parser));
return parser.ParseVerbs(args, typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7));
}
public static ParserResult<object> ParseVerbs<T1, T2, T3, T4, T5, T6, T7, T8>(this Parser parser,
IEnumerable<string> args)
{
if (parser == null) throw new ArgumentNullException(nameof(parser));
return parser.ParseVerbs(args, typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8));
}
public static ParserResult<object> ParseVerbs<T1, T2, T3, T4, T5, T6, T7, T8, T9>(this Parser parser,
IEnumerable<string> args)
{
if (parser == null) throw new ArgumentNullException(nameof(parser));
return parser.ParseVerbs(args, typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9));
}
public static ParserResult<object> ParseVerbs<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(this Parser parser,
IEnumerable<string> args)
{
if (parser == null) throw new ArgumentNullException(nameof(parser));
return parser.ParseVerbs(args, typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10));
}
public static ParserResult<object> ParseVerbs<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>(
this Parser parser, IEnumerable<string> args)
{
if (parser == null) throw new ArgumentNullException(nameof(parser));
return parser.ParseVerbs(args, typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10), typeof(T11));
}
public static ParserResult<object> ParseVerbs<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(
this Parser parser, IEnumerable<string> args)
{
if (parser == null) throw new ArgumentNullException(nameof(parser));
return parser.ParseVerbs(args, typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10), typeof(T11), typeof(T12));
}
public static ParserResult<object> ParseVerbs<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13>(
this Parser parser, IEnumerable<string> args)
{
if (parser == null) throw new ArgumentNullException(nameof(parser));
return parser.ParseVerbs(args, typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10), typeof(T11), typeof(T12), typeof(T13));
}
public static ParserResult<object> ParseVerbs<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14>(
this Parser parser, IEnumerable<string> args)
{
if (parser == null) throw new ArgumentNullException(nameof(parser));
return parser.ParseVerbs(args, typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10), typeof(T11), typeof(T12), typeof(T13), typeof(T14));
}
public static ParserResult<object> ParseVerbs
<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15>(this Parser parser,
IEnumerable<string> args)
{
if (parser == null) throw new ArgumentNullException(nameof(parser));
return parser.ParseVerbs(args, typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10), typeof(T11), typeof(T12), typeof(T13), typeof(T14), typeof(T15));
}
public static ParserResult<object> ParseVerbs
<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>(this Parser parser,
IEnumerable<string> args)
{
if (parser == null) throw new ArgumentNullException(nameof(parser));
return parser.ParseVerbs(args, typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10), typeof(T11), typeof(T12), typeof(T13), typeof(T14), typeof(T15), typeof(T16));
}
}
}