[SerializableAttribute]
[XmlRootAttribute("CommandLineValueParameterCollection")]
public class CommandLineValueParameterCollection : Collection<CommandLineValueParameter<IConvertible>>
<SerializableAttribute>
<XmlRootAttribute("CommandLineValueParameterCollection")>
Public Class CommandLineValueParameterCollection
Inherits Collection(Of CommandLineValueParameter(Of IConvertible))
Dim instance As CommandLineValueParameterCollection
[SerializableAttribute]
[XmlRootAttribute(L"CommandLineValueParameterCollection")]
public ref class CommandLineValueParameterCollection : public Collection<CommandLineValueParameter<IConvertible^>^>
[<SerializableAttribute>]
[<XmlRootAttribute("CommandLineValueParameterCollection")>]
type CommandLineValueParameterCollection =
class
inherit Collection<CommandLineValueParameter<IConvertible>>
end
No code example is currently available or this language may not be supported.
No code example is currently available or this language may not be supported.
Module Module1
Dim params As New CommandLineValueParameterCollection From {
New CommandLineValueParameter(Of String)("ParameterName1") With {
.Prefix = CommandLineParameterPrefix.Slash,
.Suffix = CommandLineParameterSuffix.EqualsSign,
.ShortName = "Param1",
.DefaultValue = "",
.IsOptional = False
},
New CommandLineValueParameter(Of Boolean)("ParameterName2") With {
.Prefix = CommandLineParameterPrefix.Slash,
.Suffix = CommandLineParameterSuffix.EqualsSign,
.ShortName = "Param2",
.DefaultValue = False,
.IsOptional = True
},
New CommandLineValueParameter(Of Integer)("ParameterName3") With {
.Prefix = CommandLineParameterPrefix.Slash,
.Suffix = CommandLineParameterSuffix.EqualsSign,
.ShortName = "Param3",
.DefaultValue = -1I,
.IsOptional = True
}
}
Public Function IsParameterAssignedInArgument(Of T As IConvertible)(ByVal parameter As CommandLineValueParameter(Of T), ByVal argument As String) As Boolean
Return (argument.StartsWith(parameter.FullName & parameter.GetSuffixChar(), StringComparison.OrdinalIgnoreCase) OrElse
argument.StartsWith(parameter.FullShortName & parameter.GetSuffixChar(), StringComparison.OrdinalIgnoreCase))
End Function
Public Sub SetParameterValue(Of T As IConvertible)(ByRef parameter As CommandLineValueParameter(Of T), ByVal argument As String)
If IsParameterAssignedInArgument(parameter, argument) Then
Dim value As String = argument.Substring(argument.IndexOf(parameter.GetSuffixChar()) + 1).Trim(ControlChars.Quote)
If String.IsNullOrEmpty(value) Then
parameter.Value = parameter.DefaultValue
Else
Try
parameter.Value = DirectCast(Convert.ChangeType(value, parameter.DefaultValue.GetType()), T)
Catch ex As InvalidCastException
Throw
Catch ex As FormatException
Throw
End Try
End If
End If
End Sub
Sub Main()
' Parse command-line arguments to set the values of the parameters.
ParseArguments(params, AddressOf OnSyntaxError, AddressOf OnMissingParameterRequired)
' Display the parameters and each value assigned.
For Each param As CommandLineValueParameter(Of IConvertible) In params
Console.WriteLine(param.ToString())
Next
End Sub
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Loop through all the command-line arguments of this application.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <param name="cmds">
''' The commandline parameters.
''' </param>
'''
''' <param name="callbackSyntaxError">
''' An encapsulated method that is invoked if a syntax error is found in one of the arguments.
''' </param>
'''
''' <param name="callbackMissingRequired">
''' An encapsulated method that is invoked if a required parameter is missing in the arguments.
''' </param>
''' ----------------------------------------------------------------------------------------------------
Friend Sub ParseArguments(ByVal cmds As CommandLineValueParameterCollection,
ByVal callbackSyntaxError As Action(Of CommandLineValueParameter(Of IConvertible)),
ByVal callbackMissingRequired As Action(Of CommandLineValueParameter(Of IConvertible)))
ParseArguments(cmds, Environment.GetCommandLineArgs.Skip(1), callbackSyntaxError, callbackMissingRequired)
End Sub
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Loop through all the command-line arguments of this application.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <param name="cmds">
''' The commandline parameters.
''' </param>
'''
''' <param name="args">
''' The collection of commandline arguments to examine.
''' </param>
'''
''' <param name="callbackSyntaxError">
''' An encapsulated method that is invoked if a syntax error is found in one of the arguments.
''' </param>
'''
''' <param name="callbackMissingRequired">
''' An encapsulated method that is invoked if a required parameter is missing in the arguments.
''' </param>
''' ----------------------------------------------------------------------------------------------------
Friend Sub ParseArguments(ByVal cmds As CommandLineValueParameterCollection,
ByVal args As IEnumerable(Of String),
ByVal callbackSyntaxError As Action(Of CommandLineValueParameter(Of IConvertible)),
ByVal callbackMissingRequired As Action(Of CommandLineValueParameter(Of IConvertible)))
If Not (args.Any()) Then
PrintHelp()
End If
' Required parameters. Not optional ones.
Dim cmdRequired As List(Of CommandLineValueParameter(Of IConvertible)) =
(From cmd As CommandLineValueParameter(Of IConvertible) In cmds
Where Not cmd.IsOptional).ToList()
For Each arg As String In args
For Each cmd As CommandLineValueParameter(Of IConvertible) In cmds
If arg.Equals("/?") Then
PrintHelp()
End If
If IsParameterAssignedInArgument(cmd, arg) Then
If (cmdRequired.Contains(cmd)) Then
cmdRequired.Remove(cmd)
End If
Try
SetParameterValue(Of IConvertible)(cmd, arg)
Catch ex As Exception
callbackSyntaxError.Invoke(cmd)
End Try
End If
Next cmd
Next arg
If (cmdRequired.Any()) Then
callbackMissingRequired.Invoke(cmdRequired.First())
End If
End Sub
Friend Sub OnSyntaxError(ByVal cmd As CommandLineValueParameter(Of IConvertible))
Console.WriteLine(String.Format("[X] Syntax error in parameter: {0})", cmd.FullName))
Environment.Exit(exitCode:=1)
End Sub
Friend Sub OnMissingParameterRequired(ByVal cmd As CommandLineValueParameter(Of IConvertible))
Console.WriteLine(String.Format("[X] Parameter {0} is required. ", cmd.FullName))
Environment.Exit(exitCode:=1)
End Sub
Friend Sub PrintHelp()
Dim sb As New Global.System.Text.StringBuilder
sb.AppendLine("Commands Help:")
For Each param As CommandLineValueParameter(Of IConvertible) In params
sb.AppendFormat("{0} {{{1}}}", param.FullName, param.DefaultValue.GetType().Name)
sb.AppendLine()
Next
Console.WriteLine(sb.ToString())
Environment.Exit(exitCode:=1)
End Sub
End Module
No code example is currently available or this language may not be supported.
No code example is currently available or this language may not be supported.
No code example is currently available or this language may not be supported.
No code example is currently available or this language may not be supported.
CommandLineValueParameterCollection | Initializes a new instance of the CommandLineValueParameterCollection class. |
Count | Gets the number of elements actually contained in the CollectionT. (Inherited from CollectionCommandLineValueParameterIConvertible) |
ItemInt32 | Gets or sets the element at the specified index. (Inherited from CollectionCommandLineValueParameterIConvertible) |
ItemString | Gets or sets the CommandLineValueParameterT that matches the specified key name. |
Add | Adds a CommandLineValueParameterT to the end of the CommandLineValueParameterCollection. |
AddRange | Adds the specified parameters to the end of the CommandLineValueParameterCollection. |
Clear | Removes all elements from the CollectionT. (Inherited from CollectionCommandLineValueParameterIConvertible) |
Contains(T) | Determines whether an element is in the CollectionT. (Inherited from CollectionCommandLineValueParameterIConvertible) |
Contains(String) | Determines whether the CommandLineValueParameterCollection contains a CommandLineValueParameterT that matches the specified key name. |
CopyTo | Copies the entire CollectionT to a compatible one-dimensional Array, starting at the specified index of the target array. (Inherited from CollectionCommandLineValueParameterIConvertible) |
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object) |
Find | Searches for an CommandLineValueParameterT that matches the specified parameter name, and returns the first occurrence within the entire CommandLineValueParameterCollection. |
GetEnumerator | Returns an enumerator that iterates through the CollectionT. (Inherited from CollectionCommandLineValueParameterIConvertible) |
GetHashCode | Serves as the default hash function. (Inherited from Object) |
GetType | Gets the Type of the current instance. (Inherited from Object) |
IndexOf(T) | Searches for the specified object and returns the zero-based index of the first occurrence within the entire CollectionT. (Inherited from CollectionCommandLineValueParameterIConvertible) |
IndexOf(String) | Searches for an CommandLineValueParameterT that matches the specified key name and returns the zero-based index of the first occurrence within the entire CommandLineValueParameterCollection. |
Insert | Inserts an element into the CollectionT at the specified index. (Inherited from CollectionCommandLineValueParameterIConvertible) |
Remove(CommandLineValueParameterIConvertible) | Removes a CommandLineValueParameterT from the CommandLineValueParameterCollection. |
Remove(String) | Removes a CommandLineValueParameterT from the CommandLineValueParameterCollection. |
RemoveAt | Removes the element at the specified index of the CollectionT. (Inherited from CollectionCommandLineValueParameterIConvertible) |
ToString | Returns a string that represents the current object. (Inherited from Object) |
CanConvertTo |
Determines whether the source object can be converted to the specified target type.
(Defined by ObjectExtensions) |
CanConvertToT |
Determines whether the source object can be converted to the specified target type.
(Defined by ObjectExtensions) |
ConvertToT |
Converts an object to the specified target type.
If the conversion fails, an exception is thrown.
(Defined by ObjectExtensions) |
ConvertToT |
Converts an object to the specified target type.
If the conversion fails, returns the specified default value.
(Defined by ObjectExtensions) |
IsDisposable |
Determines whether the specified object is a disposable type
(i.e., it implements IDisposable interface).
(Defined by ObjectExtensions) |
Speak |
Speaks the string representation of the source object by using the
operating system integrated text-to-speech synthesizer.
(Defined by ObjectExtensions) |
Speak |
Speaks the string representation of the source object by using the
operating system integrated text-to-speech synthesizer.
(Defined by ObjectExtensions) |
ThrowIfNullTException |
Throws the specified exception if the source object is null.
(Defined by ObjectExtensions) |