/// Launches in REPL mode protected void StartREPL() { string input = null; ValueArgument cmdArg = new PositionalArgument() { Key = "Command", Description = "The name of the command to execute, or Quit to exit", IsRequired = true, Validate = ValidateCommand }; System.Console.Title = string.Format("{0} Shell", ApplicationInfo.Title); _cmdLine.DisplayTitle(System.Console.Out); _cmdLine.WriteLine("Enter 'help' for help, or 'quit' to exit"); _cmdLine.WriteLine(); _inREPL = true; while (true) { input = _cmdLine.ReadLine("hfm> "); if (UI.Interrupted || String.Compare(input, "exit", StringComparison.OrdinalIgnoreCase) == 0 || String.Compare(input, "quit", StringComparison.OrdinalIgnoreCase) == 0) { break; } _cmdLine.Definition.Clear(); SetupCommandLine(); ProcessCommandLine(("hfm " + input).SplitSpaces()); } _inREPL = false; }
When_formatting_command_line_with_Single_style_Then_it_should_return_Value_string_without_prefix() { var sut = new PositionalArgument("Value"); IEnumerable <string> commandLine = sut.FormatCommandLine("---", CommandLineArgumentStyle.SingleWithEqualsSignDelimiter); commandLine.Should().Equal("Value"); }
public void ComposesOptionsForRequiredUnamedArguments() { PositionalArgument <string> positionalArgument = this.AddpositionalArgument("placeholder", "description"); this.requiredArguments.Add(positionalArgument); Usage result = this.testee.Compose(); result.Options.Should().Be(Lines("<placeholder>\tdescription")); }
public void ComposesArgumentsForRequiredPositionalNamedArguments() { PositionalArgument <string> positionalArgument = this.AddpositionalArgument("placeholder", null); this.requiredArguments.Add(positionalArgument); Usage result = this.testee.Compose(); result.Arguments.Should().Be("<placeholder>"); }
public TOutput ExecuteCommand <TOutput>(string command, PositionalArgument primaryArguments, params IArgument[] additionalArguments) { ProcessResult processResult = ExecuteCommandAndVerifyResult(command, new[] { primaryArguments }, additionalArguments); var commandOutputSerializer = new YAXSerializer(typeof(TOutput), YAXExceptionHandlingPolicies.ThrowWarningsAndErrors); var validStandardOuputText = RemoveUnsupportedCharacters(processResult.StandardOutput); var commandOutput = commandOutputSerializer.Deserialize(validStandardOuputText); return((TOutput)commandOutput); }
private PositionalArgument <string> AddpositionalArgument(string placeholder, string description) { var positionalArgument = new PositionalArgument <string>(_); var positionalHelp = new PositionalHelp <string>(positionalArgument) { Placeholder = placeholder, Description = description }; this.arguments.Add(positionalArgument); this.help.Add(positionalHelp); return(positionalArgument); }
public void Succeeds_WhenRequiredPositionalArgumentsArePresent() { var positionalArgument = new PositionalArgument <string>(v => { }); var configuration = new CommandLineConfiguration( new[] { positionalArgument }, NoLongAliases, NoRequiredArguments, NoHelp); var testee = new CommandLineParser(configuration); var result = testee.Parse(new[] { "value" }); result.Succeeded.Should().BeTrue(); }
public PositionalArgumentComposer( Action <T> callback, CommandLineParserConfigurator configurator, Action <Argument> addToArguments, Action <Argument> addToRequired, Action <Help.Help> addToHelp) : base(configurator) { this.addToRequired = addToRequired; this.current = new PositionalArgument <T>(callback); this.help = new PositionalHelp <T>(this.current); addToArguments(this.current); addToHelp(this.help); }
public void Fails_WhenRequiredPositionalArgumentIsMissing() { var positionalArgument = new PositionalArgument <string>(v => { }); var configuration = new CommandLineConfiguration( new[] { positionalArgument }, NoLongAliases, new[] { positionalArgument }, NoHelp); var testee = new CommandLineParser(configuration); var result = testee.Parse(new string[] { }); result.Should() .BeFailedParsingResult() .WithMessage(Errors.Errors.RequiredPositionalArgumentIsMissing); }
/// <summary> /// Constructs and prints out a help string for all <see cref="CommandLineCommand"/> classes supported by this program /// </summary> /// <param name="manipulator">Unused parameter</param> public override void PerformFunction(MySqlDataManipulator manipulator) { var commands = ReflectionHelper.GetAllCommands(); string format = "Command{0}:\n\tKeyed Arguments: {1}\n\tPositional Arguments: {2}\n"; int index = 1; foreach (CommandLineMapping mapping in commands) { //Determine all keyed and positional arguments required by the current command //these arguments are determined by the attributes applied to the fields of the current command List <string> keyedArguments = new List <string>(); List <string> positionalArguments = new List <string>(); foreach (FieldInfo f in mapping.KeyedFields) { KeyedArgument arg = (KeyedArgument)System.Attribute.GetCustomAttribute(f, typeof(KeyedArgument)); string toAdd = null; if (arg.ValueRequired) { toAdd = arg.Key + ' ' + arg.RequiredValue; } else { toAdd = arg.Key + ' ' + "%" + f.Name + "%: " + f.FieldType.Name; } keyedArguments.Add(toAdd); } foreach (FieldInfo f in mapping.PositionalFields) { PositionalArgument arg = (PositionalArgument)System.Attribute.GetCustomAttribute(f, typeof(PositionalArgument)); while (arg.Position >= positionalArguments.Count) { positionalArguments.Add(null); } positionalArguments[arg.Position] = "\"%" + f.Name + "%\": " + f.FieldType.Name; } //Display the formatted help string string toPrint = string.Format(format, index, string.Join(' ', keyedArguments), string.Join(' ', positionalArguments)); Console.WriteLine(toPrint); index++; } }
/// Launches in REPL mode protected void StartREPL() { // TODO: Process any options passed on command-line string input = null; ValueArgument cmdArg = new PositionalArgument() { Key = "Command", Description = "The name of the command to execute, or Quit to exit", IsRequired = true, Validate = ValidateCommand }; System.Console.Title = string.Format("{0} Shell", ApplicationInfo.Title); _cmdLine.DisplayTitle(System.Console.Out); _cmdLine.WriteLine("Enter 'help' for help, or 'quit' to exit"); _cmdLine.WriteLine(); _inREPL = true; while (true) { HFM.Session sess = (HFM.Session)_context[typeof(HFM.Session)]; input = _cmdLine.ReadLine(string.Format("{0}hfm> ", sess == null ? "" : "[" + sess.Cluster + ':' + sess.Application + "] ")); if (UI.Interrupted || String.Compare(input, "exit", StringComparison.OrdinalIgnoreCase) == 0 || String.Compare(input, "quit", StringComparison.OrdinalIgnoreCase) == 0) { break; } if (input.Trim().Length > 0) { _cmdLine.ClearArguments(); SetupCommandLine(); ProcessCommandLine(("hfm " + input).SplitSpaces()); } } _inREPL = false; }
protected PositionalBase(CliParser <TConfig> parser, IValueStoreDefinition store) : base(parser) { Arg = Arg = new PositionalArgument(store); }
private IEnumerable <string> FormatPositionalArgument(PositionalArgument positionalArgument) { return(this.FormatValue(positionalArgument.Value)); }
/// <summary> /// Analyzes the command lines stored in <paramref name="argumentsIn"/> and performs any company or user creation necessary /// </summary> /// <param name="manipulatorIn">Object to use to access the database if needed</param> /// <param name="argumentsIn">The command line arguments to analyze</param> /// <returns>True if there was a creation request made (thus the program should terminate), false otherwise (the program should continue onward)</returns> public static bool PerformRequestedCreation(MySqlDataManipulator manipulatorIn, CommandLineArgumentParser argumentsIn) { var commands = ReflectionHelper.GetAllCommands(); foreach (CommandLineMapping mapping in commands) { if (mapping.KeyedFields.Count != argumentsIn.KeyedArguments.Count) { continue; } if (mapping.PositionalFields.Count != argumentsIn.PositionalArguments.Count) { continue; } bool keysPresent = true; foreach (FieldInfo f in mapping.KeyedFields) { KeyedArgument arg = (KeyedArgument)System.Attribute.GetCustomAttribute(f, typeof(KeyedArgument)); if (!argumentsIn.KeyedArguments.ContainsKey(arg.Key)) { keysPresent = false; break; } if (arg.ValueRequired && !argumentsIn.KeyedArguments[arg.Key].Equals(arg.RequiredValue)) { keysPresent = false; break; } object value = null; if (f.FieldType.IsPrimitive) { value = f.FieldType.GetMethod("Parse", new[] { typeof(string) }).Invoke(null, new[] { argumentsIn.KeyedArguments[arg.Key] }); } else if (f.FieldType.Equals(typeof(string))) { value = argumentsIn.KeyedArguments[arg.Key]; } else { throw new ArgumentException("Non primitive field marked as a Keyed Argument in class " + mapping.Command.GetType()); } f.SetValue(mapping.Command, value); } if (!keysPresent) { continue; } keysPresent = true; foreach (FieldInfo f in mapping.PositionalFields) { PositionalArgument arg = (PositionalArgument)System.Attribute.GetCustomAttribute(f, typeof(PositionalArgument)); if (arg.Position > mapping.PositionalFields.Count) { keysPresent = false; break; } object value = null; if (f.FieldType.IsPrimitive) { value = f.FieldType.GetMethod("Parse", new[] { typeof(string) }).Invoke(null, new[] { argumentsIn.PositionalArguments[arg.Position] }); } else if (f.FieldType.Equals(typeof(string))) { value = argumentsIn.PositionalArguments[arg.Position]; } else { throw new ArgumentException("Non primitive, Non string field marked as a Positional Argument in class " + mapping.Command.GetType()); } f.SetValue(mapping.Command, value); } if (!keysPresent) { continue; } mapping.Command.PerformFunction(manipulatorIn); return(true); } return(false); }
public virtual Arguments WithPositionalArgument(PositionalArgument arg) { _positionalArgs.Add(arg); return(this); }
private IEnumerable <string> Format(PositionalArgument positionalArgument) { return(Format(positionalArgument.Value)); }
public PositionalArgumentBuilder(Action <PositionalArgument> positionalArgumentBuilt, TArgsBuilder argsBuilder) { this.positionalArgumentBuilt = positionalArgumentBuilt; this.argsBuilder = argsBuilder; positionalArgument = new PositionalArgument(typeof(TParam)); }
public CompilerError UnknownArgument(FunctionType type, PositionalArgument arg) => Add($"Argument {arg.Position} does not exist in {type}.");
public PositionalHelp(PositionalArgument <T> argument) : base(argument) { this.Placeholder = "value"; }
public PositionalArgumentStep(Step previous, PositionalArgument positionalArgument) : base(previous) { this.Description = positionalArgument; }