1 #Region "Microsoft.VisualBasic::f5419d3f4252bbbeed3dd101c5d483a3, Microsoft.VisualBasic.Core\CommandLine\Reflection\EntryPoints\Delegate.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 APIDelegate
35     
36     '         Properties: Example, Info, Name, Usage
37     
38     '         Constructor: (+2 OverloadsSub New
39     '         Function: Execute, HelpInformation, ToString
40     
41     
42     ' /********************************************************************************/
43
44 #End Region
45
46 Imports System.Reflection
47 Imports System.Text
48 Imports Microsoft.VisualBasic.CommandLine.ManView
49 Imports Microsoft.VisualBasic.ComponentModel
50 Imports Microsoft.VisualBasic.Text
51
52 Namespace CommandLine.Reflection.EntryPoints
53
54     Public Class APIDelegate : Implements IExportAPI
55
56         Protected _NumberOfParameters As Integer
57         Protected _metaData As Binding(Of ExportAPIAttribute, MethodInfo)
58         Protected __funcInvoker As Func(Of Object(), Integer)
59
60         ''' <summary>
61         ''' The usage name of this command line entry point.(本命令行对象的调用命令名称)
62         ''' </summary>
63         ''' <value></value>
64         ''' <returns></returns>
65         ''' <remarks></remarks>
66         Public ReadOnly Property Name() As String Implements IExportAPI.Name
67             Get
68                 Return _metaData.Bind.Name
69             End Get
70         End Property
71
72         Public ReadOnly Property Info() As String Implements IExportAPI.Info
73             Get
74                 Return _metaData.Bind.Info
75             End Get
76         End Property
77
78         Public ReadOnly Property Usage() As String Implements IExportAPI.Usage
79             Get
80                 Return _metaData.Bind.Usage
81             End Get
82         End Property
83
84         Public ReadOnly Property Example() As String Implements IExportAPI.Example
85             Get
86                 Return _metaData.Bind.Example
87             End Get
88         End Property
89
90         ''' <summary>
91         ''' 不可以使用本方法初始化目标对象为实例方法的类型
92         ''' </summary>
93         ''' <param name="attribute"></param>
94         ''' <param name="Invoke"></param>
95         ''' <remarks></remarks>
96         Public Sub New(attribute As Binding(Of ExportAPIAttribute, MethodInfo), [Invoke] As Func(Of Object(), Integer))
97             _metaData = attribute
98             __funcInvoker = Invoke
99             _metaData = attribute
100             _NumberOfParameters = 32
101         End Sub
102
103         Protected Sub New()
104         End Sub
105
106         Public Overridable Function HelpInformation(Optional md As Boolean = FalseAs String
107             Dim sb As New StringBuilder(1024)
108
109             If md Then
110                 sb.AppendLine($"{Name}</h3>")
111             Else
112                 sb.AppendLine($"Help for command ""{Name}"":")
113             End If
114
115             Call sb.AppendLine()
116
117             If md Then
118                 Dim prototype$ = APIPrototype(_metaData.Target.GetFullName)
119
120                 Call sb.AppendLine(Info)
121                 Call sb.AppendLine()
122                 Call sb.AppendLine($"**Prototype**: ``{prototype}``")
123                 Call sb.AppendLine()
124                 Call sb.AppendLine("###### Usage")
125                 Call sb.AppendLine()
126                 Call sb.AppendLine("```bash")
127                 Call sb.AppendLine($"{App.AssemblyName} {Usage}")
128                 Call sb.AppendLine("```")
129
130                 If Not String.IsNullOrEmpty(Example) Then
131                     Call sb.AppendLine("###### Example")
132                     Call sb.AppendLine("```bash")
133                     Call sb.AppendLine($"{App.AssemblyName} {Example}")
134                     Call sb.AppendLine("```")
135                 End If
136             Else
137                 Dim infoLines$() = Paragraph.SplitParagraph(Info, 90).ToArray
138
139                 sb.AppendLine(String.Format("  Information:  {0}", infoLines.FirstOrDefault))
140
141                 If infoLines.Length > 1 Then
142                     For Each line$ In infoLines.Skip(1)
143                         Call sb.AppendLine($"                {line}")
144                     Next
145                 End If
146
147                 sb.AppendLine(String.Format("  Usage:        {0} {1}", Application.ExecutablePath, Usage))
148                 sb.AppendLine(String.Format("  Example:      {0} {1}", App.AssemblyName, Example))
149             End If
150
151             Return sb.ToString
152         End Function
153
154         ''' <summary>
155         ''' 
156         ''' </summary>
157         ''' <param name="parameters">数组的长度必须与目标函数的参数的数目一致,否则短于目标函数的参数的数目的数组会使用Nothing来填充缺少的部分,而多于目标函数的参数会被截断</param>
158         ''' <returns></returns>
159         ''' <remarks></remarks>
160         Public Function Execute(parameters As Object()) As Integer
161             Dim callParameters() As Object
162
163             If parameters.Length < _NumberOfParameters Then
164                 callParameters = New Object(_NumberOfParameters - 1) {}
165                 Call parameters.CopyTo(callParameters, 0)
166             ElseIf parameters.Length > _NumberOfParameters Then
167                 callParameters = New Object(_NumberOfParameters - 1) {}
168                 Call Array.ConstrainedCopy(parameters, 0, callParameters, 0, _NumberOfParameters)
169             Else
170                 callParameters = parameters
171             End If
172
173             Return __funcInvoker.Invoke(callParameters)
174         End Function
175
176         Public Overrides Function ToString() As String
177             Return Name
178         End Function
179     End Class
180 End Namespace