1: using System;
2: using System.Collections.Generic;
3:
4: class Program
5: { 6: static int Main(string[] args)
7: { 8: if (args.Length == 0) return Usage(ErrorLevel.NoArgs);
9:
10: // Load args array into a generic list of strings
11: List<string> switches = new List<string>(args);
12:
13: // Asking for help?
14: if (switches.FindIndex(FindHelp) > 0) return Usage(ErrorLevel.DetailedHelp);
15:
16: string username;
17: ErrorLevel errorLevel;
18: if (!TryParseUsername(switches, out username, out errorLevel)) return Usage(errorLevel);
19:
20:
21: // Check that -deleteuser is not present with -setpassword and -showinfo
22: if (switches.Exists(FindDeleteSwitch) && (switches.Exists(FindSetPasswordSwitch) || switches.Exists(FindShowInfoSwitch)))
23: { 24: return Usage(ErrorLevel.DeleteSwitchIsExclusive);
25: }
26:
27: if(switches.Exists(FindDeleteSwitch))
28: { 29: // Delete user
30: }
31: else
32: { 33: if(switches.Exists(FindSetPasswordSwitch))
34: { 35: // Set password
36: }
37:
38: if(switches.Exists(FindShowInfoSwitch))
39: { 40: // Show info about user
41: }
42: }
43:
44: return (int)ErrorLevel.NoErrors;
45: }
46:
47:
48: private static bool FindHelp(string s)
49: { 50: s = s.Replace("/", "-"); 51: return s.Equals("-help", StringComparison.InvariantCultureIgnoreCase) || 52: s.Equals("-?", StringComparison.InvariantCultureIgnoreCase); 53: }
54:
55: private static bool FindUserSwitch(string s)
56: { 57: s = s.Replace("/", "-"); 58: return s.StartsWith("-user:", StringComparison.InvariantCultureIgnoreCase); 59: }
60:
61: private static bool FindDeleteSwitch(string s)
62: { 63: s = s.Replace("/", "-"); 64: return s.Equals("-deleteuser", StringComparison.InvariantCultureIgnoreCase); 65: }
66:
67: private static bool FindSetPasswordSwitch(string s)
68: { 69: s = s.Replace("/", "-"); 70: return s.StartsWith("-setpassword:", StringComparison.InvariantCultureIgnoreCase); 71: }
72:
73: private static bool FindShowInfoSwitch(string s)
74: { 75: s = s.Replace("/", "-"); 76: return s.Equals("-showinfo", StringComparison.InvariantCultureIgnoreCase); 77: }
78:
79: private static bool TryParseUsername(List<string> switches, out string username, out ErrorLevel errorLevel)
80: { 81: // Simple example, we just want to find the -user switch
82: username = switches.Find(FindUserSwitch);
83:
84: // Returns null if no match
85: if (username == null)
86: { 87: errorLevel = ErrorLevel.UserSwitchMissing;
88: return false;
89: }
90:
91: // Check that we have a valid <username> part of -user:<username> switch
92: string[] temp = username.Split(':'); 93: if (temp.Length != 2 || temp[1].Length == 0)
94: { 95: errorLevel = ErrorLevel.UsernameMissing;
96: return false;
97: }
98:
99: username = temp[1];
100: errorLevel = ErrorLevel.NoErrors;
101: return true;
102: }
103:
104: private static int Usage(ErrorLevel errorLevel)
105: { 106: Console.WriteLine("Usage: useradmin -user:username [ [-setpassword:password] [-showinfo] | -deleteuser ]"); 107:
108: switch(errorLevel)
109: { 110: case ErrorLevel.UserSwitchMissing:
111: Console.WriteLine("-user:<username> is mandatory."); 112: break;
113:
114: case ErrorLevel.UsernameMissing:
115: Console.WriteLine("-user:<username>, <username> is missing."); 116: break;
117:
118: // Other error level messages
119: // ...
120:
121: case ErrorLevel.DeleteSwitchIsExclusive:
122: Console.WriteLine("-deleteuser is exclusive of -showinfo and -setpassword"); 123: break;
124:
125: default:
126: break;
127: }
128:
129:
130:
131: if(errorLevel == ErrorLevel.DetailedHelp)
132: { 133: Console.WriteLine("Detailed help:..."); 134: }
135: return (int)errorLevel;
136: }
137:
138: public enum ErrorLevel
139: { 140: NoErrors,
141: NoArgs,
142: DetailedHelp,
143: UserSwitchMissing,
144: UsernameMissing,
145: DeleteSwitchIsExclusive
146: // Other error level values as necessary
147: }
148: }