public UnitaryHandler(string title, string description, IArgumentType arguments, IArgumentType results, int time)
     : base(title, description)
 {
     this.arguments = arguments;
     this.results   = results;
     this.time      = time;
 }
 // makes copy of searched
 public ActionConversion(PluginEnvironment plugenv, IArgumentType resultType, Dictionary<IAction, int> searched, Aborter aborter)
 {
     this.plugenv = plugenv;
     this.resultType = resultType;
     this.searched = new Dictionary<IAction, int>(searched);
     this.aborter = aborter;
 }
Example #3
0
 // makes copy of searched
 public ActionConversion(PluginEnvironment plugenv, IArgumentType resultType, Dictionary <IAction, int> searched, Aborter aborter)
 {
     this.plugenv    = plugenv;
     this.resultType = resultType;
     this.searched   = new Dictionary <IAction, int>(searched);
     this.aborter    = aborter;
 }
Example #4
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="innerType">The base IArgumentType instance to
 /// wrap and extend.</param>
 /// <param name="parser">Optionally provides an override implementation
 /// of the base argument type's string parser implementation.</param>
 /// <param name="formatter">Optionally provides an override
 /// implementation of the base argument type's object formatter
 /// implementation.</param>
 /// <param name="completer">Optionally provides an override
 /// implementation of the base argument type's string completer
 /// implementation.</param>
 public ArgumentTypeExtension(IArgumentType innerType, IStringParser parser, IObjectFormatter formatter, IStringCompleter completer)
 {
     InnerType = innerType;
     Parser = parser;
     Formatter = formatter;
     Completer = completer;
 }
 public UnitaryHandler(string title, string description, IArgumentType arguments, IArgumentType results, int time)
     : base(title, description)
 {
     this.arguments = arguments;
     this.results = results;
     this.time = time;
 }
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="innerType">The base IArgumentType instance to
 /// wrap and extend.</param>
 /// <param name="parser">Optionally provides an override implementation
 /// of the base argument type's string parser implementation.</param>
 /// <param name="formatter">Optionally provides an override
 /// implementation of the base argument type's object formatter
 /// implementation.</param>
 /// <param name="completer">Optionally provides an override
 /// implementation of the base argument type's string completer
 /// implementation.</param>
 public ArgumentTypeExtension(IArgumentType innerType, IStringParser parser = null, IObjectFormatter formatter = null, IStringCompleter completer = null)
 {
     InnerType = innerType;
     Parser    = parser;
     Formatter = formatter;
     Completer = completer;
 }
Example #7
0
 /// <summary>
 /// Checks if this validation attributes accepts values of the specified
 /// type.
 /// </summary>
 /// <param name="type">Type to check.</param>
 /// <returns>True if this attribute accepts values of the specified
 /// type; false if not.</returns>
 /// <exception cref="ArgumentNullException">Thrown when <paramref name="type" />
 /// is null.</exception>
 public sealed override bool AcceptsType(IArgumentType type)
 {
     if (type == null)
     {
         throw new ArgumentNullException(nameof(type));
     }
     return((type.Type == typeof(string)) || type.Type.IsEffectivelySameAs(typeof(FileSystemPath)));
 }
