Example #1
0
        /// <summary>
        /// Parses the arguments
        /// </summary>
        /// <param name="args">e.g. "-config:prod.xml", "-date:T-1", "BloombergFutOpt"</param>
        /// <param name="prefix">Character used to prefix the name
        /// of a named parameter. e.g. "-" as in "-config:prod.xml"
        /// Leave null or string.emtpy to indicate there is no prefix.
        /// In which case, only the <paramref name="separator"/> is used to distinguish
        /// namevalue pairs.</param>
        /// <param name="separator">Character used to separate the named
        /// argument with it's value in a named argument. e.g. ":" as in "-config:prod.xml"
        /// If this is null or string.empty, in addition to the <paramref name="prefix"/></param>
        /// <returns></returns>
        public static BoolMessageItem <Args> Parse(string[] args, string prefix, string separator)
        {
            // Validate the inputs.
            BoolMessageItem <Args> validationResult = ArgsValidator.ValidateInputs(args, prefix, separator);

            if (!validationResult.Success)
            {
                return(validationResult);
            }

            bool checkNamedArgs = !string.IsNullOrEmpty(separator);
            bool hasPrefix      = !string.IsNullOrEmpty(prefix);

            // Name of argument can only be letter, number, (-,_,.).
            // The value can be anything.
            string patternKeyValue = @"(?<name>[a-zA-Z0-9\-_\.]+)" + separator + @"(?<value>.+)";
            string patternBool     = @"(?<name>[a-zA-Z0-9\-_\.]+)";

            patternKeyValue = hasPrefix ? prefix + patternKeyValue : patternKeyValue;
            patternBool     = hasPrefix ? prefix + patternBool : patternBool;

            Args          resultArgs = new Args(args, prefix, separator);
            List <string> errors     = new List <string>();

            // Put the arguments into the named args dictionary and/or list.
            if (checkNamedArgs)
            {
                Parse(args, resultArgs.Named, resultArgs.Positional, patternKeyValue, patternBool);
            }
            else
            {
                resultArgs.Positional = new List <string>(args);
            }
            return(new BoolMessageItem <Args>(resultArgs, true, string.Empty));
        }
Example #2
0
        /// <summary>
        /// Parses the arguments and checks for named arguments and non-named arguments.
        /// </summary>
        /// <param name="args">e.g. "-config:prod.xml", "-date:T-1", "BloombergFutOpt"</param>
        /// <param name="prefix">Character used to identifiy the name
        /// of a named parameter. e.g. "-" as in "-config:prod.xml"
        /// Leave null or string.emtpy to indicate there is no prefix.
        /// In which case, only the <paramref name="separator"/> is used to distinguish
        /// namevalue pairs.</param>
        /// <param name="separator">Character used to separate the named
        /// argument with it's value in a named argument. e.g. ":" as in "-config:prod.xml"
        /// If this is null or string.empty, in addition to the <paramref name="prefix"/></param>
        /// <param name="argsSpec">List of expected argument definitions(both named and positional).</param>
        /// <returns></returns>
        public static BoolMessageItem <Args> Parse(string[] args, string prefix, string separator, List <ArgAttribute> argsSpec)
        {
            // Parse the args first.
            BoolMessageItem <Args> parseResult = Parse(args, prefix, separator);

            if (!parseResult.Success)
            {
                return(parseResult);
            }

            Args          resultArgs = parseResult.Item;
            List <string> errors     = new List <string>();

            // Set the named argument values on the object's properties.
            if (argsSpec != null && argsSpec.Count > 0)
            {
                ArgsValidator.Validate(resultArgs, argsSpec, errors, null);
            }
            string singleMessage = string.Empty;

            foreach (string error in errors)
            {
                singleMessage += error + Environment.NewLine;
            }

            return(new BoolMessageItem <Args>(resultArgs, errors.Count == 0, singleMessage));
        }
Example #3
0
        /// <summary>
        /// Parses the arguments into a <see cref="Args"/>
        /// </summary>
        /// <param name="args">e.g. "-env:prod", "-config:prod.xml", "-date:T-1", "20"</param>
        /// <param name="prefix">Prefix used for named arguments. E.g. "-" as in "-env:prod"</param>
        /// <param name="separator">Separator used between name and value of named arguments. E.g. ":" as in "-env:prod"</param>
        /// <param name="argsSpec">List of expected argument items(both named and positional).</param>
        /// <returns></returns>
        public static BoolMessageItem <Args> Parse(string[] args, string prefix, string separator, List <ArgAttribute> argsSpec)
        {
            // Parse the args first.
            BoolMessageItem <Args> parseResult = Parse(args, prefix, separator);

            if (!parseResult.Success)
            {
                return(parseResult);
            }

            Args resultArgs = parseResult.Item;

            // 1. Set the schema.
            resultArgs.Schema.Items = argsSpec;

            // 2. Parse interpreted values like ${today}.
            ArgsHelper.InterpretValues(resultArgs);

            // 3. Validate the values.
            var errors = new List <string>();

            if (argsSpec != null && argsSpec.Count > 0)
            {
                ArgsValidator.Validate(resultArgs, argsSpec, errors, null);
            }
            string singleMessage = string.Empty;

            foreach (string error in errors)
            {
                singleMessage += error + Environment.NewLine;
            }

            return(new BoolMessageItem <Args>(resultArgs, errors.Count == 0, singleMessage));
        }
        /// <summary>
        /// Applies the argument values to the object argument reciever.
        /// </summary>
        /// <param name="parsedArgs"></param>
        /// <param name="argReciever"></param>
        /// <param name="errors"></param>
        public static void CheckAndApplyArgs(Args parsedArgs, object argReciever, IList <string> errors)
        {
            List <KeyValuePair <ArgAttribute, PropertyInfo> > mappings = Attributes.GetPropsWithAttributesList <ArgAttribute>(argReciever);
            List <ArgAttribute> argSpecs = new List <ArgAttribute>();

            mappings.ForEach((pair) => argSpecs.Add(pair.Key));

            // Set the supplied argument value on the object that should recieve the value.
            ArgsValidator.Validate(parsedArgs, argSpecs, errors, (argAttr, argVal, ndx) =>
            {
                SetValue(argReciever, mappings[ndx], argVal);
            });
        }