Ejemplo n.º 1
0
        private static bool ParseCommandGroups <TOptions>(string[] args, ref TOptions options, TypeArgumentInfo arguments) where TOptions : new()
        {
            if (arguments.ActionArgument == null)
            {
                throw new ArgumentException("Cannot have groups unless Command argument has been specified");
            }

            if (args.Length == 0)
            {
                throw new ArgumentException("Required parameters have not been specified");
            }

            // parse based on the command passed in (the first arg).
            if (!arguments.ArgumentGroups.ContainsKey(args[0]))
            {
                throw new ArgumentException($"Unknown command [Cyan!{args[0]}]");
            }

            // short circuit the request for help!
            if (args.Length == 2 && (args[1] == "/?" || args[1] == "-?"))
            {
                HelpGenerator.DisplayHelpForCommmand(args[0], arguments.ArgumentGroups[args[0]]);
                return(false);
            }

            options = InternalParse <TOptions>(args, 1, arguments.ArgumentGroups[args[0]]);
            arguments.ActionArgument.SetValue(options, PropertyHelpers.GetValueAsType(args[0], arguments.ActionArgument));
            return(true);
        }
Ejemplo n.º 2
0
        private static bool InternalTryParse <TOptions>(string[] args, ParserOptions parserOptions, out TOptions options, out Exception ex)
            where TOptions : new()
        {
            options = default;
            ex      = null;
            TypeArgumentInfo arguments = null;

            try
            {
                // build a list of properties for the type passed in.
                // this will throw for cases where the type is incorrecly annotated with attributes
                TypeHelpers.ScanTypeForProperties <TOptions>(out arguments);

                // before we do anything, let's expand the response files (if any).
                args = ExpandResponseFiles(args);

                // short circuit the request for help!
                if (args.Length == 1)
                {
                    if (args[0] == HelpGenerator.RequestShortHelpParameter || args[0] == "/?")
                    {
                        HelpGenerator.DisplayHelp(HelpFormat.Short, arguments, ColorScheme.Get());
                        ex = new HelpRequestedException();
                        return(false);
                    }
                    else if (args[0] == HelpGenerator.RequestLongHelpParameter)
                    {
                        HelpGenerator.DisplayHelp(HelpFormat.Full, arguments, ColorScheme.Get());
                        ex = new HelpRequestedException();
                        return(false);
                    }
                }

                // we have groups!
                if (!arguments.ArgumentGroups.ContainsKey(string.Empty))
                {
                    return(ParseCommandGroups(args, ref options, arguments, parserOptions));
                }

                // parse the arguments and build the options object
                options = InternalParse <TOptions>(args, 0, arguments.ArgumentGroups[string.Empty], parserOptions);
                return(true);
            }
            catch (Exception innerParserException)
            {
                ex = new ParserException(innerParserException.Message, innerParserException);
                if (parserOptions.LogParseErrorToConsole)
                {
                    string errorFormat = $"[{ColorScheme.Get().ErrorColor}!Error]: {{0}} {{1}}";
                    Colorizer.WriteLine(errorFormat, ex.Message, Environment.NewLine);

                    HelpGenerator.DisplayHelp(HelpFormat.Short, arguments, ColorScheme.Get());
                }
                return(false);
            }
        }
Ejemplo n.º 3
0
        public static bool TryParse <TOptions>(string[] args, out TOptions options)
            where TOptions : new()
        {
            options = default(TOptions);

            TypeArgumentInfo arguments = null;

            try
            {
                // build a list of properties for the type passed in.
                // this will throw for cases where the type is incorrecly annotated with attributes
                TypeHelpers.ScanTypeForProperties <TOptions>(out arguments);

                // before we do anything, let's expand the response files (if any).
                args = ExpandResponseFiles(args);

                // short circuit the request for help!
                if (args.Length == 1)
                {
                    if (args[0] == HelpGenerator.RequestShortHelpParameter || args[0] == "/?")
                    {
                        HelpGenerator.DisplayHelp(HelpFormat.Short, arguments);
                        return(false);
                    }
                    else if (args[0] == HelpGenerator.RequestLongHelpParameter)
                    {
                        HelpGenerator.DisplayHelp(HelpFormat.Full, arguments);
                        return(false);
                    }
                }

                // we have groups!
                if (!arguments.ArgumentGroups.ContainsKey(string.Empty))
                {
                    return(ParseCommandGroups(args, ref options, arguments));
                }

                // parse the arguments and build the options object
                options = InternalParse <TOptions>(args, 0, arguments.ArgumentGroups[string.Empty]);
                return(true);
            }
            catch (Exception ex)
            {
                Colorizer.WriteLine($"[Red!Error]: {ex.Message} {Environment.NewLine}");

                HelpGenerator.DisplayHelp(HelpFormat.Short, arguments);

                return(false);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Display the help based on the <typeparamref name="TOptions"/> type provided
        /// </summary>
        /// <typeparam name="TOptions">The type for which to generate the help.</typeparam>
        /// <param name="helpFormat">Describes the level of details to generate for the help message.</param>
        public static void DisplayHelp <TOptions>(HelpFormat helpFormat = HelpFormat.Short)
        {
            if (helpFormat != HelpFormat.Short && helpFormat != HelpFormat.Full)
            {
                throw new ArgumentException("Unrecognized help format", nameof(helpFormat));
            }

            try
            {
                // build a list of properties for the type passed in.
                // this will throw for cases where the type is incorrecly annotated with attributes
                TypeHelpers.ScanTypeForProperties <TOptions>(out TypeArgumentInfo arguments);

                // If we get here, the options type is well defined, so let's display the help.
                HelpGenerator.DisplayHelp(helpFormat, arguments);
            }
            catch (Exception ex)
            {
                // If we were asked to display the help and something went wrong, display the error that went wrong.
                Colorizer.WriteLine($"[Red!Error]: {ex.Message} {Environment.NewLine}");
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Display the help based on the <typeparamref name="TOptions"/> type provided
        /// </summary>
        /// <typeparam name="TOptions">The type for which to generate the help.</typeparam>
        /// <param name="helpFormat">Describes the level of details to generate for the help message.</param>
        public static void DisplayHelp <TOptions>(HelpFormat helpFormat = HelpFormat.Short)
        {
            if (helpFormat != HelpFormat.Short && helpFormat != HelpFormat.Full)
            {
                throw new ParserException($"Unrecognized help format {helpFormat}", null);
            }

            try
            {
                // build a list of properties for the type passed in.
                // this will throw for cases where the type is incorrecly annotated with attributes
                TypeHelpers.ScanTypeForProperties <TOptions>(out TypeArgumentInfo arguments);

                // If we get here, the options type is well defined, so let's display the help.
                HelpGenerator.DisplayHelp(helpFormat, arguments, ColorScheme.Get());
            }
            catch (Exception ex)
            {
                string errorFormat = $"[{ColorScheme.Get().ErrorColor}!Error]: {{0}} {{1}}";
                // If we were asked to display the help and something went wrong, display the error that went wrong.
                Colorizer.WriteLine(errorFormat, ex.Message, Environment.NewLine);
            }
        }