Exemple #1
0
        static int Main(string[] args)
        {
            CliCommand command = null;

            try
            {
                CliModel model = CliModel.Parse(args);

                if (model.Debugger)
                {
                    Debugger.Launch();
                    Debugger.Break();
                }

                command = model.CreateCommand();
                CliMapper.Map(command, model.Arguments.ToArray(), model.Options);

                if (command.Help)
                {
                    foreach (var line in command.Usage())
                    {
                        Con.WriteLine(line);
                    }

                    return(0);
                }
                else
                {
                    return(command.Run());
                }
            }
            catch (NPlantConsoleUsageException usageException)
            {
                Con.WriteLine("Usage Error:");
                Con.WriteLine(usageException.Message);
                Con.WriteLine();

                if (command == null)
                {
                    Con.WriteLine("Available command:");

                    foreach (var commandType in CliCommand.AvailableCommandTypes)
                    {
                        var parts = commandType.Name.SplitOnPascalCasing();

                        if (string.Equals(parts[parts.Length - 1], "Command", StringComparison.CurrentCultureIgnoreCase))
                        {
                            parts[parts.Length - 1] = "";
                        }

                        string commandName = string.Join(" ", parts);

                        Con.WriteLine($"NPlant.Console.exe {commandName.ToLower()}");
                    }
                }
                else
                {
                    foreach (var line in command.Usage())
                    {
                        Con.WriteLine(line);
                    }
                }
            }
            catch (Exception consoleException)
            {
                Con.WriteLine("Fatal Error:");
                Con.WriteLine(consoleException);
            }

            return(1);
        }
Exemple #2
0
        public static void Map(CliCommand instance, string[] arguments, IEnumerable <string> options)
        {
            var properties = new HashSet <PropertyInfo>(instance.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public));

            foreach (var option in options)
            {
                var optionParts = option.Split(':');

                string optionName  = optionParts.First();
                string optionValue = null;

                if (optionParts.Length > 1)
                {
                    optionValue = string.Join(":", optionParts.Where((argPart, index) => index > 0));
                }

                if (!optionName.StartsWith("--"))
                {
                    throw new NPlantConsoleUsageException(
                              $"Arguments are expected to be in the --foo or --foo:value or --foo:\"value\" format.  Argument '{optionName}' could not be parsed.");
                }

                optionName = optionName.Substring(2);
                var property = properties.FirstOrDefault(x => string.Equals(x.Name, optionName, StringComparison.InvariantCultureIgnoreCase));

                if (property == null)
                {
                    throw new NPlantConsoleUsageException($"Option '{optionName}' is not recognized.");
                }

                if (optionValue != null)
                {
                    property.SetConvertedValue(instance, optionValue);
                    properties.Remove(property);
                }
                else
                {
                    if (property.PropertyType == typeof(bool) || property.PropertyType == typeof(bool?))
                    {
                        property.SetValue(instance, true, null);
                        properties.Remove(property);
                    }
                }
            }

            foreach (var property in properties.Where(x => x.HasAttribute <CliArgumentAttribute>()))
            {
                var attribute = property.GetAttributesOf <CliArgumentAttribute>()[0];
                var order     = attribute.Order;
                var allowed   = attribute.Allowed;

                if (order > arguments.Length)
                {
                    throw new NPlantConsoleUsageException($"Command argument requirements could not be satisfied - Argument in position {order} was not found.");
                }

                string argumentValue = arguments[order - 1];

                if (allowed != null && allowed.All(x => x != argumentValue))
                {
                    throw new NPlantConsoleUsageException($"Unexpected argument value '{argumentValue}' in position {order} - expected one of the following:  {string.Join(", ", allowed)}");
                }

                property.SetConvertedValue(instance, argumentValue);
            }
        }