Example #8
0
        /// <summary>
        /// Primary constructor.
        /// </summary>
        /// <param name="member">Field to describe.</param>
        /// <param name="attribute">Argument attribute on the field.</param>
        /// <param name="setAttribute">Attribute on the containing argument set.</param>
        /// <param name="options">Provides parser options.</param>
        /// <param name="defaultFieldValue">Default value for the field.</param>
        /// <param name="parentMember">Parent member under which this field sits.</param>
        /// <param name="fixedDestination">Optionally provides fixed parse destination object.</param>
        public ArgumentDefinition(IMutableMemberInfo member,
                                  ArgumentBaseAttribute attribute,
                                  ArgumentSetAttribute setAttribute,
                                  CommandLineParserOptions options,
                                  object defaultFieldValue        = null,
                                  IMutableMemberInfo parentMember = null,
                                  object fixedDestination         = null)
        {
            Debug.Assert(attribute != null);
            Debug.Assert(member != null);

            Member                = member;
            ParentMember          = parentMember;
            Attribute             = attribute;
            _setAttribute         = setAttribute;
            FixedDestination      = fixedDestination;
            _isPositional         = attribute is PositionalArgumentAttribute;
            _reporter             = options?.Reporter ?? (s => { });
            ArgumentType          = Attribute.GetArgumentType(member.MemberType);
            _collectionArgType    = AsCollectionType(ArgumentType);
            HasDefaultValue       = attribute.ExplicitDefaultValue || attribute.DynamicDefaultValue;
            _validationAttributes = GetValidationAttributes(ArgumentType, Member);
            _argumentParseContext = CreateParseContext(attribute, _setAttribute, options);

            LongName          = GetLongName(attribute, setAttribute, member.MemberInfo);
            ExplicitShortName = HasExplicitShortName(attribute);
            ShortName         = GetShortName(attribute, setAttribute, member.MemberInfo);
            DefaultValue      = GetDefaultValue(attribute, member, defaultFieldValue);

            var nullableBase = Nullable.GetUnderlyingType(member.MemberType);

            if (_collectionArgType != null)
            {
                _collectionValues = new ArrayList();
                _valueType        = _collectionArgType.ElementType;
            }
            else if (nullableBase != null)
            {
                // For nullable arguments, we use the wrapped type (T in
                // Nullable<T>) as the value type. Parsing an enum or int is the
                // same as parsing an enum? or int?, for example, since null can
                // only arise if the value was not provided at all.
                _valueType = Attribute.GetArgumentType(nullableBase);
            }
            else
            {
                _valueType = ArgumentType;
            }

            Debug.Assert(_valueType != null);

            if (Unique && !IsCollection)
            {
                throw new InvalidArgumentSetException(member, Strings.UniqueUsedOnNonCollectionArgument);
            }

            Debug.Assert(!string.IsNullOrEmpty(LongName));
        }
Example #9
0
        private static ICollectionArgumentType AsCollectionType(IArgumentType type)
        {
            if (type is ArgumentTypeExtension extension)
            {
                type = extension.InnerType;
            }

            return(type as ICollectionArgumentType);
        }
        /// <summary>
        /// Checks if this validation attributes accepts values of the specified
        /// type.
        /// </summary>
        /// <param name="type">Type to check.</param>
        /// <returns>True if this attribute accepts values of the specified
        /// type; false if not.</returns>
        public sealed override bool AcceptsType(IArgumentType type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            return (type.Type == typeof(string)) || (type.Type == typeof(FileSystemPath));
        }
Example #11
0
        private static ICollectionArgumentType AsCollectionType(IArgumentType type)
        {
            // TODO: This knowledge of ArgumentTypeExtension is undesirable.
            if (type is ArgumentTypeExtension extension)
            {
                type = extension.InnerType;
            }

            return(type as ICollectionArgumentType);
        }
Example #12
0
        public SetArgumentTreeArgumentType(string name, IArgumentType valuetype, string[] names, IArgumentType[] types)
        {
            this.randgen = new Random();
            this.name    = name;

            Dictionary <string, object> children = new Dictionary <string, object>();

            for (int ii = 0; ii < names.Length; ii++)
            {
                children[names[ii]] = types[ii];
            }

            this.template = new ArgumentTree(valuetype, children);
        }
        public ArgumentsHandler(string title, string description, string[] argnames, string[] argtitles, IArgumentType[] argtypes, string[] argdescs, bool[] argreqs, SetArgumentTreeArgumentType resulttype, int time)
            : base(title, description)
        {
            this.argnames = argnames;
            this.argtitles = argtitles;
            this.argtypes = argtypes;
            this.argdescs = argdescs;
            this.argreqs = argreqs;
            this.resulttype = resulttype;

            this.time = time;

            argmap = new Dictionary<string, int>();
            for (int ii = 0; ii < argnames.Length; ii++)
                argmap[argnames[ii]] = ii;
        }
