Exemplo n.º 1
0
        private static int DisplayCommandLine(ArgumentGroupInfo arguments)
        {
            int maxStringSize = 0;

            for (int i = 0; i < arguments.RequiredArguments.Count; i++)
            {
                if (!arguments.RequiredArguments.ContainsKey(i))
                {
                    Colorizer.WriteLine($"{Environment.NewLine}[Red!Error]: Required argument expected at position [Cyan!{i}]. Type declares arguments at position(s) [Cyan!{string.Join(",", arguments.RequiredArguments.Keys.OrderBy(x => x))}]. [Red!Check type definition].");
                    return(-1);
                }

                var b = arguments.RequiredArguments[i].GetCustomAttribute <ActualArgumentAttribute>();
                maxStringSize = Math.Max(maxStringSize, b.Name.Length);
                Colorizer.Write("[Cyan!{0}] ", b.Name);
            }

            foreach (var item in arguments.OptionalArguments.Values)
            {
                var b = item.GetCustomAttribute <ActualArgumentAttribute>();
                maxStringSize = Math.Max(maxStringSize, b.Name.Length);
                Colorizer.Write("\\[-[Yellow!{0}] value\\] ", b.Name);
            }

            Colorizer.WriteLine(string.Empty);

            return(maxStringSize);
        }
Exemplo n.º 2
0
        private static int DisplayCommandLine(ArgumentGroupInfo arguments, IColors colors)
        {
            int maxStringSize = 0;

            for (int i = 0; i < arguments.RequiredArguments.Count; i++)
            {
                if (!arguments.RequiredArguments.ContainsKey(i))
                {
                    string requiredArgError = $"{Environment.NewLine}[{colors.ErrorColor}!Error]: Required argument expected at position[{colors.RequiredArgumentColor}!{{0}}]. Type declares arguments at position(s) [{colors.RequiredArgumentColor}!{{1}}]. [{colors.ErrorColor}!Check type definition].";

                    Colorizer.WriteLine(requiredArgError, i, string.Join(",", arguments.RequiredArguments.Keys.OrderBy(x => x)));
                    return(-1);
                }

                var b = arguments.RequiredArguments[i].GetCustomAttribute <ActualArgumentAttribute>();
                maxStringSize = Math.Max(maxStringSize, b.Name.Length);

                string argumentName = $"[{colors.RequiredArgumentColor}!{{0}}] ";
                Colorizer.Write(argumentName, b.Name);
            }

            foreach (var item in arguments.OptionalArguments.Values)
            {
                var b = item.GetCustomAttribute <ActualArgumentAttribute>();
                maxStringSize = Math.Max(maxStringSize, b.Name.Length);

                string optionalArgumentFormat = $"\\[-[{colors.OptionalArgumentColor}!{{0}}] value\\] ";
                Colorizer.Write(optionalArgumentFormat, b.Name);
            }

            Colorizer.WriteLine(string.Empty);

            return(maxStringSize);
        }
Exemplo n.º 3
0
        public static void DisplayHelpForCommmand(string command, ArgumentGroupInfo propertyGroup, IColors colors)
        {
            string exeName = Assembly.GetEntryAssembly()?.GetName()?.Name;

            Colorizer.WriteLine("Usage: ");

            DisplayDetailedArgumentHelp(exeName, command, propertyGroup, colors);
        }
Exemplo n.º 4
0
        private static void DisplayDetailedParameterHelp(ArgumentGroupInfo arguments)
        {
            int maxStringSize = DisplayCommandLine(arguments);

            if (maxStringSize < 0)
            {
                return;
            }

            // write out the required arguments
            for (int i = 0; i < arguments.RequiredArguments.Count; i++)
            {
                var b = arguments.RequiredArguments[i].GetCustomAttribute <ActualArgumentAttribute>();
                if (TypeHelpers.IsEnum(arguments.RequiredArguments[i].PropertyType))
                {
                    Colorizer.WriteLine("  - [Cyan!{0}] : {1} (one of [Green!{2}], [Cyan!required])", b.Name.PadRight(maxStringSize), b.Description, GetEnumValuesAsString(arguments.RequiredArguments[i].PropertyType));
                }
                else
                {
                    Colorizer.WriteLine("  - [Cyan!{0}] : {1} ([Green!{2}], [Cyan!required])", b.Name.PadRight(maxStringSize), b.Description, GetFriendlyTypeName(arguments.RequiredArguments[i].PropertyType));
                }
            }

            // write out the optional arguments
            foreach (var item in arguments.OptionalArguments.Values)
            {
                var b = item.GetCustomAttribute <OptionalArgumentAttribute>();

                if (TypeHelpers.IsEnum(item.PropertyType))
                {
                    Colorizer.WriteLine("  - [Yellow!{0}] : {1} (one of [Green!{2}], default=[Yellow!{3}])", b.Name.PadRight(maxStringSize), b.Description, GetEnumValuesAsString(item.PropertyType), b.DefaultValue);
                }
                else
                {
                    Colorizer.WriteLine("  - [Yellow!{0}] : {1} ([Green!{3}], default=[Yellow!{2}])", b.Name.PadRight(maxStringSize), b.Description, b.DefaultValue ?? "", GetFriendlyTypeName(item.PropertyType));
                }
            }
            Colorizer.WriteLine(string.Empty);
        }
