Example #1
0
 public static OptionMap Create(object target, CommandLineParserSettings settings)
 {
     var list = ReflectionUtil.RetrievePropertyList<BaseOptionAttribute>(target);
     if (list == null)
     {
         return null;
     }
     var map = new OptionMap(list.Count, settings);
     foreach (var pair in list)
     {
         if (pair.Left != null && pair.Right != null)
         {
             map[pair.Right.UniqueName] = new OptionInfo(pair.Right, pair.Left);
         }
     }
     map.RawOptions = target;
     return map;
 }
        public static OptionMap CreateMap(object target,
            IList<Pair<PropertyInfo, VerbOptionAttribute>> verbs, CommandLineParserSettings settings)
        {
            var map = new OptionMap(verbs.Count, settings);
            foreach (var verb in verbs)
            {
                var optionInfo = new OptionInfo(verb.Right, verb.Left)
                    {
                        HasParameterLessCtor = verb.Left.PropertyType.GetConstructor(Type.EmptyTypes) != null

                    };
                if (!optionInfo.HasParameterLessCtor && verb.Left.GetValue(target, null) == null)
                {
                    throw new CommandLineParserException(string.Format("Type {0} must have a parameterless constructor or" +
                        " be already initialized to be used as a verb command.", verb.Left.PropertyType));
                }
                map[verb.Right.UniqueName] = optionInfo;
            }
            map.RawOptions = target;
            return map;
        }
        public override Internal.ParserState Parse(IArgumentEnumerator argumentEnumerator, OptionMap map, object options)
        {
            var parts = argumentEnumerator.Current.Substring(2).Split(new[] { '=' }, 2);
            var option = map[parts[0]];
            bool valueSetting;
            if (option == null)
            {
                return _ignoreUnkwnownArguments ? Internal.ParserState.MoveOnNextElement : Internal.ParserState.Failure;
            }
            option.IsDefined = true;

            ArgumentParser.EnsureOptionArrayAttributeIsNotBoundToScalar(option);

            if (!option.IsBoolean)
            {
                if (parts.Length == 1 && (argumentEnumerator.IsLast || !ArgumentParser.IsInputValue(argumentEnumerator.Next)))
                {
                    return Internal.ParserState.Failure;
                }
                if (parts.Length == 2)
                {
                    if (!option.IsArray)
                    {
                        valueSetting = option.SetValue(parts[1], options);
                        if (!valueSetting)
                        {
                            DefineOptionThatViolatesFormat(option);
                        }
                        return ArgumentParser.BooleanToParserState(valueSetting);
                    }

                    ArgumentParser.EnsureOptionAttributeIsArrayCompatible(option);

                    var items = ArgumentParser.GetNextInputValues(argumentEnumerator);
                    items.Insert(0, parts[1]);

                    valueSetting = option.SetValue(items, options);
                    if (!valueSetting)
                    {
                        DefineOptionThatViolatesFormat(option);
                    }
                    return ArgumentParser.BooleanToParserState(valueSetting);
                }
                else
                {
                    if (!option.IsArray)
                    {
                        valueSetting = option.SetValue(argumentEnumerator.Next, options);
                        if (!valueSetting)
                        {
                            DefineOptionThatViolatesFormat(option);
                        }
                        return ArgumentParser.BooleanToParserState(valueSetting, true);
                    }

                    ArgumentParser.EnsureOptionAttributeIsArrayCompatible(option);

                    var items = ArgumentParser.GetNextInputValues(argumentEnumerator);

                    valueSetting = option.SetValue(items, options);
                    if (!valueSetting)
                    {
                        DefineOptionThatViolatesFormat(option);
                    }
                    return ArgumentParser.BooleanToParserState(valueSetting);
                }
            }

            if (parts.Length == 2)
            {
                return Internal.ParserState.Failure;
            }
            valueSetting = option.SetValue(true, options);
            if (!valueSetting)
            {
                DefineOptionThatViolatesFormat(option);
            }
            return ArgumentParser.BooleanToParserState(valueSetting);
        }
Example #4
0
 public abstract PresentParserState Parse(IArgumentEnumerator argumentEnumerator, OptionMap map, object options);
 public abstract Internal.ParserState Parse(IArgumentEnumerator argumentEnumerator, OptionMap map, object options);
        public override Internal.ParserState Parse(IArgumentEnumerator argumentEnumerator, OptionMap map, object options)
        {
            IArgumentEnumerator group = new OneCharStringEnumerator(argumentEnumerator.Current.Substring(1));
            while (group.MoveNext())
            {
                var option = map[group.Current];
                if (option == null)
                {
                    return _ignoreUnkwnownArguments ? Internal.ParserState.MoveOnNextElement : Internal.ParserState.Failure;
                }
                option.IsDefined = true;

                ArgumentParser.EnsureOptionArrayAttributeIsNotBoundToScalar(option);

                if (!option.IsBoolean)
                {
                    if (argumentEnumerator.IsLast && group.IsLast)
                    {
                        return ParserState.Failure;
                    }
                    bool valueSetting;
                    if (!group.IsLast)
                    {
                        if (!option.IsArray)
                        {
                            valueSetting = option.SetValue(group.GetRemainingFromNext(), options);
                            if (!valueSetting)
                            {
                                DefineOptionThatViolatesFormat(option);
                            }
                            return ArgumentParser.BooleanToParserState(valueSetting);
                        }

                        ArgumentParser.EnsureOptionAttributeIsArrayCompatible(option);

                        var items = ArgumentParser.GetNextInputValues(argumentEnumerator);
                        items.Insert(0, @group.GetRemainingFromNext());

                        valueSetting = option.SetValue(items, options);
                        if (!valueSetting)
                        {
                            DefineOptionThatViolatesFormat(option);
                        }
                        return ArgumentParser.BooleanToParserState(valueSetting, true);
                    }

                    if (!argumentEnumerator.IsLast && !ArgumentParser.IsInputValue(argumentEnumerator.Next))
                    {
                        return Internal.ParserState.Failure;
                    }
                    else
                    {
                        if (!option.IsArray)
                        {
                            valueSetting = option.SetValue(argumentEnumerator.Next, options);
                            if (!valueSetting)
                            {
                                this.DefineOptionThatViolatesFormat(option);
                            }
                            return ArgumentParser.BooleanToParserState(valueSetting, true);
                        }

                        ArgumentParser.EnsureOptionAttributeIsArrayCompatible(option);

                        var items = ArgumentParser.GetNextInputValues(argumentEnumerator);

                        valueSetting = option.SetValue(items, options);
                        if (!valueSetting)
                        {
                            DefineOptionThatViolatesFormat(option);
                        }
                        return ArgumentParser.BooleanToParserState(valueSetting);
                    }
                }

                if ([email protected] && map[@group.Next] == null)
                {
                    return Internal.ParserState.Failure;
                }
                if (!option.SetValue(true, options))
                {
                    return Internal.ParserState.Failure;
                }
            }

            return Internal.ParserState.Success;
        }