Example #14
0
 /// <summary>
 /// Constructor that directly takes all required info.
 /// </summary>
 /// <param name="syntax">Argument syntax.</param>
 /// <param name="description">Argument description.</param>
 /// <param name="required">True if the argument is required;
 /// false if it's optional.</param>
 /// <param name="shortName">Argument's short form.</param>
 /// <param name="defaultValue">Argument's default value.</param>
 /// <param name="detailedSyntax">Argument detailed syntax.</param>
 /// <param name="argType">Argument type.</param>
 /// <param name="currentValue">Current value.</param>
 public ArgumentUsageInfo(
     string syntax,
     string description,
     bool required,
     string shortName      = null,
     string defaultValue   = null,
     string detailedSyntax = null,
     IArgumentType argType = null,
     object currentValue   = null)
 {
     Syntax         = syntax;
     DetailedSyntax = detailedSyntax ?? syntax;
     Description    = description;
     Required       = required;
     ShortName      = shortName;
     DefaultValue   = defaultValue;
     ArgumentType   = argType;
     CurrentValue   = currentValue;
 }
        public string GetForm(string name, ArgumentTree args, ArgumentTree invalidity)
        {
            if (action is IHtmlFormable)
            {
                return(((IHtmlFormable)action).GetHtmlForm(name, args, invalidity).Value.ToString());
            }

            IArgumentType type = action.Input;

            StringBuilder output = new StringBuilder();

            output.AppendLine("<table>");
            if (type is IHtmlFormable)
            {
                output.AppendLine(((IHtmlFormable)type).GetHtmlForm(name, args, invalidity).Value.ToString());
            }
            output.AppendLine("</table>");

            output.AppendLine(HtmlUtilities.Input("submit", "submit", "Process"));

            return(output.ToString());
        }
        private static IEnumerable <IArgumentType> GetDependencyTransitiveClosure(IArgumentType type)
        {
            var types          = new HashSet <IArgumentType>();
            var typesToProcess = new Queue <IArgumentType>();

            typesToProcess.Enqueue(type);

            while (typesToProcess.Count > 0)
            {
                var nextType = typesToProcess.Dequeue();
                if (!types.Add(nextType))
                {
                    continue;
                }

                foreach (var newType in nextType.DependentTypes.Where(t => !types.Contains(t)))
                {
                    typesToProcess.Enqueue(newType);
                }
            }

            return(types);
        }
Example #17
0
 public EnumerableArgumentType(int maxcnt, IArgumentType argtype)
 {
     this.maxcnt  = maxcnt;
     this.argtype = argtype;
 }
 public object ImmediateConvertTo(object args, IArgumentType resultType, int maxlevel, int time)
 {
     return QueueArena.CallResult(CallableConvertTo(resultType, maxlevel), args, time, 0.0);
 }
Example #19
0
        private static IReadOnlyList <ArgumentValidationAttribute> GetValidationAttributes(IArgumentType argType, IMutableMemberInfo memberInfo)
        {
            var member = memberInfo.MemberInfo;

            var collectionArgType = AsCollectionType(argType);
            var argTypeToCheck    = (collectionArgType != null) ? collectionArgType.ElementType : argType;

            var attributes = member.GetAttributes <ArgumentValidationAttribute>().ToList();

            foreach (var attrib in attributes.Where(attrib => !attrib.AcceptsType(argTypeToCheck)))
            {
                throw new InvalidArgumentSetException(memberInfo, string.Format(
                                                          CultureInfo.CurrentCulture,
                                                          Strings.BadValidationAttribute,
                                                          attrib.GetType().Name,
                                                          argType.Type.Name));
            }

            return(attributes);
        }
 public KeyValueArgumentType(IArgumentType keytype, IArgumentType valuetype)
     : base(new IArgumentType[] { keytype, valuetype })
 {
 }
 public LabelledArgumentType(string label, IArgumentType inner)
 {
     this.label = label;
     this.inner = inner;
 }
 /// <summary>
 /// Constructs a new command group definition.
 /// </summary>
 /// <param name="argType">Argument type associated with this command group definition.</param>
 internal CommandGroupDefinition(IArgumentType argType)
 {
     ArgumentType = argType;
 }
 /// <summary>
 /// Checks if this validation attributes accepts values of the specified
 /// type.
 /// </summary>
 /// <param name="type">Type to check.</param>
 /// <returns>True if this attribute accepts values of the specified
 /// type; false if not.</returns>
 public sealed override bool AcceptsType(IArgumentType type) =>
     type is IntegerArgumentType;
