Ejemplo n.º 1
0
 /// <summary>
 /// Finds the action that matches the given token in the given definition
 /// </summary>
 /// <param name="firstToken">the token to test.  If you pass null you will get null back.</param>
 /// <param name="def">The definition to inspect.  If null, the ambient definition will be used.  If there is no ambient definition and null is passed then this method throws a NullReferenceException.</param>
 /// <returns>the action that matches the given token in the given definition or null if no such action is found</returns>
 public static CommandLineAction FindContextualAction(string firstToken, CommandLineArgumentsDefinition def = null)
 {
     def = PassThroughOrTryGetAmbientDefinition(def);
     if (firstToken == null)
     {
         return(null);
     }
     return(def.FindMatchingAction(firstToken));
 }
 private static bool TryParseStageAction(CommandLineArgumentsDefinition effectiveDefinition, string actionKey, out CommandLineAction action)
 {
     if (actionKey == null)
     {
         throw new ArgException("Unexpected '|' at end of pipeline");
     }
     action = effectiveDefinition.FindMatchingAction(actionKey);
     return(action != null);
 }
Ejemplo n.º 3
0
 private static bool TryParseStageAction(CommandLineArgumentsDefinition effectiveDefinition, string actionKey, out CommandLineAction action)
 {
     if (actionKey == null) throw new ArgException("Unexpected '|' at end of pipeline");
     action = effectiveDefinition.FindMatchingAction(actionKey);
     return action != null;
 }
Ejemplo n.º 4
0
        private string[] MapObject(object o, CommandLineArgumentsDefinition effectiveDefinition)
        {
            List <string> newCommandLine = new List <string>();

            newCommandLine.AddRange(CmdLineArgs);

            var preParseResult = ArgParser.Parse(effectiveDefinition, newCommandLine.ToArray());

            var predictedAction = effectiveDefinition.FindMatchingAction(this.CmdLineArgs[0]);

            if (predictedAction == null)
            {
                PowerLogger.LogLine("Could not determine action: " + this.CmdLineArgs[0] + " - Here are the supported action:");
                foreach (var action in effectiveDefinition.Actions)
                {
                    PowerLogger.LogLine("  " + action.DefaultAlias);
                }
                throw new ArgException("TODO - Could not determine action: " + this.CmdLineArgs[0]);
            }

            PowerLogger.LogLine("Predicted action is " + predictedAction.DefaultAlias);


            var argsToInspectForDirectMappingTarget = predictedAction.Arguments.Union(effectiveDefinition.Arguments).ToList();
            var directMappingTarget = (from argument in argsToInspectForDirectMappingTarget
                                       where argument.Metadata.HasMeta <ArgPipelineTarget>()
                                       select argument).SingleOrDefault();

            if (directMappingTarget != null)
            {
                var revivedValue = o;
                if (IsCompatible(o, directMappingTarget) == false)
                {
                    PowerLogger.LogLine("Need to map " + o.GetType().FullName + " to " + directMappingTarget.ArgumentType.FullName);

                    if (TrySimpleConvert(o, directMappingTarget.ArgumentType, out revivedValue))
                    {
                        // we're good
                    }
                    else if (ArgPipelineObjectMapper.CurrentMapper == null)
                    {
                        throw new InvalidArgDefinitionException("Unable to attempt tp map type " + o.GetType().FullName + " to " + directMappingTarget.ArgumentType.FullName + " because no mapper is registered at ArgPipelineObjectMapperProvider.CurrentMapper");
                    }
                    else
                    {
                        revivedValue = ArgPipelineObjectMapper.CurrentMapper.MapIncompatibleDirectTargets(directMappingTarget.ArgumentType, o);
                    }
                }

                effectiveDefinition.Metadata.Add(new ArgumentOverrideHook(directMappingTarget, revivedValue));
            }
            else
            {
                PowerLogger.LogLine("Attempting to shred object: " + o.ToString());
                foreach (var argument in predictedAction.Arguments.Union(effectiveDefinition.Arguments))
                {
                    bool manualOverride = false;

                    foreach (var explicitKey in preParseResult.ExplicitParameters.Keys)
                    {
                        if (argument.IsMatch(explicitKey))
                        {
                            manualOverride = true;
                            break;
                        }
                    }

                    if (preParseResult.ImplicitParameters.ContainsKey(argument.Position))
                    {
                        manualOverride = true;
                    }

                    if (manualOverride)
                    {
                        continue;
                    }

                    var    mapper = argument.Metadata.Meta <ArgPipelineExtractor>() ?? new ArgPipelineExtractor();
                    string mappedKey, mappedValue;
                    if (mapper.TryExtractObjectPropertyIntoCommandLineArgument(o, argument, out mappedKey, out mappedValue))
                    {
                        newCommandLine.Add(mappedKey);
                        newCommandLine.Add(mappedValue);
                    }
                }
            }

            return(newCommandLine.ToArray());
        }
