public void TestPowerArgsRichCommandLineReaderFindContextualAction() { try { PowerArgsRichCommandLineReader.FindContextualAction("doesnotmatter"); Assert.Fail("An exception should have been thrown"); } catch (NullReferenceException ex) { Assert.IsTrue(ex.Message.Contains("ambient")); } CommandLineArgumentsDefinition def = new CommandLineArgumentsDefinition(); Assert.IsNull(PowerArgsRichCommandLineReader.FindContextualAction(null, def)); Assert.IsNull(PowerArgsRichCommandLineReader.FindContextualAction("", def)); Assert.IsNull(PowerArgsRichCommandLineReader.FindContextualAction("NonMatchingAction", def)); var action = new CommandLineAction((d) => { }); action.Aliases.Add("TheAction"); def.Actions.Add(action); var found = PowerArgsRichCommandLineReader.FindContextualAction("theaction", def); Assert.AreSame(action, found); }
private static void ShowUsage(CommandLineAction specifiedAction) { if (specifiedAction == null) { ConsoleString.Write("Supported actions: ", ConsoleColor.Cyan); Console.WriteLine("Extract"); Console.WriteLine(""); return; } Console.WriteLine(""); Console.WriteLine(""); ConsoleString.WriteLine(specifiedAction.DefaultAlias, ConsoleColor.Yellow); ConsoleString.WriteLine(new string('-', 80), ConsoleColor.DarkGray); Console.WriteLine(""); Console.WriteLine(specifiedAction.Description); Console.WriteLine(""); ConsoleString.WriteLine(new string('-', 80), ConsoleColor.DarkGray); Console.WriteLine(""); switch (specifiedAction.DefaultAlias) { case "Extract": ArgUsage.GetStyledUsage <ExtractArgs>("Extract action").WriteLine(); break; } }
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); }
static CommandLineAction ParseCommandLine(string[] args) { CommandLineAction action = new CommandLineAction(); for (int i = 0, length = args.Length; i < length; ++i) { switch (args[i]) { case k_SwitchOperation: { if (i + 1 < length) { ++i; try { action.operation = (CommandLineOperation)Enum.Parse(typeof(CommandLineOperation), args[i]); } catch (Exception e) { Debug.Log(e.ToString()); } } break; } } } return(action); }
public void TestModeledAction() { bool invoked = false; CommandLineArgumentsDefinition definition = new CommandLineArgumentsDefinition(); var action = new CommandLineAction((d) => { Assert.AreEqual("go", d.SpecifiedAction.DefaultAlias); Assert.AreEqual("Hawaii", d.SpecifiedAction.Arguments[0].RevivedValue); invoked = true; }); action.Aliases.Add("go"); action.Description = "A simple action"; definition.Actions.Add(action); var actionString = action.ToString(); // Make sure it doesn't throw var destination = new CommandLineArgument(typeof(string), "destination"); destination.Metadata.Add(new ArgRequired()); destination.Description = "The place to go to"; action.Arguments.Add(destination); Args.InvokeAction(definition, "go", "-destination", "Hawaii"); Assert.IsTrue(invoked); var usage = ArgUsage.GenerateUsageFromTemplate(definition); Assert.IsTrue(usage.Contains("A simple action")); Assert.IsTrue(usage.Contains("The place to go to")); }
public void TestPowerArgsRichCommandLineReaderFindContextualArgument() { var args = new PowerArgsRichCommandLineReader.FindContextualArgumentArgs() { CurrentTokenIndex = 0, CommandLine = "-TheString", PreviousToken = "-TheString", }; try { PowerArgsRichCommandLineReader.FindContextualArgument(args); Assert.Fail("An exception should have been thrown"); } catch (NullReferenceException ex) { Assert.IsTrue(ex.Message.Contains("ambient")); } CommandLineArgumentsDefinition def = new CommandLineArgumentsDefinition(); var globalArg = new CommandLineArgument(typeof(string), "TheString"); def.Arguments.Add(globalArg); args.Definition = def; var found = PowerArgsRichCommandLineReader.FindContextualArgument(args); Assert.AreSame(globalArg, found); args.CommandLine = "/TheString"; args.PreviousToken = args.CommandLine; found = PowerArgsRichCommandLineReader.FindContextualArgument(args); Assert.AreSame(globalArg, found); args.CommandLine = "-ActionInt"; args.PreviousToken = args.CommandLine; Assert.IsNull(PowerArgsRichCommandLineReader.FindContextualArgument(args)); Assert.IsNull(PowerArgsRichCommandLineReader.FindContextualArgument(args)); var action = new CommandLineAction((d) => { }); action.Aliases.Add("TheAction"); var actionArg = new CommandLineArgument(typeof(int), "ActionInt"); action.Arguments.Add(actionArg); def.Actions.Add(action); args.CommandLine = "-TheString"; args.PreviousToken = args.CommandLine; args.ActionContext = action; found = PowerArgsRichCommandLineReader.FindContextualArgument(args); Assert.AreSame(globalArg, found); args.CommandLine = "-ActionInt"; args.PreviousToken = args.CommandLine; found = PowerArgsRichCommandLineReader.FindContextualArgument(args); Assert.AreSame(actionArg, found); }
///<summary> /// Constructs a new instance of <see cref="T:adapman.CommandLineInfo"/>. ///</summary> /// <param name="action">One of the <see cref="T:adapman.CommandLineAction"/> /// values that specifies what the user wants to do.</param> /// <param name="wifiSSID">(Optional.) Specifies the SSID of the Wi-Fi network /// that the specified action applies to.</param> /// <param name="wifiPassword">(Optional.) Specifies the password, or network /// security key, of the Wi-Fi network that the specified action applies to.</param> protected CommandLineInfo(CommandLineAction action, string wifiSSID = "", string wifiPassword = "") { // THe action will always be specified Action = action; // If the action does not apply to a specific Wi-Fi network, the // properties below will be initialized to blank by default. WifiSSID = wifiSSID; WifiPassword = wifiPassword; }
public void TestPowerArgsRichCommandLineReaderFindCurrentTokenArgument() { bool expect; try { PowerArgsRichCommandLineReader.FindCurrentTokenArgument(null, null, out expect); } catch (NullReferenceException ex) { Assert.IsTrue(ex.Message.Contains("ambient")); } CommandLineArgumentsDefinition def = new CommandLineArgumentsDefinition(); var globalArg = new CommandLineArgument(typeof(int), "TheInt"); def.Arguments.Add(globalArg); Assert.IsNull(PowerArgsRichCommandLineReader.FindCurrentTokenArgument(null, null, out expect, def)); Assert.IsFalse(expect); var found = PowerArgsRichCommandLineReader.FindCurrentTokenArgument(null, "-TheInt", out expect, def); Assert.AreSame(globalArg, found); Assert.IsTrue(expect); found = PowerArgsRichCommandLineReader.FindCurrentTokenArgument(null, "/TheInt", out expect, def); Assert.AreSame(globalArg, found); Assert.IsTrue(expect); found = PowerArgsRichCommandLineReader.FindCurrentTokenArgument(null, "TheInt", out expect, def); Assert.IsNull(found); Assert.IsFalse(expect); found = PowerArgsRichCommandLineReader.FindCurrentTokenArgument(null, "-ActionInt", out expect, def); Assert.IsNull(found); Assert.IsTrue(expect); var action = new CommandLineAction((d) => { }); action.Aliases.Add("TheAction"); var actionArg = new CommandLineArgument(typeof(int), "ActionInt"); action.Arguments.Add(actionArg); def.Actions.Add(action); found = PowerArgsRichCommandLineReader.FindCurrentTokenArgument(action, "-ActionInt", out expect, def); Assert.AreSame(actionArg, found); Assert.IsTrue(expect); }
private static void ShowUsage(CommandLineAction specifiedAction) { if (specifiedAction == null) { ConsoleString.Write("Supported actions: ", ConsoleColor.Cyan); Console.WriteLine("Deploy, CreateListShardMap, CreateRangeShardMap, AddListMapShard, AddRangeMapShard"); Console.WriteLine(""); return; } Console.WriteLine(""); Console.WriteLine(""); ConsoleString.WriteLine(specifiedAction.DefaultAlias, ConsoleColor.Yellow); ConsoleString.WriteLine(new string('-', 80), ConsoleColor.DarkGray); Console.WriteLine(""); Console.WriteLine(specifiedAction.Description); Console.WriteLine(""); ConsoleString.WriteLine(new string('-', 80), ConsoleColor.DarkGray); Console.WriteLine(""); switch (specifiedAction.DefaultAlias) { case "Deploy": ArgUsage.GetStyledUsage <DeployArgs>("Deploy action").WriteLine(); break; case "CreateListShardMap": case "CreateRangeShardMap": ArgUsage.GetStyledUsage <CreateShardMapArgs>($"{specifiedAction.DefaultAlias} action").WriteLine(); break; case "AddListMapShard": ArgUsage.GetStyledUsage <AddListMapShardArgs>("AddListMapShard action").WriteLine(); break; case "AddRangeMapShard": ArgUsage.GetStyledUsage <AddRangeMapShardArgs>("AddRangeMapShard action").WriteLine(); break; case "AddInt32RangeMapShards": ArgUsage.GetStyledUsage <AddInt32RangeMapShardsArgs>("AddInt32RangeMapShards action").WriteLine(); break; } }
public void TestModeledActionREPL() { int invokeCount = 0; CommandLineArgumentsDefinition definition = new CommandLineArgumentsDefinition(); definition.Metadata.Add(new TabCompletion() { REPL = true, Indicator = "$" }); var action = new CommandLineAction((d) => { Assert.AreEqual("go", d.SpecifiedAction.DefaultAlias); if (invokeCount == 0) { Assert.AreEqual("Hawaii", d.SpecifiedAction.Arguments[0].RevivedValue); } else if (invokeCount == 1) { Assert.AreEqual("Mexico", d.SpecifiedAction.Arguments[0].RevivedValue); } invokeCount++; }); action.Aliases.Add("go"); action.Description = "A simple action"; definition.Actions.Add(action); var destination = new CommandLineArgument(typeof(string), "destination"); destination.Metadata.Add(new ArgRequired()); destination.Description = "The place to go to"; action.Arguments.Add(destination); var provider = TestConsoleProvider.SimulateConsoleInput("g\t -dest\t Hawaii{enter}go -dest\t Mexico{enter}quit"); Args.InvokeAction(definition, "$"); Assert.AreEqual(2, invokeCount); }
public void TestPowerArgsRichCommandLineReaderFindContextualArgument() { try { PowerArgsRichCommandLineReader.FindContextualArgument("-TheString", null); Assert.Fail("An exception should have been thrown"); } catch (NullReferenceException ex) { Assert.IsTrue(ex.Message.Contains("ambient")); } CommandLineArgumentsDefinition def = new CommandLineArgumentsDefinition(); var globalArg = new CommandLineArgument(typeof(string), "TheString"); def.Arguments.Add(globalArg); var found = PowerArgsRichCommandLineReader.FindContextualArgument("-TheString", null, def); Assert.AreSame(globalArg, found); found = PowerArgsRichCommandLineReader.FindContextualArgument("/TheString", null, def); Assert.AreSame(globalArg, found); Assert.IsNull(PowerArgsRichCommandLineReader.FindContextualArgument("-ActionInt", null, def)); Assert.IsNull(PowerArgsRichCommandLineReader.FindContextualArgument(null, null, def)); var action = new CommandLineAction((d) => { }); action.Aliases.Add("TheAction"); var actionArg = new CommandLineArgument(typeof(int), "ActionInt"); action.Arguments.Add(actionArg); def.Actions.Add(action); found = PowerArgsRichCommandLineReader.FindContextualArgument("-TheString", action, def); Assert.AreSame(globalArg, found); found = PowerArgsRichCommandLineReader.FindContextualArgument("-ActionInt", action, def); Assert.AreSame(actionArg, found); }
/// <summary> /// A helper that detects the argument represented by the current token given a definition. /// </summary> /// <param name="contextualAction">An action to inspect for a match if the current token does not match a global argument. Pass null to only check global arguments.</param> /// <param name="currentToken">The token to inspect. If you pass null you will get null back.</param> /// <param name="expectMatchingArg">This will be set to true if the current token starts with a '-' or a '/' meaning that the token was an argument indicator, even if it didn't match an argument in the definition.</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>An argument that is matched by the given token or null if there was no match</returns> public static CommandLineArgument FindCurrentTokenArgument(CommandLineAction contextualAction, string currentToken, out bool expectMatchingArg, CommandLineArgumentsDefinition def = null) { def = PassThroughOrTryGetAmbientDefinition(def); if (currentToken == null) { expectMatchingArg = false; return(null); } string currentTokenArgumentNameValue = null; expectMatchingArg = false; if (currentToken.StartsWith("-")) { currentTokenArgumentNameValue = currentToken.Substring(1); expectMatchingArg = true; } else if (currentToken.StartsWith("/")) { currentTokenArgumentNameValue = currentToken.Substring(1); expectMatchingArg = true; } CommandLineArgument currentTokenArgument = null; if (currentTokenArgumentNameValue != null) { currentTokenArgument = def.Arguments.Where(arg => arg.IsMatch(currentTokenArgumentNameValue)).SingleOrDefault(); if (currentTokenArgument == null && contextualAction != null) { currentTokenArgument = contextualAction.Arguments.Where(arg => arg.IsMatch(currentTokenArgumentNameValue)).SingleOrDefault(); } } return(currentTokenArgument); }
static void Execute(CommandLineAction action) { switch (action.operation) { case CommandLineOperation.ResetMaterialKeywords: { Console.WriteLine("[HDEditorCLI][ResetMaterialKeywords] Starting material reset"); var matIds = AssetDatabase.FindAssets("t:Material"); for (int i = 0, length = matIds.Length; i < length; i++) { var path = AssetDatabase.GUIDToAssetPath(matIds[i]); var mat = AssetDatabase.LoadAssetAtPath <Material>(path); if (HDShaderUtils.ResetMaterialKeywords(mat)) { Console.WriteLine("[HDEditorCLI][ResetMaterialKeywords] " + path); } } break; } } }
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; }
/// <summary> /// A helper that detects the argument represented by the current token given a definition. /// </summary> /// <param name="contextualAction">An action to inspect for a match if the current token does not match a global argument. Pass null to only check global arguments.</param> /// <param name="currentToken">The token to inspect. If you pass null you will get null back.</param> /// <param name="expectMatchingArg">This will be set to true if the current token starts with a '-' or a '/' meaning that the token was an argument indicator, even if it didn't match an argument in the definition.</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>An argument that is matched by the given token or null if there was no match</returns> public static CommandLineArgument FindCurrentTokenArgument(CommandLineAction contextualAction, string currentToken, out bool expectMatchingArg, CommandLineArgumentsDefinition def = null) { def = PassThroughOrTryGetAmbientDefinition(def); if (currentToken == null) { expectMatchingArg = false; return null; } string currentTokenArgumentNameValue = null; expectMatchingArg = false; if (currentToken.StartsWith("-")) { currentTokenArgumentNameValue = currentToken.Substring(1); expectMatchingArg = true; } else if (currentToken.StartsWith("/")) { currentTokenArgumentNameValue = currentToken.Substring(1); expectMatchingArg = true; } CommandLineArgument currentTokenArgument = null; if (currentTokenArgumentNameValue != null) { currentTokenArgument = def.Arguments.Where(arg => arg.IsMatch(currentTokenArgumentNameValue)).SingleOrDefault(); if (currentTokenArgument == null && contextualAction != null) { currentTokenArgument = contextualAction.Arguments.Where(arg => arg.IsMatch(currentTokenArgumentNameValue)).SingleOrDefault(); } } return currentTokenArgument; }
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); }
/// <summary> /// A helper that detects the argument represented by the current token given a definition. /// </summary> /// <param name="previousToken">The token to inspect. If you pass null you will get null back.</param> /// <param name="contextualAction">An action to inspect for a match if the current token does not match a global argument. Pass null to only check global arguments.</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>An argument that is matched by the given token or null if there was no match</returns> 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); }
/// <summary> /// A helper that detects the argument represented by the current token given a definition. /// </summary> /// <param name="previousToken">The token to inspect. If you pass null you will get null back.</param> /// <param name="contextualAction">An action to inspect for a match if the current token does not match a global argument. Pass null to only check global arguments.</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>An argument that is matched by the given token or null if there was no match</returns> 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; }