private bool DoParseArgumentsVerbs(ParserContext context) { var verbs = ReflectionUtil.RetrievePropertyList<VerbOptionAttribute>(context.Target); var helpInfo = ReflectionUtil.RetrieveMethod<HelpVerbOptionAttribute>(context.Target); if (context.HasNoArguments()) { if (helpInfo != null || Settings.HelpWriter != null) { DisplayHelpVerbText(context.Target, helpInfo, null); } return false; } var optionMap = OptionMap.Create(context.Target, verbs, Settings); // Read the verb from command line arguments if (TryParseHelpVerb(context.Arguments, context.Target, helpInfo, optionMap)) { // Since user requested help, parsing is considered a fail return false; } var verbOption = optionMap[context.FirstArgument]; // User invoked a bad verb name if (verbOption == null) { if (helpInfo != null) { DisplayHelpVerbText(context.Target, helpInfo, null); } return false; } if (verbOption.GetValue(context.Target) == null) { // Developer has not provided a default value and did not assign an instance verbOption.CreateInstance(context.Target); } var verbResult = DoParseArgumentsCore(context.ToCoreInstance(verbOption)); if (!verbResult && helpInfo != null) { // Particular verb parsing failed, we try to print its help DisplayHelpVerbText(context.Target, helpInfo, context.FirstArgument); } return verbResult; }
private bool DoParseArgumentsDispatcher(ParserContext context) { return context.Target.HasVerbs() ? DoParseArgumentsVerbs(context) : DoParseArgumentsCore(context); }
private bool DoParseArgumentsCore(ParserContext context) { var hadError = false; var optionMap = OptionMap.Create(context.Target, Settings); optionMap.SetDefaults(); var valueMapper = new ValueMapper(context.Target, Settings.ParsingCulture); var arguments = new StringArrayEnumerator(context.Arguments); while (arguments.MoveNext()) { var argument = arguments.Current; if (string.IsNullOrEmpty(argument)) { continue; } var parser = ArgumentParser.Create(argument, Settings.IgnoreUnknownArguments); if (parser != null) { var result = parser.Parse(arguments, optionMap, context.Target); if ((result & PresentParserState.Failure) == PresentParserState.Failure) { SetParserStateIfNeeded(context.Target, parser.PostParsingState); hadError = true; continue; } if ((result & PresentParserState.MoveOnNextElement) == PresentParserState.MoveOnNextElement) { arguments.MoveNext(); } } else if (valueMapper.CanReceiveValues) { if (!valueMapper.MapValueItem(argument)) { hadError = true; } } } hadError |= !optionMap.EnforceRules(); return !hadError; }
private bool DoParseArguments(string[] args, object options) { var pair = ReflectionUtil.RetrieveMethod<HelpOptionAttribute>(options); var helpWriter = Settings.HelpWriter; _context = new ParserContext(args, options); if (pair != null && helpWriter != null) { // If help can be handled is displayed if is requested or if parsing fails if (ParseHelp(args, pair.Right) || !DoParseArgumentsDispatcher(_context)) { string helpText; HelpOptionAttribute.InvokeMethod(options, pair, out helpText); helpWriter.Write(helpText); return false; } return true; } return DoParseArgumentsDispatcher(_context); }
/// <summary> /// Parses a <see cref="System.String"/> array of command line arguments with verb commands, setting values in <paramref name="options"/> /// parameter instance's public fields decorated with appropriate attributes. If parsing fails, the method invokes /// the <paramref name="onFail"/> delegate, if null exits with <see cref="Parser.DefaultExitCodeFail"/>. /// This overload supports verb commands. /// </summary> /// <param name="args">A <see cref="System.String"/> array of command line arguments.</param> /// <param name="options">An instance used to receive values. /// Parsing rules are defined using <see cref="CommandLine.BaseOptionAttribute"/> derived types.</param> /// <param name="onVerbCommand">Delegate executed to capture verb command name and instance.</param> /// <param name="onFail">The <see cref="Action"/> delegate executed when parsing fails.</param> /// <returns>True if parsing process succeed.</returns> /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="args"/> is null.</exception> /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="options"/> is null.</exception> /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="onVerbCommand"/> is null.</exception> public bool ParseArgumentsStrict(string[] args, object options, Action<string, object> onVerbCommand, Action onFail = null) { Assumes.NotNull(args, "args", SR.ArgumentNullException_ArgsStringArrayCannotBeNull); Assumes.NotNull(options, "options", SR.ArgumentNullException_OptionsInstanceCannotBeNull); Assumes.NotNull(options, "onVerbCommand", SR.ArgumentNullException_OnVerbDelegateCannotBeNull); object verbInstance = null; var context = new ParserContext(args, options); if (!this.DoParseArgumentsVerbs(context, ref verbInstance)) { onVerbCommand(context.FirstArgument ?? string.Empty, null); this.InvokeAutoBuildIfNeeded(options); if (onFail == null) { Environment.Exit(DefaultExitCodeFail); } else { onFail(); } return false; } onVerbCommand(context.FirstArgument ?? string.Empty, verbInstance); return true; }
/// <summary> /// Parses a <see cref="System.String"/> array of command line arguments with verb commands, setting values in <paramref name="options"/> /// parameter instance's public fields decorated with appropriate attributes. /// This overload supports verb commands. /// </summary> /// <param name="args">A <see cref="System.String"/> array of command line arguments.</param> /// <param name="options">An instance used to receive values. /// Parsing rules are defined using <see cref="CommandLine.BaseOptionAttribute"/> derived types.</param> /// <param name="onVerbCommand">Delegate executed to capture verb command name and instance.</param> /// <returns>True if parsing process succeed.</returns> /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="args"/> is null.</exception> /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="options"/> is null.</exception> /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="onVerbCommand"/> is null.</exception> public bool ParseArguments(string[] args, object options, Action<string, object> onVerbCommand) { Assumes.NotNull(args, "args", SR.ArgumentNullException_ArgsStringArrayCannotBeNull); Assumes.NotNull(options, "options", SR.ArgumentNullException_OptionsInstanceCannotBeNull); Assumes.NotNull(options, "onVerbCommand", SR.ArgumentNullException_OnVerbDelegateCannotBeNull); object verbInstance = null; var context = new ParserContext(args, options); var result = this.DoParseArgumentsVerbs(context, ref verbInstance); onVerbCommand(context.FirstArgument ?? string.Empty, result ? verbInstance : null); return result; }