PowerArgs.Cli.PowerArgsRichCommandLineReader.FindContextualArgument C# (CSharp) Method

FindContextualArgument() public static method

A helper that detects the argument represented by the current token given a definition.
public static FindContextualArgument ( string previousToken, CommandLineAction contextualAction, CommandLineArgumentsDefinition def = null ) : System.CommandLineArgument
previousToken string The token to inspect. If you pass null you will get null back.
contextualAction CommandLineAction An action to inspect for a match if the current token does not match a global argument. Pass null to only check global arguments.
def CommandLineArgumentsDefinition 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.
return System.CommandLineArgument
        public static CommandLineArgument FindContextualArgument(string previousToken, CommandLineAction contextualAction, CommandLineArgumentsDefinition def = null)
        {
            def = PassThroughOrTryGetAmbientDefinition(def);

            if (previousToken == null)
            {
                return null;
            }

            string currentTokenArgumentNameValue = null;
            if (previousToken.StartsWith("-"))
            {
                currentTokenArgumentNameValue = previousToken.Substring(1);
            }
            else if (previousToken.StartsWith("/"))
            {
                currentTokenArgumentNameValue = previousToken.Substring(1);
            }

            CommandLineArgument currentTokenArgument = null;
            if (currentTokenArgumentNameValue != null)
            {
                currentTokenArgument = def.Arguments.Where(arg => arg.IsMatch(currentTokenArgumentNameValue) && arg.ArgumentType != typeof(bool)).SingleOrDefault();

                if (currentTokenArgument == null && contextualAction != null)
                {
                    currentTokenArgument = contextualAction.Arguments.Where(arg => arg.IsMatch(currentTokenArgumentNameValue) && arg.ArgumentType != typeof(bool)).SingleOrDefault();
                }
            }
            return currentTokenArgument;
        }

Usage Example

Example #1
0
        public bool ShouldBeHighlighted(RichCommandLineContext readerContext, HighlighterContext highlighterContext)
        {
            // don't even try mark tokens as invalid unless the cursor is on it
            if (readerContext.BufferPosition >= highlighterContext.CurrentToken.StartIndex && readerContext.BufferPosition < highlighterContext.CurrentToken.EndIndex)
            {
                return(false);
            }

            var currentToken  = highlighterContext.CurrentToken.Value;
            var previousToken = PowerArgsRichCommandLineReader.FindPreviousNonWhitespaceToken(readerContext, highlighterContext);
            var firstToken    = readerContext.Tokens[0].Value;

            CommandLineAction   contextualAction   = PowerArgsRichCommandLineReader.FindContextualAction(firstToken, definition);
            CommandLineArgument contextualArgument = PowerArgsRichCommandLineReader.FindContextualArgument(previousToken, contextualAction, definition);

            if (contextualArgument != null)
            {
                if (contextualArgument.TestIsValidAndRevivable(currentToken) == false)
                {
                    // the current token either failed validation or could not be revived
                    return(true);
                }
            }

            bool expectMatchingArg;
            CommandLineArgument currentTokenArgument = PowerArgsRichCommandLineReader.FindCurrentTokenArgument(contextualAction, currentToken, out expectMatchingArg, definition);

            if (currentTokenArgument == null && expectMatchingArg)
            {
                // The current token starts with a - or /, but does not match a global or action specific argument, so we'll highlight the token red
                return(true);
            }

            return(false);
        }