Exemple #1
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());
        }
        internal static ArgumentData Parse(PropertyInfo propertyInfo)
        {
            DetailsAttribute  detailsAttr  = propertyInfo.GetCustomAttribute <DetailsAttribute>();
            OptionalAttribute optionalAttr = propertyInfo.GetCustomAttribute <OptionalAttribute>();
            ParamAttribute    paramAttr    = propertyInfo.GetCustomAttribute <ParamAttribute>();

            if (paramAttr == null)
            {
                return(null);
            }

            if (!IsTypeSupported(propertyInfo.PropertyType, paramAttr.Type))
            {
                throw new InvalidOperationException(
                          string.Format("Incompatible type specified for '{0}'", propertyInfo.Name));
            }

            if (string.IsNullOrWhiteSpace(paramAttr.Key) && string.IsNullOrWhiteSpace(paramAttr.LongKey))
            {
                throw new InvalidOperationException(
                          string.Format("Both Key & LongKey cannot be null for property '{0}'.", propertyInfo.Name));
            }

            string       details      = detailsAttr != null ? detailsAttr.Details : null;
            string       defaultValue = optionalAttr != null ? optionalAttr.DefaultValue : null;
            ArgumentData data         = new ArgumentData
            {
                DefaultValue = defaultValue,
                Details      = details,
                Key          = paramAttr.Key,
                LongKey      = paramAttr.LongKey,
                Optional     = optionalAttr != null,
                Property     = propertyInfo,
                Type         = paramAttr.Type,
            };

            return(data);
        }