예제 #1
0
 private string GetPropertyErrorDescription(ArgumentParsed argumentParsed)
 {
     if (argumentParsed.ParsingStates.HasFlag(ArgumentParsedState.ArgumentAlreadyBeenSet))
     {
         return(string.Format(Strings.ArgumentAlreadyBeenSet, argumentParsed.GetArgumentNameInputted()));
     }
     if (argumentParsed.ParsingStates.HasFlag(ArgumentParsedState.ArgumentNotExistsByName))
     {
         return(string.Format(Strings.ArgumentNotExistsByName, argumentParsed.GetArgumentNameInputted()));
     }
     if (argumentParsed.ParsingStates.HasFlag(ArgumentParsedState.ArgumentNotExistsByValue))
     {
         return(string.Format(Strings.ArgumentNotExistsByValue, argumentParsed.Raw));
     }
     if (argumentParsed.ParsingStates.HasFlag(ArgumentParsedState.ArgumentIsRequired))
     {
         return(string.Format(Strings.ArgumentIsRequired, argumentParsed.GetArgumentNameInputted()));
     }
     if (argumentParsed.ParsingStates.HasFlag(ArgumentParsedState.ArgumentHasInvalidInput))
     {
         return(string.Format(Strings.ArgumentHasInvalidInput, argumentParsed.GetArgumentNameInputted()));
     }
     if (argumentParsed.ParsingStates.HasFlag(ArgumentParsedState.ArgumentHasUnsupportedType))
     {
         return(string.Format(Strings.ArgumentHasUnsupportedType, argumentParsed.GetArgumentNameInputted()));
     }
     return(null);
 }
예제 #2
0
        /// <summary>
        /// Initialize
        /// </summary>
        /// <param name="argumentParsed">ArgumentParsed of this method</param>
        public PropertyResult(ArgumentParsed argumentParsed)
        {
            this.ArgumentParsed = argumentParsed;
            this.Name           = this.ArgumentParsed.Name;

            if (argumentParsed.Map != null)
            {
                this.PropertyInfo = (PropertyInfo)argumentParsed.Map.TargetMember;
                this.Value        = argumentParsed.Value;
                this.Target       = argumentParsed.Map.Target;
            }
        }
예제 #3
0
        public string HistoryList()
        {
            var strBuilder = new StringBuilder();
            var histories  = this.FileManager.GetOrCreate <List <History> >(FILE_NAME);
            var table      = new TableView();

            table.AddLineSeparator = false;

            foreach (var history in histories)
            {
                var newArgs = history.Args.Select(arg => ArgumentParsed.GetValueRaw(arg));
                table.AddRowSummary("[" + history.Name + "] " + string.Join(" ", newArgs));
            }

            return(table
                   .Build()
                   .ToString());
        }
예제 #4
0
        private ArgumentParsedState GetState(ArgumentParsed arg, IEnumerable <ArgumentParsed> argumentsMapped)
        {
            if (arg.ParsingType == ArgumentParsedType.NotMapped)
            {
                if (arg.AllRaw.First().Format != ArgumentFormat.Unnamed)
                {
                    var hasMappedBefore = argumentsMapped.Any(f => f.IsMapped && arg.Name.In(f.Map.LongName, f.Map.ShortName.ToString()) && f != arg);
                    if (hasMappedBefore)
                    {
                        return(ArgumentParsedState.ArgumentAlreadyBeenSet | ArgumentParsedState.IsInvalid);
                    }
                    else
                    {
                        return(ArgumentParsedState.ArgumentNotExistsByName | ArgumentParsedState.IsInvalid);
                    }
                }
                else
                {
                    return(ArgumentParsedState.ArgumentNotExistsByValue | ArgumentParsedState.IsInvalid);
                }
            }
            else if (!arg.Map.HasDefaultValue && arg.ParsingType == ArgumentParsedType.HasNoInput)
            {
                if (arg.Map.IsOptional)
                {
                    return(ArgumentParsedState.ArgumentIsNotRequired | ArgumentParsedState.IsInvalid);
                }
                else
                {
                    return(ArgumentParsedState.ArgumentIsRequired | ArgumentParsedState.IsInvalid);
                }
            }
            else if (arg.IsMapped && arg.HasInvalidInput)
            {
                return(ArgumentParsedState.ArgumentHasInvalidInput | ArgumentParsedState.IsInvalid);
            }
            else if (arg.IsMapped && arg.HasUnsuporttedType)
            {
                return(ArgumentParsedState.ArgumentHasUnsupportedType | ArgumentParsedState.IsInvalid);
            }

            return(ArgumentParsedState.Valid);
        }
