示例#1
0
        private void RegisterOption(string currentCommand, ArgumentProperty argumentProperty, OptionAttribute option)
        {
            _optionNames.Add(option.ShortName);
            _optionNames.Add(option.LongName);

            _argumentProperties[currentCommand][option.ShortName] = argumentProperty;
            _argumentProperties[currentCommand][option.LongName]  = argumentProperty;
        }
示例#2
0
        private void RegisterFlag(string currentCommand, ArgumentProperty argumentProperty, FlagAttribute flag)
        {
            _flagNames.Add(flag.ShortName);
            _flagNames.Add(flag.LongName);

            _argumentProperties[currentCommand][flag.ShortName] = argumentProperty;
            _argumentProperties[currentCommand][flag.LongName]  = argumentProperty;
        }
示例#3
0
 private void RegisterValue(string currentCommand, ArgumentProperty argumentProperty, ValueAttribute value)
 {
     _valueNames.Add(value.Name);
     if (!_valueProperties.ContainsKey(currentCommand))
     {
         _valueProperties[currentCommand] = new Queue <ArgumentProperty>();
     }
     _valueProperties[currentCommand].Enqueue(argumentProperty);
 }
示例#4
0
        /// <summary>
        /// Registers a type into the parser.
        /// </summary>
        /// <typeparam name="T">The type being registered.</typeparam>
        /// <param name="callback">A callback which will be executed when the values of the registered type have been parsed.</param>
        /// <returns></returns>
        public Parser Register <T>(Action <T> callback) where T : class, new()
        {
            var argumentsType    = typeof(T);
            var commandAttribute = argumentsType.GetTypeInfo()
                                   .GetCustomAttribute <CommandAttribute>();

            if (_currentArguments != null && commandAttribute == null)
            {
                throw new MultipleTopLevelArgumentsNotAllowedException();
            }
            var currentCommand = string.Empty;

            if (commandAttribute == null)
            {
                _commandNames.Add(string.Empty);
                _commandArguments[string.Empty] = new T();
                _currentArguments            = _commandArguments[string.Empty];
                _commandHelp[currentCommand] = string.Empty;
            }
            else
            {
                if (_commandArguments.ContainsKey(commandAttribute.Name))
                {
                    throw new InvalidOperationException($"Command whith name \"{commandAttribute.Name}\" has already been registered.");
                }

                _commandNames.Add(commandAttribute.Name);
                _commandArguments[commandAttribute.Name] = new T();
                currentCommand = commandAttribute.Name;
                _commandHelp[currentCommand] = $"{currentCommand}{Environment.NewLine}\t{commandAttribute.Description}{Environment.NewLine}";
            }

            var properties = argumentsType
                             .GetRuntimeProperties()
                             .ToArray();

            foreach (var property in properties)
            {
                var argumentAttributes = property.GetCustomAttributes <ArgumentAttribute>().ToArray();
                if (argumentAttributes.Length > 1)
                {
                    throw new MultipleArgumentAttributesNotAllowedException();
                }

                if (!_argumentProperties.ContainsKey(currentCommand))
                {
                    _argumentProperties[currentCommand] = new Dictionary <string, ArgumentProperty>();
                }

                if (argumentAttributes.Length == 1)
                {
                    var attribute = argumentAttributes[0];

                    var argumentProperty = new ArgumentProperty
                    {
                        Argument = attribute,
                        Property = property
                    };

                    switch (attribute)
                    {
                    case OptionAttribute option:
                        RegisterOption(currentCommand, argumentProperty, option);
                        _commandHelp[currentCommand] += $"\t{option.ShortName}|{option.LongName} {attribute.Description}{Environment.NewLine}";
                        break;

                    case FlagAttribute flag:
                        if (property.PropertyType != typeof(bool))
                        {
                            throw new InvalidAttributeUsageException($"{nameof(FlagAttribute)} can only be used on boolean properties.");
                        }

                        RegisterFlag(currentCommand, argumentProperty, flag);
                        _commandHelp[currentCommand] += $"\t{flag.ShortName}|{flag.LongName} {attribute.Description}{Environment.NewLine}";
                        break;

                    case ValueAttribute value:
                        RegisterValue(currentCommand, argumentProperty, value);
                        _commandHelp[currentCommand] += $"\t{value.Name} {attribute.Description}{Environment.NewLine}";
                        break;
                    }
                }
            }

            _argumentCallbacks[typeof(T)] = callback;
            return(this);
        }
