Instances of this class represent a single command line argument that users can specify on the command line. Supported syntaxes include: -argumentName argumentValue /argumentName:argumentValue -argumentName - If the argument is a boolean it will be true in this case. --argumentName=argumentValue - Only works if you have added an alias that starts with --. argumentValue - Only works if this argument defines the Position property as >= 0
Пример #1
0
 internal bool TryGetAndRemoveAdditionalExplicitParameters(CommandLineArgument argument, out List<string> result)
 {
     foreach(var knownKey in AdditionalExplicitParameters.Keys)
     {
         if(argument.IsMatch(knownKey))
         {
             result = AdditionalExplicitParameters[knownKey];
             AdditionalExplicitParameters.Remove(knownKey);
             return true;
         }
     }
     result = null;
     return false;
 }
Пример #2
0
 /// <summary>
 /// Always validates the given argument, even if it was not specified by the user (arg will be null in this case).
 /// If you override this method then you should also override ImplementsValidateAlways so it returns true.
 ///</summary>
 /// <param name="argument">The argument that the attribute was placed on.</param>
 /// <param name="arg">The value specified on the command line or null if the user didn't actually specify a value for the argument.  If the user specified the argument name, but not a value then arg will equal string.Empty</param>
 public virtual void ValidateAlways(CommandLineArgument argument, ref string arg)
 {
     throw new NotImplementedException();
 }
Пример #3
0
        private ArgAction ParseInternal(CommandLineArgumentsDefinition definition, string[] input)
        {
            // TODO - Validation should be consistently done against the definition, not against the raw type
            if (definition.ValidationEnabled && definition.ArgumentScaffoldType != null && validatedScaffoldTypes.Contains(definition.ArgumentScaffoldType) == false)
            {
                ValidateArgScaffold(definition.ArgumentScaffoldType);
                validatedScaffoldTypes.Add(definition.ArgumentScaffoldType);
            }

            definition.Clean();

            var context = ArgHook.HookContext.Current;

            context.Definition = definition;
            _ambientDefinition = definition;

            definition.Validate(context);

            if (definition.ArgumentScaffoldType != null)
            {
                context.Args = ObjectFactory.CreateInstance(definition.ArgumentScaffoldType);
            }
            context.CmdLineArgs = input;

            context.RunBeforeParse();
            context.ParserData = ArgParser.Parse(definition, context.CmdLineArgs);

            var actionToken = context.CmdLineArgs.FirstOrDefault();
            var actionQuery = context.Definition.Actions.Where(a => a.IsMatch(actionToken));

            if (actionQuery.Count() == 1)
            {
                context.SpecifiedAction = actionQuery.First();
            }
            else if (actionQuery.Count() > 1)
            {
                throw new InvalidArgDefinitionException("There are multiple actions that match argument '" + actionToken + "'");
            }

            context.RunBeforePopulateProperties();
            CommandLineArgument.PopulateArguments(context.Definition.Arguments, context);
            context.Definition.SetPropertyValues(context.Args);

            object actionArgs = null;

            object[] actionParameters = null;

            if (context.SpecifiedAction == null && context.Definition.Actions.Count > 0)
            {
                if (context.CmdLineArgs.FirstOrDefault() == null)
                {
                    throw new MissingArgException("No action was specified");
                }
                else
                {
                    throw new UnknownActionArgException(string.Format("Unknown action: '{0}'", context.CmdLineArgs.FirstOrDefault()));
                }
            }
            else if (context.SpecifiedAction != null)
            {
                PropertyInfo actionProp = null;
                if (context.Definition.ArgumentScaffoldType != null)
                {
                    actionProp = ArgAction.GetActionProperty(context.Definition.ArgumentScaffoldType);
                }

                if (actionProp != null)
                {
                    actionProp.SetValue(context.Args, context.SpecifiedAction.Aliases.First(), null);
                }

                context.ParserData.ImplicitParameters.Remove(0);
                CommandLineArgument.PopulateArguments(context.SpecifiedAction.Arguments, context);
            }

            context.RunAfterPopulateProperties();

            if (context.SpecifiedAction != null)
            {
                actionArgs = context.SpecifiedAction.PopulateArguments(context.Args, ref actionParameters);
            }

            if (context.Definition.Metadata.HasMeta <AllowUnexpectedArgs>() == false)
            {
                if (context.ParserData.ImplicitParameters.Count > 0)
                {
                    throw new UnexpectedArgException("Unexpected unnamed argument: " + context.ParserData.ImplicitParameters.First().Value);
                }

                if (context.ParserData.ExplicitParameters.Count > 0)
                {
                    throw new UnexpectedArgException("Unexpected named argument: " + context.ParserData.ExplicitParameters.First().Key);
                }

                if (context.ParserData.AdditionalExplicitParameters.Count > 0)
                {
                    throw new UnexpectedArgException("Unexpected named argument: " + context.ParserData.AdditionalExplicitParameters.First().Key);
                }
            }
            else
            {
                definition.UnexpectedExplicitArguments = context.ParserData.ExplicitParameters;
                definition.UnexpectedImplicitArguments = context.ParserData.ImplicitParameters;
            }


            if (definition.ArgumentScaffoldType != null)
            {
                if (AmbientArgs.ContainsKey(definition.ArgumentScaffoldType))
                {
                    AmbientArgs[definition.ArgumentScaffoldType] = context.Args;
                }
                else
                {
                    AmbientArgs.Add(definition.ArgumentScaffoldType, context.Args);
                }
            }

            PropertyInfo actionArgsPropertyInfo = null;

            if (context.SpecifiedAction != null)
            {
                if (context.SpecifiedAction.Source is PropertyInfo)
                {
                    actionArgsPropertyInfo = context.SpecifiedAction.Source as PropertyInfo;
                }
                else if (context.SpecifiedAction.Source is MethodInfo)
                {
                    actionArgsPropertyInfo = new ArgActionMethodVirtualProperty(context.SpecifiedAction.Source as MethodInfo);
                }
            }

            return(new ArgAction()
            {
                Value = context.Args,
                ActionArgs = actionArgs,
                ActionParameters = actionParameters,
                ActionArgsProperty = actionArgsPropertyInfo,
                ActionArgsMethod = context.SpecifiedAction != null ? context.SpecifiedAction.ActionMethod : null,
                Definition = context.Definition,
                Context = context,
            });
        }
 public abstract bool TryComplete(bool shift, CommandLineArgument context, string soFar, out string completion);