Example #24
0
 public SetArgumentTreeArgumentType(string name, IArgumentType valuetype)
 {
     this.randgen  = new Random();
     this.name     = name;
     this.template = new ArgumentTree(valuetype);
 }
Example #25
0
 public DictionaryArgumentType(IArgumentType keytype, IArgumentType valuetype)
 {
     this.keytype   = keytype;
     this.valuetype = valuetype;
 }
 public ICallable CallableConvertTo(IArgumentType resultType, int maxlevel)
 {
     return new ActionConversion(this, resultType, new Dictionary<IAction,int>(), Aborter.NewAborter(maxlevel));
 }
Example #27
0
        /// <summary>
        /// Tries to retrieve the registered, stock IArgumentType implementation
        /// that describes the specified type.
        /// </summary>
        /// <param name="type">Type to look up.</param>
        /// <param name="argType">On success, receives the object that
        /// describes the specified type; receives null otherwise.</param>
        /// <returns>True on success; false otherwise.</returns>
        public static bool TryGetType(Type type, out IArgumentType argType)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            // See if it's a registered, well-known type.
            if (TryGetBuiltInType(type, out argType))
            {
                return(true);
            }

            // Or possibly a type that directly implements IArgumentType itself.
            if (type.GetTypeInfo().GetInterfaces().Contains(typeof(IArgumentType)))
            {
                var constructor = type.GetTypeInfo().GetConstructor(Array.Empty <Type>());
                if (constructor == null)
                {
                    argType = null;
                    return(false);
                }

                argType = (IArgumentType)constructor.Invoke(Array.Empty <object>());
                return(true);
            }

            // Specially handle all enum types.
            if (type.GetTypeInfo().IsEnum)
            {
                argType = EnumArgumentType.Create(type);
                return(true);
            }

            // And arrays.
            if (type.IsArray)
            {
                argType = new ArrayArgumentType(type);
                return(true);
            }

            // Handle all types that implement the generic ICollection<T>
            // interface.
            if (type.GetTypeInfo().GetInterface(typeof(ICollection <>).Name) != null)
            {
                argType = new CollectionOfTArgumentType(type);
                return(true);
            }

            // Specially handle a few well-known generic types.
            if (type.GetTypeInfo().IsGenericType)
            {
                var genericTy = type.GetGenericTypeDefinition();

                if (genericTy.IsEffectivelySameAs(typeof(KeyValuePair <,>)))
                {
                    argType = new KeyValuePairArgumentType(type);
                    return(true);
                }
                else if (genericTy.IsEffectivelySameAs(typeof(CommandGroup <>)))
                {
                    argType = new CommandGroupArgumentType(type);
                    return(true);
                }

                if (type.GetTypeInfo().GetInterface("ITuple") != null)
                {
                    argType = new TupleArgumentType(type);
                    return(true);
                }
            }

            // See if it's a nullable wrapper of a type we can handle.
            var underlyingType = Nullable.GetUnderlyingType(type);

            if (underlyingType != null)
            {
                return(TryGetType(underlyingType, out argType));
            }

            argType = null;
            return(false);
        }
Example #28
0
 /// <summary>
 /// Checks if this validation attributes accepts values of the specified
 /// type.
 /// </summary>
 /// <param name="type">Type to check.</param>
 /// <returns>True if this attribute accepts values of the specified
 /// type; false if not.</returns>
 public sealed override bool AcceptsType(IArgumentType type) =>
 type is IntegerArgumentType;
Example #29
0
 public ICallable CallableConvertTo(IArgumentType resultType, int maxlevel)
 {
     return(new ActionConversion(this, resultType, new Dictionary <IAction, int>(), Aborter.NewAborter(maxlevel)));
 }
Example #30
0
 public object ImmediateConvertTo(object args, IArgumentType resultType, int maxlevel, int time)
 {
     return(QueueArena.CallResult(CallableConvertTo(resultType, maxlevel), args, time, 0.0));
 }
Example #31
0
 public NamedArgumentType(string name, IArgumentType inner)
 {
     this.name  = name;
     this.inner = inner;
 }
