Ejemplo n.º 1
0
        public static NameToken Create(string name)
        {
            if (!NameMap.TryGetValue(name, out var value))
            {
                return(new NameToken(name));
            }

            return(value);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a new <see cref="NameToken"/> with the given name, ensuring only one instance of each
        /// <see cref="NameToken"/> can exist.
        /// </summary>
        /// <param name="name">The string representation of the name for the token to create.</param>
        /// <returns>The created or existing token.</returns>
        public static NameToken Create([NotNull] string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (!NameMap.TryGetValue(name, out var value))
            {
                return(new NameToken(name));
            }

            return(value);
        }
        /// <inheritdoc />
        public bool Remove(T setting)
        {
            ThrowIfReadOnly();
            if (!NameMap.TryGetValue(setting.MainName, out var guid))
            {
                return(false);
            }

            if (setting.IsHelp)
            {
                HasHelpCommand = false;
            }

            foreach (var name in setting.Names)
            {
                NameMap.Remove(name);
            }
            return(SettingMap.Remove(guid));
        }
        /// <summary>
        /// Attempts to get the setting with the specified name.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="state"></param>
        /// <param name="setting"></param>
        /// <returns></returns>
        public bool TryGetSetting(string name, PrefixState state, out T setting)
        {
            bool InternalTryGetSetting(string deprefixedName, out T returned)
            {
                var valid = NameMap.TryGetValue(deprefixedName, out var guid);

                returned = valid ? SettingMap[guid] : default;
                return(valid);
            }

            foreach (var prefix in Prefixes)
            {
                var deprefixed = ArgumentMappingUtils.Deprefix(prefix, name, state);
                if (deprefixed != null && InternalTryGetSetting(deprefixed, out setting))
                {
                    return(true);
                }
            }
            setting = default;
            return(false);
        }