Пример #5
0
        internal ITabCompletionSource CreateTabCompletionSource(CommandLineArgumentsDefinition definition, CommandLineArgument argument)
        {
            if (this.CompletionSourceType.GetInterfaces().Contains(typeof(ITabCompletionSource)) == false)
            {
                throw new InvalidArgDefinitionException("The type " + this.CompletionSourceType.FullName + " does not implement ITabCompletionSource.  The target argument was " + argument.DefaultAlias);
            }

            ITabCompletionSource ret;

            if (this.CompletionSourceType.IsSubclassOf(typeof(ArgumentAwareTabCompletionSource)))
            {
                ret = (ITabCompletionSource)Activator.CreateInstance(this.CompletionSourceType, definition, argument);
            }
            else
            {
                var toWrap = (ITabCompletionSource)Activator.CreateInstance(this.CompletionSourceType);
                ret = new ArgumentAwareWrapperTabCompletionSource(definition, argument, toWrap);
            }
            return(ret);
        }
Пример #6
0
        internal static CommandLineAction Create(MethodInfo actionMethod, List <string> knownAliases)
        {
            var ret = PropertyInitializer.CreateInstance <CommandLineAction>();

            ret.ActionMethod = actionMethod;

            ret.Source = actionMethod;
            ret.Aliases.Add(actionMethod.Name);

            ret.Metadata.AddRange(actionMethod.Attrs <IArgMetadata>().AssertAreAllInstanceOf <ICommandLineActionMetadata>());

            ret.IgnoreCase = true;

            if (actionMethod.DeclaringType.HasAttr <ArgIgnoreCase>() && actionMethod.DeclaringType.Attr <ArgIgnoreCase>().IgnoreCase == false)
            {
                ret.IgnoreCase = false;
            }

            if (actionMethod.HasAttr <ArgIgnoreCase>() && actionMethod.Attr <ArgIgnoreCase>().IgnoreCase == false)
            {
                ret.IgnoreCase = false;
            }

            if (actionMethod.GetParameters().Length == 1 && ArgRevivers.CanRevive(actionMethod.GetParameters()[0].ParameterType) == false)
            {
                ret.Arguments.AddRange(actionMethod.GetParameters()[0].ParameterType.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => CommandLineArgument.IsArgument(p)).Select(p => CommandLineArgument.Create(p, knownAliases)));
            }
            else if (actionMethod.GetParameters().Length > 0 && actionMethod.GetParameters().Where(p => ArgRevivers.CanRevive(p.ParameterType) == false).Count() == 0)
            {
                ret.Arguments.AddRange(actionMethod.GetParameters().Where(p => CommandLineArgument.IsArgument(p)).Select(p => CommandLineArgument.Create(p)));
                foreach (var arg in (ret.Arguments).Where(a => a.Position >= 0))
                {
                    arg.Position++; // Since position 0 is reserved for the action specifier
                }
            }
            else if (actionMethod.GetParameters().Length > 0)
            {
                throw new InvalidArgDefinitionException("Your action method contains a parameter that cannot be revived on its own.  That is only valid if the non-revivable parameter is the only parameter.  In that case, the properties of that parameter type will be used.");
            }

            return(ret);
        }
Пример #7
0
 internal ArgumentAwareTabCompletionSource(CommandLineArgumentsDefinition definition, CommandLineArgument target)
 {
     this.definition = definition;
     this.argument   = target;
 }
Пример #8
0
 public ArgumentAwareWrapperSmartTabCompletionSource(CommandLineArgumentsDefinition definition, CommandLineArgument target, ITabCompletionSource toWrap)
 {
     this.Target        = target;
     this.Definition    = definition;
     this.WrappedSource = toWrap;
 }