Exemplo n.º 5
0
        private static void DisplayDetailedParameterHelp(ArgumentGroupInfo arguments, IColors colors)
        {
            int maxStringSize = DisplayCommandLine(arguments, colors);

            // write out the required arguments
            for (int i = 0; i < arguments.RequiredArguments.Count; i++)
            {
                var b = arguments.RequiredArguments[i].GetCustomAttribute <ActualArgumentAttribute>();
                if (TypeHelpers.IsEnum(arguments.RequiredArguments[i].PropertyType))
                {
                    string requiredArgFormatDetailed = $"  - [{colors.RequiredArgumentColor}!{{0}}] : {{1}} (one of [{colors.ArgumentValueColor}!{{2}}], [{colors.RequiredArgumentColor}!required])";
                    Colorizer.WriteLine(requiredArgFormatDetailed, b.Name.PadRight(maxStringSize), b.Description, GetEnumValuesAsString(arguments.RequiredArguments[i].PropertyType));
                }
                else
                {
                    string requiredArgFormatDetailed = $"  - [{colors.RequiredArgumentColor}!{{0}}] : {{1}} ([{colors.ArgumentValueColor}!{{2}}], [{colors.RequiredArgumentColor}!required])";
                    Colorizer.WriteLine(requiredArgFormatDetailed, b.Name.PadRight(maxStringSize), b.Description, GetFriendlyTypeName(arguments.RequiredArguments[i].PropertyType));
                }
            }

            // write out the optional arguments
            foreach (var item in arguments.OptionalArguments.Values)
            {
                var b = item.GetCustomAttribute <OptionalArgumentAttribute>();

                if (TypeHelpers.IsEnum(item.PropertyType))
                {
                    string optionalFormatArg = $"  - [{colors.OptionalArgumentColor}!{{0}}] : {{1}} (one of [{colors.ArgumentValueColor}!{{2}}], default=[{colors.OptionalArgumentColor}!{{3}}])";
                    Colorizer.WriteLine(optionalFormatArg, b.Name.PadRight(maxStringSize), b.Description, GetEnumValuesAsString(item.PropertyType), b.DefaultValue);
                }
                else
                {
                    string optionalFormatArg = $"  - [{colors.OptionalArgumentColor}!{{0}}] : {{1}} ([{colors.ArgumentValueColor}!{{3}}], default=[{colors.OptionalArgumentColor}!{{2}}])";
                    Colorizer.WriteLine(optionalFormatArg, b.Name.PadRight(maxStringSize), b.Description, b.DefaultValue ?? "", GetFriendlyTypeName(item.PropertyType));
                }
            }
            Colorizer.WriteLine(string.Empty);
        }
Exemplo n.º 6
0
        private static void DisplayDetailedArgumentHelp(string exeName, string command, ArgumentGroupInfo arguments, IColors colors)
        {
            string argumentNameFormat = $" [{colors.AssemblyNameColor}!{{ 0}}.exe] ";

            Colorizer.Write(argumentNameFormat, exeName);
            if (!string.IsNullOrEmpty(command))
            {
                string formatArgs = $"[{colors.ArgumentGroupColor}!{{0}}] ";
                Colorizer.Write(formatArgs, command);
            }
            DisplayDetailedParameterHelp(arguments, colors);
        }
Exemplo n.º 7
0
        private static void ParseRequiredParameters <TOptions>(string[] args, int offsetInArray, ArgumentGroupInfo TypeArgumentInfo, TOptions options, ref int currentLogicalPosition) where TOptions : new()
        {
            if (args.Length == 0)
            {
                throw new ArgumentException("Required parameters have not been specified");
            }

            int matchedRequiredParameters = 0;

            do
            {
                //set the required property
                PropertyInfo propInfo;
                if (!TypeArgumentInfo.RequiredArguments.TryGetValue(currentLogicalPosition, out propInfo))
                {
                    break;
                }

                //make sure that we don't run out of array
                if (offsetInArray + currentLogicalPosition >= args.Length)
                {
                    throw new ArgumentException("Required parameters have not been specified");
                }
                var value = PropertyHelpers.GetValueFromArgsArray(args, offsetInArray, ref currentLogicalPosition, propInfo);

                propInfo.SetValue(options, value);
                matchedRequiredParameters++;
            } while (currentLogicalPosition < args.Length);

            // no more? do we have any properties that we have not yet set?
            if (TypeArgumentInfo.RequiredArguments.Count != matchedRequiredParameters)
            {
                throw new ArgumentException("Not all required arguments have been specified");
            }
        }