示例#5
0
        /// <summary>
        /// Registers a type into the parser.
        /// </summary>
        /// <typeparam name="T">The type being registered.</typeparam>
        /// <param name="callback">A callback which will be executed when the values of the registered type have been parsed.</param>
        /// <returns></returns>
        public Parser Register <T>(Action <T> callback) where T : class, new()
        {
            var argumentsType    = typeof(T);
            var commandAttribute = argumentsType.GetTypeInfo()
                                   .GetCustomAttribute <CommandAttribute>();

            if (_currentArguments != null && commandAttribute == null)
            {
                throw new MultipleTopLevelArgumentsNotAllowedException();
            }
            var currentCommand     = string.Empty;
            var currentCommandHelp = new StringBuilder();

            if (commandAttribute == null)
            {
                _commandNames.Add(string.Empty);
                _commandArguments[string.Empty] = new T();
                _currentArguments = _commandArguments[string.Empty];
                currentCommandHelp.AppendLine($"Displaying help for application");
                currentCommandHelp.AppendLine();
            }
            else
            {
                if (_commandArguments.ContainsKey(commandAttribute.Name))
                {
                    throw new Exception($"Command whith name {commandAttribute.Name} has already been registered.");
                }
                _commandNames.Add(commandAttribute.Name);
                _commandArguments[commandAttribute.Name] = new T();
                currentCommand = commandAttribute.Name;
                currentCommandHelp.AppendLine(
                    $"Displaying help for command \"{currentCommand}\": {commandAttribute.Description}");
                currentCommandHelp.AppendLine();
                //currentCommandHelp.AppendLine($"{currentCommand, -30} {commandAttribute.Description}");
            }

            var properties = argumentsType
                             .GetRuntimeProperties()
                             .ToArray();

            foreach (var property in properties)
            {
                var argumentAttributes = property.GetCustomAttributes <ArgumentAttribute>().ToArray();
                if (argumentAttributes.Length > 1)
                {
                    throw new MultipleArgumentAttributesNotAllowedException();
                }

                if (!_argumentProperties.ContainsKey(currentCommand))
                {
                    _argumentProperties[currentCommand] =
                        new Dictionary <string, ArgumentProperty>();
                }

                if (argumentAttributes.Length == 1)
                {
                    var attribute = argumentAttributes[0];

                    var argumentProperty = new ArgumentProperty
                    {
                        Argument = attribute,
                        Property = property
                    };

                    if (attribute is OptionAttribute option)
                    {
                        _optionNames.Add(option.ShortName);
                        _optionNames.Add(option.LongName);

                        _argumentProperties[currentCommand][option.ShortName] = argumentProperty;
                        _argumentProperties[currentCommand][option.LongName]  = argumentProperty;

                        currentCommandHelp.AppendLine($"\t{ "-" + option.ShortName + " | --" + option.LongName, -30 } { option.Description }");
                    }
                    else if (attribute is FlagAttribute flag)
                    {
                        if (property.PropertyType != typeof(bool))
                        {
                            throw new InvalidAttributeUsageException($"{nameof(FlagAttribute)} can only be used on boolean properties.");
                        }

                        _flagNames.Add(flag.ShortName);
                        _flagNames.Add(flag.LongName);

                        _argumentProperties[currentCommand][flag.ShortName] = argumentProperty;
                        _argumentProperties[currentCommand][flag.LongName]  = argumentProperty;

                        currentCommandHelp.AppendLine($"\t{ "-" + flag.ShortName + " | --" + flag.LongName, -30 } { flag.Description }");
                    }
                    else if (attribute is ValueAttribute value)
                    {
                        _valueNames.Add(value.Name);
                        if (!_valueProperties.ContainsKey(currentCommand))
                        {
                            _valueProperties[currentCommand] = new Queue <ArgumentProperty>();
                        }
                        _valueProperties[currentCommand].Enqueue(argumentProperty);
                        currentCommandHelp.AppendLine($"\t{ value.Name, -30 } { value.Description }");
                    }
                }
            }

            _commandHelp[currentCommand]  = currentCommandHelp.ToString();
            _argumentCallbacks[typeof(T)] = callback;
            return(this);
        }