| 1 | #Region "Microsoft.VisualBasic::2b74c5cb5cf52d05d72b08ee4671c092, Microsoft.VisualBasic.Core\CommandLine\Reflection\ArgumentCollection.vb" |
| 2 | |
| 3 | ' Author: |
| 4 | ' |
| 5 | ' asuka (amethyst.asuka@gcmodeller.org) |
| 6 | ' xie (genetics@smrucc.org) |
| 7 | ' xieguigang (xie.guigang@live.com) |
| 8 | ' |
| 9 | ' Copyright (c) 2018 GPL3 Licensed |
| 10 | ' |
| 11 | ' |
| 12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
| 13 | ' |
| 14 | ' |
| 15 | ' This program is free software: you can redistribute it and/or modify |
| 16 | ' it under the terms of the GNU General Public License as published by |
| 17 | ' the Free Software Foundation, either version 3 of the License, or |
| 18 | ' (at your option) any later version. |
| 19 | ' |
| 20 | ' This program is distributed in the hope that it will be useful, |
| 21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | ' GNU General Public License for more details. |
| 24 | ' |
| 25 | ' You should have received a copy of the GNU General Public License |
| 26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 27 | |
| 28 | |
| 29 | |
| 30 | ' /********************************************************************************/ |
| 31 | |
| 32 | ' Summaries: |
| 33 | |
| 34 | ' Class ArgumentCollection |
| 35 | ' |
| 36 | ' Properties: Count, EmptyExample, EmptyUsage, GetExample, GetUsage |
| 37 | ' |
| 38 | ' Constructor: (+1 Overloads) Sub New |
| 39 | ' Function: GetEnumerator, IEnumerable_GetEnumerator, ToString |
| 40 | ' |
| 41 | ' |
| 42 | ' /********************************************************************************/ |
| 43 | |
| 44 | #End Region |
| 45 | |
| 46 | Imports System.Reflection |
| 47 | Imports System.Runtime.CompilerServices |
| 48 | Imports System.Text |
| 49 | Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel |
| 50 | Imports Microsoft.VisualBasic.Language |
| 51 | |
| 52 | Namespace CommandLine.Reflection |
| 53 | |
| 54 | ''' <summary> |
| 55 | ''' The help information for a specific command line parameter switch.(某一个指定的命令的开关的帮助信息) |
| 56 | ''' </summary> |
| 57 | ''' <remarks></remarks> |
| 58 | Public Class ArgumentCollection : Implements IEnumerable(Of NamedValue(Of Argument)) |
| 59 | |
| 60 | ReadOnly _params As New Dictionary(Of String, Argument) |
| 61 | |
| 62 | ''' <summary> |
| 63 | ''' 本命令行对象中的包含有帮助信息的开关参数的数目 |
| 64 | ''' </summary> |
| 65 | ''' <value></value> |
| 66 | ''' <returns></returns> |
| 67 | ''' <remarks></remarks> |
| 68 | Public ReadOnly Property Count As Integer |
| 69 | Get |
| 70 | Return _params.Count |
| 71 | End Get |
| 72 | End Property |
| 73 | |
| 74 | ''' <summary> |
| 75 | ''' Returns the parameter switch help information with the specific name value.(显示某一个指定名称的开关信息) |
| 76 | ''' </summary> |
| 77 | ''' <param name="Name"></param> |
| 78 | ''' <value></value> |
| 79 | ''' <returns></returns> |
| 80 | ''' <remarks></remarks> |
| 81 | Default Public ReadOnly Property Parameter(Name As String) As String |
| 82 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 83 | Get |
| 84 | Return _params(Name).ToString |
| 85 | End Get |
| 86 | End Property |
| 87 | |
| 88 | ''' <summary> |
| 89 | ''' Gets the usage example of this parameter switch.(获取本参数开关的帮助信息) |
| 90 | ''' </summary> |
| 91 | ''' <value></value> |
| 92 | ''' <returns></returns> |
| 93 | ''' <remarks></remarks> |
| 94 | Public ReadOnly Property GetExample() As String |
| 95 | Get |
| 96 | Dim required = From par As Argument |
| 97 | In Me._params.Values |
| 98 | Where Not par.Optional |
| 99 | Select par |
| 100 | Dim optionals = From par |
| 101 | In Me._params.Values |
| 102 | Where par.Optional |
| 103 | Select par |
| 104 | Dim sb As New StringBuilder(1024) |
| 105 | |
| 106 | For Each Switch As Argument In required |
| 107 | Call sb.AppendFormat("{0} {1} ", Switch.Name, Switch.Example) |
| 108 | Next |
| 109 | For Each Switch As Argument In optionals |
| 110 | Call sb.AppendFormat("[{0} {1}] ", Switch.Name, Switch.Example) |
| 111 | Next |
| 112 | |
| 113 | Return sb.ToString.Trim |
| 114 | End Get |
| 115 | End Property |
| 116 | |
| 117 | Public ReadOnly Property GetUsage() As String |
| 118 | Get |
| 119 | Dim requireds = From par |
| 120 | In Me._params.Values |
| 121 | Where Not par.Optional |
| 122 | Select par |
| 123 | Dim optionals = From par |
| 124 | In Me._params.Values |
| 125 | Where par.Optional |
| 126 | Select par |
| 127 | Dim sb As New StringBuilder(1024) |
| 128 | |
| 129 | For Each param As Argument In requireds |
| 130 | Call sb.AppendFormat("{0} {1} ", param.Name, param.Usage) |
| 131 | Next |
| 132 | For Each param As Argument In optionals |
| 133 | Call sb.AppendFormat("[{0} {1}] ", param.Name, param.Usage) |
| 134 | Next |
| 135 | |
| 136 | Return sb.ToString.Trim |
| 137 | End Get |
| 138 | End Property |
| 139 | |
| 140 | Public ReadOnly Property EmptyUsage As Boolean |
| 141 | Get |
| 142 | Dim LQuery = From arg In _params.Values Where String.IsNullOrEmpty(arg.Usage) Select 1 ' Return LQuery.Sum = _params.Count |
| 143 | End Get |
| 144 | End Property |
| 145 | |
| 146 | Public ReadOnly Property EmptyExample As Boolean |
| 147 | Get |
| 148 | Dim LQuery = From par As Argument |
| 149 | In _params.Values |
| 150 | Where String.IsNullOrEmpty(par.Example) |
| 151 | Select 1 ' Return LQuery.Sum = _params.Count |
| 152 | End Get |
| 153 | End Property |
| 154 | |
| 155 | ''' <summary> |
| 156 | ''' 显示所有的开关信息 |
| 157 | ''' </summary> |
| 158 | ''' <returns></returns> |
| 159 | ''' <remarks></remarks> |
| 160 | Public Overrides Function ToString() As String |
| 161 | Dim sb As New StringBuilder(1024) |
| 162 | |
| 163 | For Each parameter As Argument In _params.Values |
| 164 | Call sb.AppendLine(parameter.ToString) |
| 165 | Next |
| 166 | Return Trim(sb.ToString) |
| 167 | End Function |
| 168 | |
| 169 | ReadOnly __flag As Type = GetType(Argument) |
| 170 | |
| 171 | Sub New(methodInfo As MethodInfo) |
| 172 | Dim attrs() = methodInfo.GetCustomAttributes(__flag, inherit:=False) |
| 173 | Dim LQuery = LinqAPI.Exec(Of Argument) _ |
| 174 | _ |
| 175 | () <= From attr As Object |
| 176 | In attrs |
| 177 | Let parameter As Argument = TryCast(attr, Argument) |
| 178 | Select parameter |
| 179 | Order By parameter.Optional, parameter.TokenType Ascending ' 必须参数都在前面,可选参数都在后面 |
| 180 | |
| 181 | For Each param As Argument In LQuery |
| 182 | Call _params.Add(param.Name, param) |
| 183 | Next |
| 184 | End Sub |
| 185 | |
| 186 | Public Iterator Function GetEnumerator() As IEnumerator(Of NamedValue(Of Argument)) Implements IEnumerable(Of NamedValue(Of Argument)).GetEnumerator |
| 187 | For Each obj In _params |
| 188 | Yield New NamedValue(Of Argument) With { |
| 189 | .Name = obj.Key, |
| 190 | .Value = obj.Value |
| 191 | } |
| 192 | Next |
| 193 | End Function |
| 194 | |
| 195 | Private Iterator Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator |
| 196 | Yield GetEnumerator() |
| 197 | End Function |
| 198 | End Class |
| 199 | End Namespace |