Exemplo n.º 8
0
        private static List <PropertyInfo> ParseOptionalParameters <TOptions>(string[] args, int offsetInArray, ArgumentGroupInfo TypeArgumentInfo, TOptions options, ref int currentLogicalPosition) where TOptions : new()
        {
            // we are going to assume that all optionl parameters are not matched to values in 'args'
            List <PropertyInfo> unmatched = new List <PropertyInfo>(TypeArgumentInfo.OptionalArguments.Values);

            // process the optional arguments
            while (offsetInArray + currentLogicalPosition < args.Length)
            {
                if (args[offsetInArray + currentLogicalPosition][0] != '-')
                {
                    throw new ArgumentException("Optional parameter name should start with '-'");
                }

                PropertyInfo optionalProp      = null;
                var          optionalParamName = args[offsetInArray + currentLogicalPosition].Substring(1);
                if (!TypeArgumentInfo.OptionalArguments.TryGetValue(optionalParamName, out optionalProp))
                {
                    throw new ArgumentException($"Could not find argument {args[currentLogicalPosition]}");
                }

                // skip over the parameter name
                currentLogicalPosition++;

                var value = PropertyHelpers.GetValueFromArgsArray(args, offsetInArray, ref currentLogicalPosition, optionalProp);

                optionalProp.SetValue(options, value);
                unmatched.Remove(optionalProp);
            }

            return(unmatched);
        }
Exemplo n.º 9
0
        private static TOptions InternalParse <TOptions>(string[] args, int offsetInArray, ArgumentGroupInfo arguments) where TOptions : new()
        {
            TOptions options = new TOptions();
            int      currentLogicalPosition = 0;

            // let's match them to actual required args, in positional
            if (arguments.RequiredArguments.Count > 0)
            {
                ParseRequiredParameters(args, offsetInArray, arguments, options, ref currentLogicalPosition);
            }

            // we are going to keep track of any properties that have not been specified so that we can set their default value.
            var unmatchedOptionalProperties = ParseOptionalParameters(args, offsetInArray, arguments, options, ref currentLogicalPosition);

            // for all the remaining optional properties, set their default value.
            foreach (var property in unmatchedOptionalProperties)
            {
                //get the default value..
                var value = property.GetCustomAttribute <OptionalArgumentAttribute>();
                property.SetValue(options, Convert.ChangeType(value.DefaultValue, property.PropertyType));
            }

            return(options);
        }
Exemplo n.º 10
0
        private static TOptions InternalParse <TOptions>(string[] args, int offsetInArray, ArgumentGroupInfo arguments, ParserOptions parseOptions)
            where TOptions : new()
        {
            TOptions options = new TOptions();
            int      currentLogicalPosition = 0;

            // let's match them to actual required args, in positional
            if (arguments.RequiredArguments.Count > 0)
            {
                ParseRequiredParameters(args, offsetInArray, arguments, options, ref currentLogicalPosition);
            }

            // we are going to keep track of any properties that have not been specified so that we can set their default value.
            var unmatchedOptionalProperties = ParseOptionalParameters(args, offsetInArray, arguments, options, ref currentLogicalPosition);

            // for all the remaining optional properties, set their default value.
            foreach (var property in unmatchedOptionalProperties)
            {
                //get the default value..
                var    value        = property.GetCustomAttribute <OptionalArgumentAttribute>();
                object defaultValue = value.DefaultValue;

                // If we want to read values from the environment, try to get the value
                if (parseOptions.ReadFromEnvironment && !value.IsCollection && parseOptions.VariableNamePrefix != null)
                {
                    var envVar = Environment.GetEnvironmentVariable($"{parseOptions.VariableNamePrefix}{value.Name}");

                    if (!string.IsNullOrEmpty(envVar))
                    {
                        defaultValue = PropertyHelpers.GetValueForProperty(envVar, property);
                    }
                }

                property.SetValue(options, Convert.ChangeType(defaultValue, property.PropertyType));
            }

            return(options);
        }
Exemplo n.º 11
0
 private static void DisplayDetailedArgumentHelp(string exeName, string command, ArgumentGroupInfo arguments)
 {
     Colorizer.Write(" [White!{0}.exe] ", exeName);
     if (!string.IsNullOrEmpty(command))
     {
         Colorizer.Write("[Green!{0}] ", command);
     }
     DisplayDetailedParameterHelp(arguments);
 }