public bool Execute(string[] args, StringBuilder output)
        {
            CommandLineParser parser    = new CommandLineParser(typeof(TArgs));
            TArgs             arguments = null;

            try
            {
                arguments = (TArgs)parser.Parse(args);
            }
            catch (CommandLineArgumentException ex)
            {
                output.AppendLine(Invariant($"Error parsing ${this.Name} directive: {ex.Message}"));
                using (StringWriter writer = new StringWriter(output, CultureInfo.InvariantCulture))
                {
                    WriteUsageOptions options = new WriteUsageOptions()
                    {
                        UsagePrefix = Invariant($"    Usage: ${this.Name}"),
                        Indent      = 8
                    };

                    parser.WriteUsage(writer, 0, options);
                }

                return(false);
            }

            return(this.ExecuteCore(arguments, output));
        }
Пример #2
0
 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);
 }
Пример #3
0
        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);
        }
Пример #4
0
        /// <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);
        }
Пример #5
0
        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);
        }