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; } }
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); }
/// <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; }
/// <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; }
protected bool Equals(CommandLineAction other) { return Equals(Source, other.Source); }
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.GetUsage(definition, "test"); Assert.IsTrue(usage.Contains("A simple action")); Assert.IsTrue(usage.Contains("The place to go to")); }
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); }
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); }
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); }