예제 #5
0
        /// <summary>
        /// Create a list of argument that are required or have default value
        /// </summary>
        /// <param name="argsMaps">List of ArgumentMap</param>
        /// <returns>List of ArgumentParsed</returns>
        public IEnumerable <ArgumentParsed> CreateArgumentsDefaultValueOrRequired(IEnumerable <ArgumentMap> argsMaps)
        {
            var list = new List <ArgumentParsed>();

            foreach (var mapWithoutInput in argsMaps)
            {
                var argMapped = new ArgumentParsed(mapWithoutInput.MapName, null, null, mapWithoutInput.Type, mapWithoutInput);

                if (mapWithoutInput.HasDefaultValue)
                {
                    argMapped.ParsingType = ArgumentParsedType.DefaultValue;
                    argMapped.Value       = mapWithoutInput.DefaultValue;
                    argMapped.ValueParsed = mapWithoutInput.DefaultValue;
                }
                else
                {
                    argMapped.ParsingType = ArgumentParsedType.HasNoInput;
                }

                list.Add(argMapped);
            }
            return(list);
        }
예제 #6
0
        private bool AllRawAreEqualsInArgumentParsed(ArgumentParsed argA, ArgumentParsed argB)
        {
            if (argA == null || argB == null)
            {
                return(false);
            }

            var countA = argA.AllRaw.Count();
            var countB = argB.AllRaw.Count();

            if (countA + countB == 0)
            {
                return(true);
            }

            if (countA < countB)
            {
                return(false);
            }

            if (countA > 0)
            {
                for (var i = 0; i < countA; i++)
                {
                    var rawA = argA.AllRaw.ElementAt(i);
                    var rawB = argB.AllRaw.ElementAtOrDefault(i);
                    if (rawA != rawB)
                    {
                        return(false);
                    }
                }

                return(true);
            }

            return(false);
        }
예제 #7
0
        /// <summary>
        /// Create a list of argument parsed from the specific target object
        /// </summary>
        /// <param name="argumentsRaw">List of arguments raw</param>
        /// <param name="enablePositionalArgs">Determine whether the parser will consider the position</param>
        /// <param name="maps">Map of arguments</param>
        /// <returns>List of ArgumentParsed</returns>
        public IEnumerable <ArgumentParsed> Parse(IEnumerable <ArgumentRaw> argumentsRaw, bool enablePositionalArgs, IEnumerable <ArgumentMap> maps)
        {
            if (argumentsRaw == null)
            {
                throw new ArgumentNullException("argumentsRaw");
            }

            if (maps == null)
            {
                throw new ArgumentNullException("maps");
            }

            var argumentsMappeds = new List <ArgumentParsed>();
            var mapsUseds        = maps.ToList();

            var i = 0;

            using (IEnumerator <ArgumentRaw> enumerator = argumentsRaw.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    var argRaw = enumerator.Current;
                    var map    = mapsUseds.FirstOrDefault(m =>
                    {
                        if (argRaw.IsLongName)
                        {
                            return(m.LongName == argRaw.Name);
                        }
                        else if (argRaw.IsShortName)
                        {
                            return(m.ShortName == argRaw.Name[0]);
                        }
                        else
                        {
                            return(false);
                        }
                    });

                    if (enablePositionalArgs && map == null && argRaw.Format == ArgumentFormat.Unnamed)
                    {
                        map = mapsUseds.FirstOrDefault();
                    }

                    if (map != null)
                    {
                        var argMapped = new ArgumentParsed(map.MapName, ArgumentParsed.GetValueRaw(argRaw.Value), null, map.Type, map);
                        argMapped.AddRaw(argRaw);

                        if (argRaw.Format == ArgumentFormat.Unnamed)
                        {
                            argMapped.ParsingType = ArgumentParsedType.Position;
                        }
                        else
                        {
                            argMapped.ParsingType = ArgumentParsedType.Name;
                        }

                        argumentsMappeds.Add(argMapped);
                        ProcessValue(enumerator, argumentsRaw, ref i, argRaw, map, argMapped);

                        mapsUseds.Remove(map);
                    }
                    else
                    {
                        var argMapped = new ArgumentParsed(argRaw.Name, ArgumentParsed.GetValueRaw(argRaw.Value), argRaw.Value, typeof(string), null);
                        argMapped.AddRaw(argRaw);
                        argMapped.ParsingType = ArgumentParsedType.NotMapped;
                        argumentsMappeds.Add(argMapped);
                    }

                    i++;
                }
            }

            argumentsMappeds.AddRange(this.CreateArgumentsDefaultValueOrRequired(mapsUseds));
            this.SetState(argumentsMappeds);
            return(argumentsMappeds);
        }
