Ejemplo n.º 1
0
        private static bool MapMulti(
            PropertyInfo pi,
            ParsedArgument <string[]> parsed,
            ParsedArguments result)
        {
            var underlyingType = pi.PropertyType.GetCollectionItemType();

            if (underlyingType == null)
            {
                return(false);
            }

            var converted = parsed.Value.Select(
                stringValue =>
            {
                var couldConvert = TryChangeType(stringValue, underlyingType, out var convertedValue);
                return(couldConvert, convertedValue);
            })
                            .Where(o => o.couldConvert)
                            .Select(o => o.convertedValue)
                            .ToArray();

            if (converted.Length != parsed.Value.Length)
            {
                // TODO: throw: one or more items could not be converted
            }

            var propertyValue = converted.CastCollection(
                underlyingType,
                pi.PropertyType.IsArray);

            // TODO: handle other collection types
            pi.SetValue(result, propertyValue);
            return(true);
        }
Ejemplo n.º 2
0
        private void RenderHelp(ParsedArguments result)
        {
            var flagHelp = result.Flags.Aggregate(
                new Dictionary <string[], string[]>(),
                (acc, kvp) =>
            {
                acc[kvp.Value.Switches] = kvp.Value.Help;
                return(acc);
            });
            var parameterHelp = result.Parameters.Aggregate(
                new Dictionary <string[], string[]>(),
                (acc, kvp) =>
            {
                acc[kvp.Value.Switches] = kvp.Value.Help;
                return(acc);
            });
            var sorted = flagHelp.Union(parameterHelp)
                         .OrderBy(kvp => kvp.Key.FirstOrDefault())
                         .ToArray();

            _help.ForEach(line =>
            {
                var available = Console.WindowWidth;
                var subs      = line.SplitLines(available);
                subs.ForEach(s => Console.WriteLine(s));
            });
            sorted.ForEach(o => RenderHelpFor(o.Key, o.Value));
        }
Ejemplo n.º 3
0
 private static bool MapEnum(
     PropertyInfo pi,
     ParsedArgument <string[]> parsed,
     ParsedArguments result)
 {
     return(MapSingle(
                pi,
                parsed,
                result,
                () => pi.PropertyType.IsEnum,
                (type, value) => Enum.TryParse(type, value?.Replace("-", ""), true, out var converted)
             ? (true, value: converted)
             : (false, value: converted)
                ));
 }
Ejemplo n.º 4
0
 private static bool MapSingle(
     PropertyInfo pi,
     ParsedArgument <string[]> parsed,
     ParsedArguments result)
 {
     return(MapSingle(
                pi,
                parsed,
                result,
                () => !pi.PropertyType.IsCollection(),
                (desiredType, value) => TryChangeType(value, desiredType, out var converted)
             ? (true, converted)
             : (false, converted)
                ));
 }
Ejemplo n.º 5
0
 private static bool MapSingle(
     PropertyInfo pi,
     ParsedArgument <string[]> parsed,
     ParsedArguments result,
     Func <bool> propertyTypeCheck,
     Func <Type, string, (bool success, object converted)> parser)