Ejemplo n.º 1
0
        /// <summary>
        /// Whether or not the there is a named argument/value associated with the supplied argument.
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        public bool Contains(ArgAttribute arg)
        {
            if (Named == null || Named.Count == 0)
            {
                return(false);
            }

            if (Named.ContainsKey(arg.Name))
            {
                return(true);
            }
            if (Named.ContainsKey(arg.Alias))
            {
                return(true);
            }
            if (arg.IsCaseSensitive)
            {
                return(false);
            }
            if (Named.ContainsKey(arg.NameLowered))
            {
                return(true);
            }
            if (Named.ContainsKey(arg.AliasLowered))
            {
                return(true);
            }
            return(false);
        }
Ejemplo n.º 2
0
        private void AddToLookup(ArgAttribute argAtt)
        {
            if (argAtt.IsNamed)
            {
                // Set the name
                _named[argAtt.Name] = argAtt;
                bool hasAlias = !string.IsNullOrEmpty(argAtt.Alias);

                // Set the alias
                if (hasAlias)
                {
                    _named[argAtt.Alias] = argAtt;
                }

                // Lowercase name and alias
                if (!argAtt.IsCaseSensitive)
                {
                    _named[argAtt.Name.ToLower()] = argAtt;
                }
                if (!argAtt.IsCaseSensitive && hasAlias)
                {
                    _named[argAtt.Alias.ToLower()] = argAtt;
                }
            }
            else
            {
                _positional[argAtt.IndexPosition] = argAtt;
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Validates various aspects of the argument.
        /// </summary>
        /// <param name="argAttr"></param>
        /// <param name="argVal"></param>
        /// <param name="errors"></param>
        public static bool ValidateArg(ArgAttribute argAttr, string argVal, IList <string> errors)
        {
            // Arg name or index.
            string argId             = string.IsNullOrEmpty(argAttr.Name) ? "[" + argAttr.IndexPosition + "]" : argAttr.Name;
            int    initialErrorCount = errors.Count;

            // Argument missing and required.
            if (argAttr.IsRequired && string.IsNullOrEmpty(argVal))
            {
                errors.Add(string.Format("Required argument '{0}' : {1} is missing.", argAttr.Name, argAttr.DataType.FullName));
                return(false);
            }

            // Argument missing and Optional - Can't do much.
            if (!argAttr.IsRequired && string.IsNullOrEmpty(argVal))
            {
                return(true);
            }

            // File doesn't exist.
            if (argAttr.DataType == typeof(System.IO.File) && !System.IO.File.Exists(argVal))
            {
                errors.Add(string.Format("File '{0}' associated with argument '{1}' does not exist.", argVal, argId));
            }

            // Wrong data type.
            else if (!argAttr.Interpret && !Converter.CanConvertTo(argAttr.DataType, argVal))
            {
                errors.Add(string.Format("Argument value of '{0}' for '{1}' does not match type {2}.",
                                         argVal, argId, argAttr.DataType.FullName));
            }

            return(initialErrorCount == errors.Count);
        }
Ejemplo n.º 4
0
        private ArgsSchema Add <T>(int indexPosition, string name, string alias, bool isRequired, T defaultValue, string description, string example, string exampleMultipe, bool interpret,
                                   bool isCaseSensitive, string tag, bool onlyForDevelopment)
        {
            var latest = new ArgAttribute()
            {
                Name                     = name,
                Alias                    = alias,
                DefaultValue             = defaultValue,
                Description              = description,
                DataType                 = typeof(T),
                Example                  = example,
                ExampleMultiple          = exampleMultipe,
                Interpret                = interpret,
                IsCaseSensitive          = isCaseSensitive,
                IsRequired               = isRequired,
                IsUsedOnlyForDevelopment = onlyForDevelopment,
                IndexPosition            = indexPosition,
                Tag = tag
            };

            _attributes.Add(latest);
            AddToLookup(latest);
            _last = latest;

            return(this);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Set the argument value from command line on the property of the object
        /// recieving the value.
        /// </summary>
        /// <param name="argReciever"></param>
        /// <param name="val"></param>
        /// <param name="rawArgValue"></param>
        private static void SetValue(object argReciever, KeyValuePair <ArgAttribute, PropertyInfo> val, string rawArgValue)
        {
            ArgAttribute argAttr = val.Key;

            // First interpret.
            string argValue = rawArgValue;

            if (argAttr.Interpret)
            {
                argValue = Substitute(argValue);
            }
            ReflectionHelper.SetProperty(argReciever, val.Value, argValue);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Validates the index position of the non-named argument.
        /// </summary>
        /// <param name="argAttr"></param>
        /// <param name="positionalArgCount"></param>
        /// <param name="errors"></param>
        /// <returns></returns>
        public static bool ValidateIndex(ArgAttribute argAttr, int positionalArgCount, IList <string> errors)
        {
            // Now check the positional args.
            bool isValidIndex = argAttr.IndexPosition < positionalArgCount;


            // Required and positional arg valid.
            if (argAttr.IsRequired && !isValidIndex)
            {
                errors.Add(string.Format("Positional argument at index : [{0}]' was not supplied.", argAttr.IndexPosition));
            }
            return(isValidIndex);
        }
Ejemplo n.º 7
0
        private static bool IsSameArg(string key, ArgAttribute arg)
        {
            if (string.Compare(key, arg.Name, true) == 0)
            {
                return(true);
            }

            if (string.Compare(key, arg.Alias, true) == 0)
            {
                return(true);
            }

            return(false);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Validate the parsed args supplied with the args specification list.
        /// </summary>
        /// <param name="parsedArgs"></param>
        /// <param name="argSpecs"></param>
        /// <param name="errors"></param>
        /// <param name="onArgumentValidationSuccessCallback"></param>
        public static void Validate(Args parsedArgs, List <ArgAttribute> argSpecs, IList <string> errors,
                                    Action <ArgAttribute, string, int> onArgumentValidationSuccessCallback)
        {
            // Get all the properties that have arg attributes.
            bool hasPositionalArgs  = parsedArgs.Positional != null && parsedArgs.Positional.Count > 0;
            int  positionalArgCount = hasPositionalArgs ? parsedArgs.Positional.Count : 0;

            // Go through all the arg specs.
            for (int ndx = 0; ndx < argSpecs.Count; ndx++)
            {
                ArgAttribute argAttr           = argSpecs[ndx];
                string       argVal            = string.Empty;
                int          initialErrorCount = errors.Count;

                // Named argument. key=value
                if (argAttr.IsNamed)
                {
                    // FIX: Item #	3926 - Case insensitivity not working.
                    argVal = GetNamedArgValue(argAttr, parsedArgs);
                    ValidateArg(argAttr, argVal, errors);
                }
                else
                {
                    // Index argument [0] [1]
                    bool validIndex = ValidateIndex(argAttr, positionalArgCount, errors);
                    if (validIndex)
                    {
                        argVal = parsedArgs.Positional[argAttr.IndexPosition];
                        ValidateArg(argAttr, argVal, errors);
                    }
                }

                // Notify if successful validation of single attribute.
                if (initialErrorCount == errors.Count && onArgumentValidationSuccessCallback != null)
                {
                    onArgumentValidationSuccessCallback(argAttr, argVal, ndx);
                }
            }
        }
Ejemplo n.º 9
0
        private static string GetNamedArgValue(ArgAttribute argAttr, Args args)
        {
            // Case sensitive
            if (argAttr.IsCaseSensitive)
            {
                return(args.Get <string>(argAttr.Name, string.Empty));
            }

            // Not case sensitive. Try to match name.
            string suppliedName = argAttr.Name;

            foreach (var pair in args.Named)
            {
                if (string.Compare(pair.Key, argAttr.Name, true) == 0)
                {
                    suppliedName = pair.Key;
                    break;
                }
            }

            // Get the value based on the correct name.
            return(args.Get <string>(suppliedName, string.Empty));
        }
Ejemplo n.º 10
0
        private static string GetNamedArgValue(ArgAttribute argAttr, Args args)
        {
            // Case sensitive
            if (argAttr.IsCaseSensitive)
            {
                return(args.Get <string>(argAttr.Name, string.Empty));
            }

            // Not case sensitive. Try to match name.
            string suppliedName = argAttr.Name;

            foreach (var pair in args.Named)
            {
                // Can be either alias or full name.
                if (IsSameArg(pair.Key, argAttr))
                {
                    suppliedName = pair.Key;
                    break;
                }
            }

            // Get the value based on the correct name.
            return(args.Get <string>(suppliedName, string.Empty));
        }