home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / WordCount / ArgParser.cs next >
Encoding:
Text File  |  2000-06-23  |  6.3 KB  |  159 lines

  1. /*=====================================================================
  2.   File:      ArgParse.cs
  3.  
  4.   Summary:   Reusable class for parsing command-line arguments.
  5.  
  6. ---------------------------------------------------------------------
  7.   This file is part of the Microsoft COM+ 2.0 SDK Code Samples.
  8.  
  9.   Copyright (C) 2000 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. // Add the classes in the following namespaces to our namespace
  22. using System;
  23.  
  24.  
  25. ///////////////////////////////////////////////////////////////////////////////
  26.  
  27.  
  28. public abstract class ArgParser {
  29.     private String[] switchChars;             // For example: "/", "-"
  30.     private String[] switchSymbols;           // Switch character(s)
  31.     private Boolean  caseSensitiveSwitches;   // Are switches case-sensitive?
  32.  
  33.  
  34.     // Domain of results when processing a command-line argument switch
  35.     protected enum SwitchStatus { NoError, Error, ShowUsage };
  36.  
  37.  
  38.     // Constructor that defaults to case-insensitive switches and 
  39.     // defaults to "/" and "-" as the only valid switch characters
  40.     public ArgParser(String[] switchSymbols) 
  41.         : this(switchSymbols, false, new string[] { "/", "-" }) {
  42.     }
  43.  
  44.  
  45.     // Constructor that defaults to "/" and "-" as the only valid switch characters
  46.     public ArgParser(String[] switchSymbols, Boolean caseSensitiveSwitches) 
  47.         : this(switchSymbols, caseSensitiveSwitches, new string[] { "/", "-" }) {
  48.     }
  49.  
  50.  
  51.     // Constructor with no defaults
  52.     public ArgParser(String[] switchSymbols, Boolean caseSensitiveSwitches, String[] switchChars) {
  53.         this.switchSymbols = switchSymbols;
  54.         this.caseSensitiveSwitches = caseSensitiveSwitches;
  55.         this.switchChars   = switchChars;
  56.     }
  57.  
  58.  
  59.     // Every derived class must implement an OnUsage method
  60.     public abstract void OnUsage(String errorInfo);
  61.  
  62.  
  63.     // Every derived class must implement an OnSwitch method or a switch is considerred an error
  64.     protected virtual SwitchStatus OnSwitch(String switchSymbol, String switchValue) {
  65.         return(SwitchStatus.Error);
  66.     }
  67.  
  68.  
  69.     // Every derived class must implement an OnNonSwitch method or a non-switch is considerred an error
  70.     protected virtual SwitchStatus OnNonSwitch(String value) {
  71.         return(SwitchStatus.Error);
  72.     }
  73.  
  74.  
  75.     // The derived class is notified after all command-line switches have been parsed.
  76.     // The derived class can perform any sanity checking required at this time.
  77.     protected virtual SwitchStatus OnDoneParse() {
  78.         // By default, we'll assume that all parsing was successful
  79.         return(SwitchStatus.Error);
  80.     }
  81.  
  82.  
  83.     // This Parse method always parses the application's command-line arguments
  84.     public Boolean Parse() {
  85.         // Visual Basic will use this method since its entry-point function 
  86.         // doesn't get the command-line arguments passed to it.
  87.         return(Parse(Environment.GetCommandLineArgs()));
  88.     }
  89.  
  90.  
  91.     // This Parse method parses an arbitrary set of arguments
  92.     public Boolean Parse(String[] args) {
  93.         SwitchStatus ss = SwitchStatus.NoError;        // Assume parsing is sucessful.
  94.  
  95.         int ArgNum;
  96.         for (ArgNum = 0; (ss == SwitchStatus.NoError) && (ArgNum < args.Length); ArgNum++) {
  97.  
  98.             // Determine if this argument starts with a valid switch character
  99.             Boolean fIsSwitch = false;
  100.             for (int n = 0; !fIsSwitch && (n < switchChars.Length); n++) {
  101.                 fIsSwitch = (0 == String.CompareOrdinal(args[ArgNum], 0, 
  102.                     switchChars[n], 0, 1));
  103.             }
  104.  
  105.             if (fIsSwitch) {
  106.                 // Does the switch begin with a legal switch symbol?
  107.                 Boolean fLegalSwitchSymbol = false;
  108.                 int n;
  109.                 for (n = 0; !fLegalSwitchSymbol && (n < switchSymbols.Length); n++) {
  110.                     if (caseSensitiveSwitches) {
  111.                         fLegalSwitchSymbol = (0 == String.CompareOrdinal(args[ArgNum], 1, 
  112.                             switchSymbols[n], 0, switchSymbols[n].Length));
  113.                     } else {
  114.                         fLegalSwitchSymbol = (0 == String.CompareOrdinal(args[ArgNum].ToUpper(), 1, 
  115.                             switchSymbols[n].ToUpper(), 0, switchSymbols[n].Length));
  116.                     }
  117.                     if (fLegalSwitchSymbol) break;
  118.                 }
  119.                 if (!fLegalSwitchSymbol) {
  120.                     // User specified an unrecognized switch, exit
  121.                     ss = SwitchStatus.Error;
  122.                     break;
  123.                 } else {
  124.                     // This is a legal switch, notified the derived class of this switch and its value
  125.                     ss = OnSwitch(
  126.                         caseSensitiveSwitches ? switchSymbols[n] : switchSymbols[n].ToLower(), 
  127.                         args[ArgNum].Substring(1 + switchSymbols[n].Length));
  128.                 }
  129.             } else {
  130.                 // This is not a switch, notified the derived class of this "non-switch value"
  131.                 ss = OnNonSwitch(args[ArgNum]);
  132.             }
  133.         }
  134.  
  135.         // Finished parsing arguments
  136.         if (ss == SwitchStatus.NoError) {
  137.             // No error occurred while parsing, let derived class perform a 
  138.             // sanity check and return an appropraite status
  139.             ss = OnDoneParse();
  140.         }
  141.  
  142.         if (ss == SwitchStatus.ShowUsage) {
  143.             // Status indicates that usage should be shown, show it
  144.             OnUsage(null);
  145.         }
  146.  
  147.         if (ss == SwitchStatus.Error) {
  148.             // Status indicates that an error occurred, show it and the proper usage
  149.             OnUsage((ArgNum == args.Length) ? null : args[ArgNum]);
  150.         }
  151.  
  152.         // Return whether all parsing was sucessful.
  153.         return(ss == SwitchStatus.NoError);
  154.     }
  155. }
  156.  
  157.  
  158. ///////////////////////////////// End of File /////////////////////////////////
  159.