home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / dotNETSDK / SETUP.EXE / netfxsd1.cab / FL_argparser_vb________.3643236F_FC70_11D3_A536_0090278A1BB8 < prev    next >
Encoding:
Text File  |  2002-05-01  |  7.1 KB  |  176 lines

  1. '=====================================================================
  2. '  File:      ArgParser.vb
  3. '
  4. '  Summary:   Reusable class for parsing command-line arguments.
  5. '
  6. '---------------------------------------------------------------------
  7. '  This file is part of the Microsoft .NET Framework SDK Code Samples.
  8. '
  9. '  Copyright (C) Microsoft Corporation.  All rights reserved.
  10. '
  11. 'This source code is intended only as a supplement to Microsoft
  12. 'Development Tools and/or on-line documentation.  See these other
  13. 'materials for detailed information regarding Microsoft code samples.
  14. '
  15. 'THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  16. 'KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  17. 'IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  18. 'PARTICULAR PURPOSE.
  19. '=====================================================================*/
  20.  
  21. Option Explicit On 
  22. Option Strict On
  23.  
  24.  
  25. Imports System
  26. Imports System.Globalization
  27.  
  28. MustInherit Public Class ArgParser
  29.     Private switchChars() As String         ' For example: "/", "-"
  30.     Private switchSymbols() As String           ' Switch character(s)
  31.     Private caseSensitiveSwitches As Boolean    ' Are switches case-sensitive?
  32.     
  33.     ' Domain of results when processing a command-line argument switch
  34.     Public Enum SwitchStatus
  35.         NoError
  36.         YesError
  37.         ShowUsage 
  38.     End Enum 'SwitchStatus
  39.       
  40.         
  41.     ' Constructor that defaults to case-insensitive switches and 
  42.     ' defaults to "/" and "-" as the only valid switch characters
  43.     Public Sub New(ByVal switchSymbols() As String)
  44.         MyClass.New(switchSymbols, False, New String() {"/", "-"})
  45.     End Sub 'New
  46.  
  47.  
  48.  
  49.     ' Constructor that defaults to "/" and "-" as the only valid switch characters
  50.     Public Sub New(ByVal switchSymbols() As String, ByVal caseSensitiveSwitches As Boolean)
  51.         MyClass.New(switchSymbols, caseSensitiveSwitches, New String() {"/", "-"})
  52.     End Sub 'New
  53.  
  54.  
  55.  
  56.     ' Constructor with no defaults
  57.     Public Sub New(ByVal switchSymbols() As String, ByVal caseSensitiveSwitches As Boolean, ByVal switchChars() As String)
  58.         Me.switchSymbols = switchSymbols
  59.         Me.caseSensitiveSwitches = caseSensitiveSwitches
  60.         Me.switchChars = switchChars
  61.     End Sub 'New
  62.  
  63.  
  64.  
  65.     ' Every derived class must implement an OnUsage method
  66.     Public MustOverride Sub OnUsage(ByVal errorInfo As String)
  67.  
  68.  
  69.  
  70.     ' Every derived class must implement an OnSwitch method or a switch is considered an error
  71.     Protected Overridable Function OnSwitch(ByVal switchSymbol As String, ByVal switchValue As String) As SwitchStatus
  72.         Return SwitchStatus.YesError
  73.     End Function 'OnSwitch
  74.  
  75.  
  76.  
  77.     ' Every derived class must implement an OnNonSwitch method or a non-switch is considerred an error
  78.     Protected Overridable Function OnNonSwitch(ByVal value As String) As SwitchStatus
  79.         Return SwitchStatus.YesError
  80.     End Function 'OnNonSwitch
  81.  
  82.  
  83.  
  84.     ' The derived class is notified after all command-line switches have been parsed.
  85.     ' The derived class can perform any sanity checking required at this time.
  86.     Protected Overridable Function OnDoneParse() As SwitchStatus
  87.         ' By default, we'll assume that all parsing was successful
  88.         Return SwitchStatus.YesError
  89.     End Function 'OnDoneParse
  90.  
  91.  
  92.  
  93.     ' This Parse method always parses the application's command-line arguments
  94.     Public Overloads Function Parse() As Boolean
  95.         ' Visual Basic will use this method since its entry-point function 
  96.         ' doesn't get the command-line arguments passed to it.
  97.         Return Parse(Environment.GetCommandLineArgs())
  98.     End Function 'Parse
  99.  
  100.  
  101.  
  102.     ' This Parse method parses an arbitrary set of arguments
  103.     Public Overloads Function Parse(ByVal args() As String) As Boolean
  104.         Dim ss As SwitchStatus = SwitchStatus.NoError ' Assume parsing is sucessful.
  105.         Dim ArgNum As Integer
  106.         For ArgNum = 0 To (args.Length - 1)
  107.  
  108.             If Not (ss = SwitchStatus.NoError) Then Exit For
  109.  
  110.             ' Determine if this argument starts with a valid switch character
  111.             Dim fIsSwitch As Boolean = False
  112.             Dim n As Integer
  113.             For n = 0 To (switchChars.Length - 1)
  114.                 If (fIsSwitch = True) Then Exit For
  115.                 fIsSwitch = (0 = String.CompareOrdinal(args(ArgNum), 0, switchChars(n), 0, 1))
  116.             Next n
  117.  
  118.             If fIsSwitch Then
  119.                 ' Does the switch begin with a legal switch symbol?
  120.                 Dim fLegalSwitchSymbol As Boolean = False
  121.                 For n = 0 To (switchSymbols.Length - 1) ' re-using local var n to traverse an entirely seperate loop
  122.                     If caseSensitiveSwitches Then
  123.                         fLegalSwitchSymbol = (0 = String.CompareOrdinal(args(ArgNum), 1, switchSymbols(n), 0, switchSymbols(n).Length))
  124.                     Else
  125.                         fLegalSwitchSymbol = (0 = String.CompareOrdinal(args(ArgNum).ToUpper(CultureInfo.InvariantCulture), 1, switchSymbols(n).ToUpper(CultureInfo.InvariantCulture), 0, switchSymbols(n).Length))
  126.                     End If
  127.                     If fLegalSwitchSymbol Then
  128.                         Exit For
  129.                     End If
  130.                 Next n
  131.                 If Not fLegalSwitchSymbol Then
  132.                     ' User specified an unrecognized switch, exit
  133.                     ss = SwitchStatus.YesError
  134.                     Exit For
  135.                 Else
  136.                     ' This is a legal switch, notified the derived class of this switch and its value
  137.                     If (caseSensitiveSwitches) Then
  138.                         ss = OnSwitch(switchSymbols(n), args(ArgNum).Substring((1 + switchSymbols(n).Length)))
  139.                     Else
  140.                         ss = OnSwitch(switchSymbols(n).ToLower(CultureInfo.InvariantCulture), args(ArgNum).Substring((1 + switchSymbols(n).Length)))
  141.                     End If
  142.                 End If
  143.             Else
  144.                 ' This is not a switch, notified the derived class of this "non-switch value"
  145.                 ss = OnNonSwitch(args(ArgNum))
  146.             End If
  147.         Next ArgNum
  148.  
  149.         ' Finished parsing arguments
  150.         If ss = SwitchStatus.NoError Then
  151.             ' No error occurred while parsing, let derived class perform a 
  152.             ' sanity check and return an appropraite status
  153.             ss = OnDoneParse()
  154.         End If
  155.  
  156.         If ss = SwitchStatus.ShowUsage Then
  157.             ' Status indicates that usage should be shown, show it
  158.             OnUsage(Nothing)
  159.         End If
  160.  
  161.         If ss = SwitchStatus.YesError Then
  162.             ' Status indicates that an error occurred, show it and the proper usage
  163.             If (ArgNum = args.Length) Then
  164.                 OnUsage(Nothing)
  165.             Else
  166.                 OnUsage(args(ArgNum))
  167.             End If
  168.         End If
  169.  
  170.         ' Return whether all parsing was sucessful.
  171.         Return ss = SwitchStatus.NoError
  172.     End Function 'Parse
  173. End Class 'ArgParser
  174.  
  175.  
  176. '/////////////////////////////// End of File /////////////////////////////////