Ejemplo n.º 5
0
        private string[] MapObject(object o, CommandLineArgumentsDefinition effectiveDefinition)
        {
            List<string> newCommandLine = new List<string>();
            newCommandLine.AddRange(CmdLineArgs);

            var preParseResult = ArgParser.Parse(effectiveDefinition, newCommandLine.ToArray());

            var predictedAction = effectiveDefinition.FindMatchingAction(this.CmdLineArgs[0]);

            if (predictedAction == null)
            {
                PowerLogger.LogLine("Could not determine action: "+this.CmdLineArgs[0]+" - Here are the supported action:");
                foreach(var action in effectiveDefinition.Actions)
                {
                    PowerLogger.LogLine("  "+action.DefaultAlias);
                }
                throw new ArgException("TODO - Could not determine action: "+this.CmdLineArgs[0]);
            }

            PowerLogger.LogLine("Predicted action is " + predictedAction.DefaultAlias);

            var argsToInspectForDirectMappingTarget = predictedAction.Arguments.Union(effectiveDefinition.Arguments).ToList();
            var directMappingTarget = (from argument in argsToInspectForDirectMappingTarget
                                       where argument.Metadata.HasMeta<ArgPipelineTarget>()
                                       select argument).SingleOrDefault();

            if (directMappingTarget != null)
            {
                var revivedValue = o;
                if (IsCompatible(o, directMappingTarget) == false)
                {
                    PowerLogger.LogLine("Need to map "+o.GetType().FullName+" to "+directMappingTarget.ArgumentType.FullName);

                    if(TrySimpleConvert(o, directMappingTarget.ArgumentType, out revivedValue))
                    {
                        // we're good
                    }
                    else if (ArgPipelineObjectMapper.CurrentMapper == null)
                    {
                        throw new InvalidArgDefinitionException("Unable to attempt tp map type " + o.GetType().FullName + " to " + directMappingTarget.ArgumentType.FullName + " because no mapper is registered at ArgPipelineObjectMapperProvider.CurrentMapper");
                    }
                    else
                    {
                        revivedValue = ArgPipelineObjectMapper.CurrentMapper.MapIncompatibleDirectTargets(directMappingTarget.ArgumentType, o);
                    }
                }

                directMappingTarget.RevivedValueOverride = revivedValue;
            }
            else
            {
                PowerLogger.LogLine("Attempting to shred object: " + o.ToString());
                foreach (var argument in predictedAction.Arguments.Union(effectiveDefinition.Arguments))
                {
                    bool manualOverride = false;

                    foreach (var explicitKey in preParseResult.ExplicitParameters.Keys)
                    {
                        if (argument.IsMatch(explicitKey))
                        {
                            manualOverride = true;
                            break;
                        }
                    }

                    if (preParseResult.ImplicitParameters.ContainsKey(argument.Position))
                    {
                        manualOverride = true;
                    }

                    if (manualOverride) continue;

                    var mapper = argument.Metadata.Meta<ArgPipelineExtractor>() ?? new ArgPipelineExtractor();
                    string mappedKey, mappedValue;
                    if (mapper.TryExtractObjectPropertyIntoCommandLineArgument(o, argument, out mappedKey, out mappedValue))
                    {
                        newCommandLine.Add(mappedKey);
                        newCommandLine.Add(mappedValue);
                    }
                }
            }

            return newCommandLine.ToArray();
        }
 /// <summary>
 /// Finds the action that matches the given token in the given definition
 /// </summary>
 /// <param name="firstToken">the token to test.  If you pass null you will get null back.</param>
 /// <param name="def">The definition to inspect.  If null, the ambient definition will be used.  If there is no ambient definition and null is passed then this method throws a NullReferenceException.</param>
 /// <returns>the action that matches the given token in the given definition or null if no such action is found</returns>
 public static CommandLineAction FindContextualAction(string firstToken, CommandLineArgumentsDefinition def = null)
 {
     def = PassThroughOrTryGetAmbientDefinition(def);
     if(firstToken == null)
     {
         return null;
     }
     return def.FindMatchingAction(firstToken);
 }