/// <summary> /// Prompts the user to enter a value for the given property in the case that the option was specified with no value /// </summary> /// <param name="context">the parser context</param> public override void BeforePopulateProperty(ArgHook.HookContext context) { if (string.IsNullOrEmpty(context.ArgumentValue)) { do { var cli = new CliHelper(); ITabCompletionHandler tabHandler; IHighlighterConfigurator highlighterConfigurator; if (TabCompletionHandlerType.TryCreate <ITabCompletionHandler>(out tabHandler)) { cli.Reader.TabHandler.TabCompletionHandlers.Add(tabHandler); } if (HighlighterConfiguratorType.TryCreate <IHighlighterConfigurator>(out highlighterConfigurator)) { cli.Reader.Highlighter = new SimpleSyntaxHighlighter(); highlighterConfigurator.Configure(cli.Reader.Highlighter); } var result = cli.PromptForLine("Enter value for " + context.CurrentArgument.DefaultAlias); result = result == "" ? null : result; context.ArgumentValue = result; } while (KeepAsking && context.ArgumentValue == null); } }
/// <summary> /// Validates that the user actually specified a value and optionally prompts them when it is missing. /// </summary> /// <param name="argument">The argument being populated. This validator doesn't do anything with it.</param> /// <param name="arg">The value specified on the command line or null if it wasn't specified</param> public override void ValidateAlways(CommandLineArgument argument, ref string arg) { if (IsConditionallyRequired) { var matchingHook = (from h in argument.Metadata.Metas <ArgRequiredConditionalHook>() where h.parent == this select h).SingleOrDefault(); if (matchingHook == null) { argument.Metadata.Add(new ArgRequiredConditionalHook(this)); } } if (IsConditionallyRequired == false && arg == null && PromptIfMissing && ArgHook.HookContext.Current.Definition.IsNonInteractive == false) { var cli = new CliHelper(); ITabCompletionHandler tabHandler; IHighlighterConfigurator highlighterConfigurator; if (TabCompletionHandlerType.TryCreate <ITabCompletionHandler>(out tabHandler)) { cli.Reader.TabHandler.TabCompletionHandlers.Add(tabHandler); } if (HighlighterConfiguratorType.TryCreate <IHighlighterConfigurator>(out highlighterConfigurator)) { cli.Reader.Highlighter = new SimpleSyntaxHighlighter(); highlighterConfigurator.Configure(cli.Reader.Highlighter); } cli.Reader.UnregisterHandler(ConsoleKey.Escape); cli.Reader.RegisterHandler(KeyHandler.FromAction((searchReaderContext) => { TabCompletion tabCompletionInfo; if (ArgHook.HookContext.Current.Definition.IsNonInteractive == false && ArgHook.HookContext.Current.Definition.Metadata.TryGetMeta <TabCompletion>(out tabCompletionInfo) && tabCompletionInfo.REPL == true) { // if this is an interactive REPL then continue the REPL in this case as the user may have changed their mind about taking // this action - Note there are two places in this file that have this logic throw new REPLContinueException(); } else { throw new MissingArgException("The argument '" + argument.DefaultAlias + "' is required", new ArgumentNullException(argument.DefaultAlias)); } }, ConsoleKey.Escape)); arg = cli.PromptForLine("Enter value for " + argument.DefaultAlias); } if (arg == null && IsConditionallyRequired == false) { throw new MissingArgException("The argument '" + argument.DefaultAlias + "' is required", new ArgumentNullException(argument.DefaultAlias)); } }
private bool TryPreventExceptionWithPrompt(ArgHook.HookContext context) { if (parent.PromptIfMissing && ArgHook.HookContext.Current.Definition.IsNonInteractive == false) { var cli = new CliHelper(); ITabCompletionHandler tabHandler; IHighlighterConfigurator highlighterConfigurator; if (TabCompletionHandlerType.TryCreate <ITabCompletionHandler>(out tabHandler)) { cli.Reader.TabHandler.TabCompletionHandlers.Add(tabHandler); } if (HighlighterConfiguratorType.TryCreate <IHighlighterConfigurator>(out highlighterConfigurator)) { cli.Reader.Highlighter = new SimpleSyntaxHighlighter(); highlighterConfigurator.Configure(cli.Reader.Highlighter); } cli.Reader.UnregisterHandler(ConsoleKey.Escape); cli.Reader.RegisterHandler(KeyHandler.FromAction((searchReaderContext) => { TabCompletion tabCompletionInfo; if (context.Definition.IsNonInteractive == false && context.Definition.Metadata.TryGetMeta <TabCompletion>(out tabCompletionInfo) && tabCompletionInfo.REPL == true) { // if this is an interactive REPL then continue the REPL in this case as the user may have changed their mind about taking // this action - Note there are two places in this file that have this logic throw new REPLContinueException(); } else { throw new MissingArgException("The argument '" + context.CurrentArgument.DefaultAlias + "' is required", new ArgumentNullException(context.CurrentArgument.DefaultAlias)); } }, ConsoleKey.Escape)); context.ArgumentValue = cli.PromptForLine("Enter value for " + context.CurrentArgument.DefaultAlias); context.CurrentArgument.Populate(context); var property = context.CurrentArgument.Source as PropertyInfo; if (property != null && context.Args != null) { property.SetValue(context.Args, context.CurrentArgument.RevivedValue, null); } return(true); } else { return(false); } }
/// <summary> /// Prompts the user to enter a value for the given property in the case that the option was specified with no value /// </summary> /// <param name="context">the parser context</param> public override void BeforePopulateProperty(ArgHook.HookContext context) { if (context.ArgumentValue == string.Empty) { var cli = new CliHelper(); ITabCompletionHandler tabHandler; IHighlighterConfigurator highlighterConfigurator; if (TabCompletionHandlerType.TryCreate <ITabCompletionHandler>(out tabHandler)) { cli.Reader.TabHandler.TabCompletionHandlers.Add(tabHandler); } if (HighlighterConfiguratorType.TryCreate <IHighlighterConfigurator>(out highlighterConfigurator)) { cli.Reader.Highlighter = new SimpleSyntaxHighlighter(); highlighterConfigurator.Configure(cli.Reader.Highlighter); } context.ArgumentValue = cli.PromptForLine("Enter value for " + context.CurrentArgument.DefaultAlias); } }