/// <summary> /// Parses the command-line arguments that are passed to this program by /// the user and fills the properties of a newly-instantiated instance /// of <see cref="T:my_uuidgen.CommandLineInfo" /> accordingly. /// </summary> /// <param name="args"> /// Enumerable collection of strings, each of which is a command-line argument. /// </param> /// <returns> /// Reference to an instance of /// <see /// cref="T:my_uuidgen.CommandLineInfo" /> /// , whose properties are /// initialized according to the switches passed to this application on /// the command line. /// </returns> public static CommandLineInfo ParseCommandLine(IEnumerable <string> args) { if (args == null) { throw new ArgumentNullException(nameof(args)); } var result = new CommandLineInfo(); var argsList = new Stack <string>(args); if (!argsList.Any()) { return(result); } var formatTypeProvided = false; // this can only happen once while (argsList.Any()) { var arg = argsList.Pop(); if (string.IsNullOrWhiteSpace(arg)) { continue; } if ("/verysilent".Equals(arg.ToLowerInvariant())) { continue; } if (Resources.UppercaseSwitch.Equals(arg.ToLowerInvariant())) { result.IsUppercase = true; continue; } if (Resources.NoCopySwitch.Equals(arg.ToLowerInvariant())) { result.ShouldNotCopy = true; continue; } if (!arg.IsFormatTypeArgument() || formatTypeProvided) { continue; } result.FormatType = GetFormatType.FromSwitch(arg); formatTypeProvided = !formatTypeProvided; } return(result); }
public static void Main(string[] args) { var cmdInfo = CommandLineInfo.ParseCommandLine(args); if (args.Any()) { if (Resources.VersionSwitch.Equals(args[0])) { Console.WriteLine( Assembly.GetExecutingAssembly() .GetName() .Version ); Environment.Exit(0); /* exit code of zero means success */ } } /* This software has one job in life -- to get a new Globally-Unique Identifier (GUID) and then * write it to the standard output and then exit. This program is meant to replicate the uuidgen.exe * utility provided with the Windows SDK, but I wanted to use it in my own batch files, and who the heck * wants to download and install the SDK all the time? */ var newGuidString = Guid.NewGuid() .ToString( GetGuidFormatSpecifier.ForFormatType( cmdInfo.FormatType ) ); var guidString = cmdInfo.IsUppercase ? newGuidString.ToUpperInvariant() : newGuidString.ToLowerInvariant(); Console.WriteLine(guidString); if (!cmdInfo.ShouldNotCopy) { // place the GUID string that we otherwise pump to standard // output, also to be on the Clipboard. This way, this app can // also be launched, e.g., from the Tools menu on Visual Studio // and then the user can just do a paste into whatever file they // are working on right off the bat. Clipboard.SetText(guidString); } Environment.Exit(0); /* exit code of zero means success */ }