forked from commandlineparser/commandline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVerb.cs
68 lines (58 loc) · 1.96 KB
/
Verb.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
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace CommandLine.Core
{
sealed class Verb
{
private readonly string name;
private readonly string helpText;
private readonly bool hidden;
private readonly bool isDefault;
public Verb(string name, string helpText, bool hidden = false, bool isDefault = false)
{
if ( string.IsNullOrWhiteSpace(name))
throw new ArgumentNullException(nameof(name));
this.name = name;
this.helpText = helpText ?? throw new ArgumentNullException(nameof(helpText));
this.hidden = hidden;
this.isDefault = isDefault;
}
public string Name
{
get { return name; }
}
public string HelpText
{
get { return helpText; }
}
public bool Hidden
{
get { return hidden; }
}
public bool IsDefault
{
get => isDefault;
}
public static Verb FromAttribute(VerbAttribute attribute)
{
return new Verb(
attribute.Name,
attribute.HelpText,
attribute.Hidden,
attribute.IsDefault
);
}
public static IEnumerable<Tuple<Verb, Type>> SelectFromTypes(IEnumerable<Type> types)
{
return from type in types
let attrs = type.GetTypeInfo().GetCustomAttributes(typeof(VerbAttribute), true).ToArray()
where attrs.Length == 1
select Tuple.Create(
FromAttribute((VerbAttribute)attrs.Single()),
type);
}
}
}