Example #32
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="innerType">The base IArgumentType instance to
 /// wrap and extend.</param>
 public ArgumentTypeExtension(IArgumentType innerType) : this(innerType, null, null, null)
 {
 }
Example #33
0
 public static RequiredArgumentBuilder <Source, T> Argument <T>(string name, IArgumentType <T> argumentType)
 {
     return(RequiredArgumentBuilder <Source, T> .Argument(name, argumentType));
 }
Example #34
0
 /// <summary>
 /// Checks if this validation attributes accepts values of the specified
 /// type.
 /// </summary>
 /// <param name="type">Type to check.</param>
 /// <returns>True if this attribute accepts values of the specified
 /// type; false if not.</returns>
 public override bool AcceptsType(IArgumentType type) => true;
Example #35
0
 /// <summary>
 /// Checks if this validation attributes accepts values of the specified
 /// type.
 /// </summary>
 /// <param name="type">Type to check.</param>
 /// <returns>True if this attribute accepts values of the specified
 /// type; false if not.</returns>
 public abstract bool AcceptsType(IArgumentType type);
Example #36
0
        /// <summary>
        /// Tries to retrieve the registered, stock IArgumentType implementation
        /// that describes the specified type.
        /// </summary>
        /// <param name="type">Type to look up.</param>
        /// <param name="argType">On success, receives the object that
        /// describes the specified type; receives null otherwise.</param>
        /// <returns>True on success; false otherwise.</returns>
        public static bool TryGetType(Type type, out IArgumentType argType)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            // See if it's a registered, well-known type.
            if (TryGetBuiltInType(type, out argType))
            {
                return true;
            }

            // Or possibly a type that directly implements IArgumentType itself.
            if (type.GetInterfaces().Contains(typeof(IArgumentType)))
            {
                var constructor = type.GetConstructor(new Type[] { });
                if (constructor == null)
                {
                    argType = null;
                    return false;
                }

                argType = (IArgumentType)constructor.Invoke(new object[] { });
                return true;
            }

            // Specially handle all enum types.
            if (type.IsEnum)
            {
                argType = EnumArgumentType.Create(type);
                return true;
            }

            // And arrays.
            if (type.IsArray)
            {
                argType = new ArrayArgumentType(type);
                return true;
            }

            // Handle all types that implement the generic ICollection<T>
            // interface.
            if (type.GetInterface(typeof(ICollection<>).Name) != null)
            {
                argType = new CollectionOfTArgumentType(type);
                return true;
            }

            // Specially handle KeyValuePair and Tuple types.
            if (type.IsGenericType)
            {
                var genericTy = type.GetGenericTypeDefinition();
                
                if (genericTy == typeof(KeyValuePair<,>))
                {
                    argType = new KeyValuePairArgumentType(type);
                    return true;
                }

                if (type.GetInterface("ITuple") != null)
                {
                    argType = new TupleArgumentType(type);
                    return true;
                }
            }

            // See if it's a nullable wrapper of a type we can handle.
            var underlyingType = Nullable.GetUnderlyingType(type);
            if (underlyingType != null)
            {
                return TryGetType(underlyingType, out argType);
            }

            argType = null;
            return false;
        }
Example #37
0
 private static bool TryGetBuiltInType(Type type, out IArgumentType argType) =>
 s_builtInTypes.TryGetValue(type.GetTypeInfo().GUID, out argType);
Example #38
0
 private static bool TryGetBuiltInType(Type type, out IArgumentType argType) =>
     s_builtInTypes.TryGetValue(type, out argType);
Example #39
0
 /// <summary>
 /// Checks if this validation attributes accepts values of the specified
 /// type.
 /// </summary>
 /// <param name="type">Type to check.</param>
 /// <returns>True if this attribute accepts values of the specified
 /// type; false if not.</returns>
 public override bool AcceptsType(IArgumentType type) => true;
 /// <summary>
 /// Checks if this validation attributes accepts values of the specified
 /// type.
 /// </summary>
 /// <param name="type">Type to check.</param>
 /// <returns>True if this attribute accepts values of the specified
 /// type; false if not.</returns>
 public abstract bool AcceptsType(IArgumentType type);