Exemplo n.º 1
0
        public static IList <KeyValuePair <IFreeValueDefinition, FreeValue> > TransformFreeValues(
            IList <KeyValuePair <IFreeValueDefinition, string> > rawFreeValues)
        {
            if (rawFreeValues == null)
            {
                return(Array.Empty <KeyValuePair <IFreeValueDefinition, FreeValue> >());
            }

            return(rawFreeValues.Select(fv =>
            {
                IFreeValueDefinition freeValueDefinition = fv.Key;
                ValueTransformer transformer = freeValueDefinition.Transformer;

                try
                {
                    IList <object> transformed = transformer.Transform(fv.Value);
                    return new KeyValuePair <IFreeValueDefinition, FreeValue>(
                        freeValueDefinition,
                        new FreeValue(fv.Value, transformed));
                }
                catch (ArgParsingException)
                {
                    throw;
                }
                catch
                {
                    throw new ArgParsingException(
                        ArgsParsingErrorCode.TransformValueFailed,
                        fv.Value);
                }
            })
                   .ToArray());
        }
Exemplo n.º 2
0
        public static IList <KeyValuePair <IOptionDefinition, OptionValue> > TransformOptionValues(
            IEnumerable <KeyValuePair <IOptionDefinition, IList <string> > > rawOptionValues)
        {
            if (rawOptionValues == null)
            {
                return(Array.Empty <KeyValuePair <IOptionDefinition, OptionValue> >());
            }

            return(rawOptionValues
                   .Select(
                       ov =>
            {
                IOptionDefinition optionDefinition = ov.Key;
                ValueTransformer transformer = optionDefinition.Transformer;

                try
                {
                    return new KeyValuePair <IOptionDefinition, OptionValue>(
                        optionDefinition,
                        new OptionValue(ov.Value, transformer.Transform(ov.Value)));
                }
                catch (ArgParsingException)
                {
                    throw;
                }
                catch
                {
                    throw new ArgParsingException(
                        ArgsParsingErrorCode.TransformValueFailed,
                        $"Option: {ov.Key.ToString()}; Values: {String.Join(" ", ov.Value)}.");
                }
            })
                   .ToArray());
        }
Exemplo n.º 3
0
 public OptionDefinition(
     string symbol,
     char?abbreviation,
     string description,
     bool isRequired = false,
     OptionType type = OptionType.KeyValue,
     ValueTransformer transformer = null)
 {
     Symbol      = new OptionSymbol(symbol, abbreviation);
     Description = description ?? string.Empty;
     IsRequired  = isRequired;
     Type        = type;
     Transformer = transformer ?? DefaultTransformer.Instance;
 }
Exemplo n.º 4
0
 /// <summary>
 /// Add a key-value option for current command.
 /// </summary>
 /// <param name="fullForm">
 /// The full form of the key. If current key does not contains a full form. The argument
 /// should be <c>null</c>.
 /// </param>
 /// <param name="abbreviation">
 /// The abbreviation form of the key. If current key does not contains a abbreviation form.
 /// The argument should be <c>null</c>.
 /// </param>
 /// <param name="description">
 /// The description for this key-value option. Please note that all the line-breaks will be
 /// removed for consisitency.
 /// </param>
 /// <param name="isRequired">
 /// Indicating whether this key-value option is required.
 /// </param>
 /// <param name="transformer">
 /// The transformer of the value. If you would like to keep the original string representation,
 /// just pass <c>null</c>.
 /// </param>
 /// <returns>The command builder instance.</returns>
 /// <exception cref="ArgumentException">
 /// <para>
 /// The <paramref name="fullForm"/> and <paramref name="abbreviation"/> both are <c>null</c>.
 /// </para>
 /// <para>-- Or --</para>
 /// <para>
 /// The <paramref name="fullForm"/> is not a valid identifier.
 /// </para>
 /// <para>-- Or --</para>
 /// <para>
 /// The <paramref name="abbreviation"/> is not an english alphabet.
 /// </para>
 /// <para>-- Or --</para>
 /// <para>
 /// This definition is conflict with another existed definition.
 /// </para>
 /// </exception>
 public CommandBuilder AddOptionWithValue(
     string fullForm,
     char?abbreviation,
     string description,
     bool isRequired = false,
     ValueTransformer transformer = null)
 {
     commandDefinition.RegisterOption(
         new OptionDefinition(
             fullForm,
             abbreviation,
             description,
             isRequired,
             OptionType.KeyValue,
             transformer));
     return(this);
 }
Exemplo n.º 5
0
        public FreeValueDefinition(string name, string description, bool isRequired = false, ValueTransformer transformer = null)
        {
            ValidateName(name);

            Name        = name;
            IsRequired  = isRequired;
            Description = description ?? string.Empty;
            Transformer = transformer ?? DefaultTransformer.Instance;
        }
Exemplo n.º 6
0
        /// <summary>
        /// Add a free value definition for current command. This operation will automatically enable
        /// free value for this command.
        /// </summary>
        /// <param name="name">The name of the free value.</param>
        /// <param name="description">
        /// The description of the free value. Please note that all the line-breaks will be
        /// removed for consisitency.
        /// </param>
        /// <param name="isRequired">
        /// Whether the free value is mandatory.
        /// </param>
        /// <param name="transformer">
        /// The transformer of the value. If you would like to keep the original string representation,
        /// just pass <c>null</c>.
        /// </param>
        /// <returns>The command builder instance.</returns>
        /// <exception cref="ArgumentNullException">
        /// The <paramref name="name"/> is <c>null</c>.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// Current free value is conflict with existing free value definitions.
        /// </exception>
        public CommandBuilder AddFreeValue(string name, string description, bool isRequired = false, ValueTransformer transformer = null)
        {
            var definition = new FreeValueDefinition(name, description, isRequired, transformer);

            commandDefinition.RegisterFreeValue(definition);
            allowFreeValue = true;
            return(this);
        }