예제 #1
0
        /// <summary>
        /// Internal helper method for parsing arguments
        /// </summary>
        /// <returns>True if all arguments were successfully parsed</returns>
        internal bool Parse()
        {
            while (++Position < Arguments.Length)
            {
                Tuple <string, string> param = ArgumentParserUtils.ParseParam(Arguments[Position]);
                if (param == null || !Data.ContainsKey(param.Item1))
                {
                    if (Arguments[Position].Equals("/?"))
                    {
                        NeedHelp = true;
                        return(false);
                    }

                    LogHelper.LogError("Unrecogized parameter '{0}'.", Arguments[Position]);
                    return(false);
                }

                if (param.Item1.Equals("h", StringComparison.OrdinalIgnoreCase) ||
                    param.Item1.Equals("help", StringComparison.OrdinalIgnoreCase))
                {
                    NeedHelp = true;
                    return(false);
                }

                ArgumentData argumentData = Data[param.Item1];
                if (argumentData.Seen)
                {
                    LogHelper.LogError("Argument '{0}' re-defined with value '{1}'.", param.Item1, param.Item2);
                    return(false);
                }

                argumentData.Seen = true;
                if (!HandleArgument(argumentData, param.Item2))
                {
                    return(false);
                }
            }

            foreach (ArgumentData argumentData in Data.Values.Where(d => !d.Seen))
            {
                if (!argumentData.Optional)
                {
                    LogHelper.LogError("Argument [{0}] is required.", argumentData.Name);
                    return(false);
                }

                if (!HandleDefaultValue(argumentData))
                {
                    return(false);
                }
            }

            return(true);
        }
예제 #2
0
        /// <summary>
        /// Generates help text based on custom arguments class
        /// </summary>
        internal static string Usage()
        {
            IDictionary <string, ArgumentData> data = ArgumentParserUtils.GetArgumentData <T>();
            DetailsAttribute detailsAttr            = typeof(T).GetCustomAttribute <DetailsAttribute>();
            string           details  = detailsAttr != null ? detailsAttr.Details : string.Empty;
            ITemplate        template = new CommandLineUsage
            {
                Name        = Process.GetCurrentProcess().ProcessName,
                Description = details,
                Required    = data.Values.Where(x => !x.Optional).Distinct(),
                Optional    = data.Values.Where(x => x.Optional).Distinct(),
            };

            return(template.Print());
        }
예제 #3
0
        /// <summary>
        /// Returns an instance of custom parameters class
        /// </summary>
        /// <param name="arg">List of arguments to parse</param>
        /// <returns>Parsed arguments</returns>
        public static T Parse(string[] arg)
        {
            var parser = new ArgumentParser <T>(arg, ArgumentParserUtils.GetArgumentData <T>());

            if (!parser.Parse())
            {
                // TODO: Remove calls to PrintUsage from consumers and uncomment below
                //if(parser.NeedHelp)
                //{
                //    PrintUsage();
                //}

                return(default(T));
            }


            return(parser.Result);
        }
예제 #4
0
        /// <summary>
        /// Handler for single param arguments
        /// </summary>
        /// <param name="argument"><see cref="ArgumentData"/></param>
        /// <param name="value">Value of argument</param>
        /// <returns>True if it was successfully handled</returns>
        private bool HandleParam(ArgumentData argument, string value)
        {
            // Param is specified as key:value or key=value
            if (!string.IsNullOrWhiteSpace(value))
            {
                return(Result.TrySetPropertyValue(argument.Property, value));
            }

            // Value should be the next argumentKey
            if (++Position < Arguments.Length)
            {
                Tuple <string, string> nextParam = ArgumentParserUtils.ParseParam(Arguments[Position]);
                if (nextParam == null)
                {
                    return(Result.TrySetPropertyValue(argument.Property, Arguments[Position]));
                }
            }

            Position--;
            LogHelper.LogError("Value for parameter '{0}' not specified", argument.Key);
            return(false);
        }