예제 #8
0
        private void ProcessValue(IEnumerator <ArgumentRaw> enumerator, IEnumerable <ArgumentRaw> argumentsRaw, ref int i, ArgumentRaw argRaw, ArgumentMap map, ArgumentParsed argMapped)
        {
            Type typeOriginal = ReflectionHelper.GetTypeOrTypeOfNullable(map.Type);

            if (argRaw.Value == null && typeOriginal != typeof(bool))
            {
                argMapped.Value = ReflectionHelper.GetDefaultForType(map.Type);
            }
            else
            {
                var          value                = argRaw.Value;
                var          hasInvalidInput      = false;
                var          hasUnsuporttedType   = false;
                object       valueConverted       = null;
                var          iDelegate            = i;
                Action <int> actionConvertSuccess = (int position) =>
                {
                    if (position > 0)
                    {
                        enumerator.MoveNext();
                        iDelegate++;
                        argMapped.AddRaw(enumerator.Current);
                    }
                };

                if (ReflectionHelper.IsEnum(map.Type))
                {
                    var values = new List <string>()
                    {
                        argRaw.Value
                    };
                    values.AddRange(GetUnamedValues(argumentsRaw, i + 1));
                    var valueArray = values.ToArray();
                    argMapped.ValueParsed = ArgumentParsed.GetValueRaw(valueArray);
                    valueConverted        = ConverterHelper.TryConvertEnum(map.Type, valueArray, out hasInvalidInput, actionConvertSuccess);
                    i = iDelegate;
                }
                else if (map.Type != typeof(string) && typeof(IEnumerable).IsAssignableFrom(map.Type))
                {
                    var values = new List <string>()
                    {
                        argRaw.Value
                    };
                    values.AddRange(GetUnamedValues(argumentsRaw, i + 1));
                    var valueArray = values.ToArray();
                    argMapped.ValueParsed = ArgumentParsed.GetValueRaw(valueArray);

                    valueConverted = ConverterHelper.TryConvertCollection(map.Type, valueArray, out hasInvalidInput, out hasUnsuporttedType, actionConvertSuccess);
                    i = iDelegate;
                }
                else
                {
                    valueConverted = ConverterHelper.TryConvertPrimitives(map.Type, value, out hasInvalidInput, out hasUnsuporttedType);
                }

                argMapped.HasInvalidInput    = hasInvalidInput;
                argMapped.HasUnsuporttedType = hasUnsuporttedType;

                if (!hasInvalidInput && !hasUnsuporttedType)
                {
                    argMapped.Value = valueConverted;
                }
            }
        }