/// <summary> /// Utility method used by the commands to write exception data to the console. /// </summary> /// <param name="message"></param> public static void WriteErrorMessage(string message) { using (LineWrappingTextWriter writer = LineWrappingTextWriter.ForConsoleError()) { writer.WriteLine(message); } }
public static CommandLineParser Parse(string[] args) { Request = null; Ookii.CommandLine.CommandLineParser parser = new Ookii.CommandLine.CommandLineParser(typeof(CommandLineParser)); parser.ArgumentParsed += CommandLineParser_ArgumentParsed; try { try { Request = (CommandLineParser)parser.Parse(args); } finally { MaybeShowLicense(args); } } catch (CommandLineArgumentException ex) { using (LineWrappingTextWriter writer = LineWrappingTextWriter.ForConsoleError()) { Logger.Log(Logger.Level.Error, ex.Message); } } if (Request == null) { WriteUsageOptions options = new WriteUsageOptions() { IncludeDefaultValueInDescription = true, IncludeAliasInDescription = true }; parser.WriteUsageToConsole(options); Logger.Log(Logger.Level.Info, "Example:"); Logger.Log(Logger.Level.Info, " SqlProcScaffold.exe \"Server=my...\" MyNameSpace dbo.sp% C:\\src\\MyProj"); } return(Request); }
public static ProgramArguments Create(string[] args) { // Using a static creation function for a command line arguments class is not required, but it's a convenient // way to place all command-line related functionality in one place. To parse the arguments (eg. from the Main method) // you then only need to call this function. CommandLineParser parser = new CommandLineParser(typeof(ProgramArguments)); // The ArgumentParsed event is used by this sample to stop parsing after the -Help argument is specified. parser.ArgumentParsed += CommandLineParser_ArgumentParsed; try { // The Parse function returns null only when the ArgumentParsed event handler cancelled parsing. ProgramArguments result = (ProgramArguments)parser.Parse(args); if (result != null) { return(result); } } catch (CommandLineArgumentException ex) { // We use the LineWrappingTextWriter to neatly wrap console output. using (LineWrappingTextWriter writer = LineWrappingTextWriter.ForConsoleError()) { // Tell the user what went wrong. writer.WriteLine(ex.Message); writer.WriteLine(); } } // If we got here, we should print usage information to the console. // By default, aliases and default values are not included in the usage descriptions; for this sample, I do want to include them. WriteUsageOptions options = new WriteUsageOptions() { IncludeDefaultValueInDescription = true, IncludeAliasInDescription = true }; // WriteUsageToConsole automatically uses a LineWrappingTextWriter to properly word-wrap the text. parser.WriteUsageToConsole(options); return(null); }
/// <summary> /// コマンドライン引数の生成 /// </summary> /// <param name="args">コマンドライン引数</param> /// <returns></returns> public static CommandLineArgs Create(string[] args) { var parser = new CommandLineParser(typeof(CommandLineArgs)); // parser.ArgumentParsed += Parser_ArgumentParsed; // try { var result = (CommandLineArgs)parser.Parse(args); if (result != null) { return(result); } } catch (CommandLineArgumentException ex) { using (var writer = LineWrappingTextWriter.ForConsoleError()) { NativeMethods.AttachConsole(uint.MaxValue); writer.WriteLine(GetParsingErrorMessage(ex)); writer.WriteLine(); } return(new CommandLineArgs()); } // var options = new WriteUsageOptions() { IncludeAliasInDescription = true, IncludeDefaultValueInDescription = true }; if (NativeMethods.AttachConsole(uint.MaxValue)) { parser.WriteUsageToConsole(options); } return(null); }
public static ProgramArguments Create(string[] args) { // Using a static creation function for a command line arguments class is not required, but it's a convenient // way to place all command-line related functionality in one place. To parse the arguments (eg. from the Main method) // you then only need to call this function. CommandLineParser parser = new CommandLineParser(typeof(ProgramArguments)); ProgramArguments result = null; bool showFullHelp = false; try { // The Parse function returns null only when the ArgumentParsed event handler cancelled parsing. result = (ProgramArguments)parser.Parse(args); showFullHelp = result.Help; if (result.Help) { showFullHelp = false; switch (result.Command) { case Command.Add: Console.WriteLine("Help specific to Add command here."); break; case Command.Delete: Console.WriteLine("Help specific to Delete command here."); break; case Command.Update: Console.WriteLine("Help specific to Update command here."); break; default: showFullHelp = true; break; } } } catch (CommandLineArgumentException ex) { // We use the LineWrappingTextWriter to neatly wrap console output. using (LineWrappingTextWriter writer = LineWrappingTextWriter.ForConsoleError()) { // Tell the user what went wrong. writer.WriteLine(ex.Message); writer.WriteLine(); } } if (showFullHelp) { // If we got here, we should print usage information to the console. // By default, aliases and default values are not included in the usage descriptions; for this sample, I do want to include them. WriteUsageOptions options = new WriteUsageOptions() { IncludeDefaultValueInDescription = true, IncludeAliasInDescription = true }; // WriteUsageToConsole automatically uses a LineWrappingTextWriter to properly word-wrap the text. parser.WriteUsageToConsole(options); } return(result); }