Exemplo n.º 1
0
        private void AddUnprocessedArgument(ArgumentRecord rec)
        {
            var arg  = CurrentContext.CurrentOption != null ? (IArgument)CurrentContext.CurrentOption : (IArgument)CurrentContext.CurrentCommand;
            var item = new UnusedArgumentModel(rec.RawData, arg);

            unprocessedArguments.Add(item);
        }
Exemplo n.º 2
0
            public bool MoveNext()
            {
                if (!enumerator.MoveNext())
                {
                    return(false);
                }

                Current = CreateRecord(enumerator.Current);

                return(true);
            }
Exemplo n.º 3
0
        private static string GetArgumentValue( string[] arguments,
            ArgumentRecord record,
            ErrorCollector collector,
            ref int index)
        {
            var arg = arguments[ index ];
            var required = record.ArgumentAttribute.RequiredParameters > 0;
            string result;
            if( arg.IndexOf( ':' ) > -1 && arg[ arg.Length - 1 ] != ':' )
                result = arg.Substring( arg.IndexOf( ':' ) + 1 );
            else if( index < arguments.Length - 1 )
            {
                result = arguments[ index + 1 ];
                if( result.Length > 0 && ( result[ 0 ] == '/' || result[ 0 ] == '-' ) && ! required )
                    result = null;
                else
                    index++;
            }
            else
            {
                if( required )
                {
                    collector(
                        String.Format( CultureInfo.InvariantCulture,
                                       "Missing <{0}> parameter for {1} argument",
                                       record.ValueNames[ 0 ],
                                       arg ),
                        arg );
                }
                return null;
            }

            return result;
        }
Exemplo n.º 4
0
        private static bool AssignValue( object target,
            ArgumentRecord record,
            object value,
            string arg,
            ErrorCollector collector)
        {
            if( record.PropertyType.IsArray )
            {
                if( ( record.ArgumentAttribute.ArgumentOptions & ArgumentOptions.Unique ) != 0 &&
                    record.ArrayValues.Contains( value ) )
                {
                    collector(
                        String.Format( CultureInfo.InvariantCulture,
                                       "Duplicate value specified for the {0} argument. Value was '{1}'.",
                                       arg,
                                       value ),
                        arg );
                    return false;
                }
                record.ArrayValues.Add( value );
            }
            else
            {
                if( record.ValueSet )
                {
                    if( ( record.ArgumentAttribute.ArgumentOptions & ArgumentOptions.AtMostOnce ) != 0 )
                    {
                        collector(
                            String.Format( CultureInfo.InvariantCulture,
                                           "The {0} argument has already been specified and can be assigned only once.",
                                           arg ),
                            arg );
                        return false;
                    }

                    if( ( record.ArgumentAttribute.ArgumentOptions & ArgumentOptions.AtMostOnce ) == 0 )
                        return true;
                }

                try
                {
                    record.SetValue( target, value );
                }
                catch( Exception ex )
                {
                    collector( ex.Message, arg );
                }
            }

            return true;
        }
Exemplo n.º 5
0
        private static void ParseKnownArguments( object target,
            out ArrayList records,
            out Hashtable nameIndex,
            out Hashtable shortNameIndex,
            out ArgumentRecord defaultArg)
        {
            var targetType = target.GetType();

            records = new ArrayList();
            nameIndex = new Hashtable();
            shortNameIndex = new Hashtable();

            defaultArg = null;

            var rec = new List<ArgumentRecord>();
            ParseKnownArgumentsOfType( targetType, rec );

            string group = null;
            for( var ix = 0; ix < rec.Count; ix++ )
            {
                var record = rec[ ix ];
                records.Add( record );
                if( record.ArgumentAttribute.Default )
                    defaultArg = record;

                nameIndex[ record.SearchName ] = ix;
                if( record.ShortName != null )
                    shortNameIndex[ record.SearchShortName ] = ix;

                if( record.ArgumentAttribute.Group == null )
                    record.ArgumentAttribute.Group = group;
                else
                    group = record.ArgumentAttribute.Group;
            }
        }
Exemplo n.º 6
0
 public void Dispose()
 {
     Current = null;
     enumerator.Dispose();
 }
Exemplo n.º 7
0
 public void Reset()
 {
     Current = null;
     enumerator.Reset();
 }
Exemplo n.º 8
0
        private bool ProcessCommandOrOptionValue(ArgumentRecord rec)
        {
            foreach (var cmd in CurrentContext.CurrentCommand.Commands)
            {
                if (!rec.RawData.EqualsIgnoreCase(cmd.Name))
                {
                    continue;
                }

                results.Add(cmd, new ArgumentModel(cmd.Name, null));

                CurrentContext = new ProcessingContext(CurrentContext, (ICommandLineCommandContainer)cmd, logger);

                return(true);
            }

            var context = CurrentContext;

            while (context != null)
            {
                if (context.NextArgumentIsForClusteredOptions)
                {
                    foreach (var option in context.ProcessedClusteredOptions)
                    {
                        results[option].Values.Add(rec.RawData);
                    }

                    context.ProcessedClusteredOptions         = null;
                    context.NextArgumentIsForClusteredOptions = false;
                }

                if (context.CurrentOption == null)
                {
                    context = context.Parent;
                    continue;
                }

                if (!TryGetValue(context.CurrentOption, out var model))
                {
                    // not sure yet what to do here..
                    // no option yet and not matching command => unknown item
                    context = context.Parent;
                    continue;
                }

                if (model.HasValue && !context.CurrentOption.AllowMultipleValues)
                {
                    // multiple values are not allowed for this option type
                    context = context.Parent;
                    continue;
                }

                model.Values.Add(rec.RawData);
                return(true);
            }

            context = CurrentContext;

            //if (isFirstArgument)
            //{
            //    return false;
            //}

            while (context != null)
            {
                foreach (var option in context.CurrentCommand.Options.Where(opt => opt.Order.HasValue).OrderBy(opt => opt.Order))
                {
                    if (results.ContainsKey(option))
                    {
                        continue;
                    }

                    var argumentModel = new ArgumentModel(string.Empty, rec.RawData);

                    results.Add(option, argumentModel);

                    context.CurrentOption = option;
                    context.MarkOrderedOptionAsProcessed(option);

                    return(true);
                }

                context = context.Parent;
            }

            return(false);
        }