private static T DoStandardExceptionHandling<T>(ArgException ex, ArgHook.HookContext context, CommandLineArgumentsDefinition definition) where T : class { Console.WriteLine(ex.Message); ArgUsage.GetStyledUsage(definition, definition.ExceptionBehavior.ExeName, new ArgUsageOptions { ShowPosition = definition.ExceptionBehavior.ShowPositionColumn, ShowType = definition.ExceptionBehavior.ShowTypeColumn, ShowPossibleValues = definition.ExceptionBehavior.ShowPossibleValues, }).Write(); return CreateEmptyResult<T>(context, ex); }
public static ConsoleString GetUsage(Type usageTemplateProviderType, CommandLineArgumentsDefinition definition) { if (usageTemplateProviderType.GetInterfaces().Contains(typeof(IUsageTemplateProvider)) == false) { throw new InvalidArgDefinitionException("The UsageTemplateProviderType " + usageTemplateProviderType.FullName + " does not implement " + typeof(IUsageTemplateProvider).Name); } var provider = ObjectFactory.CreateInstance(usageTemplateProviderType) as IUsageTemplateProvider; string template = provider.GetTemplate(); var usage = ArgUsage.GenerateUsageFromTemplate(definition, template); return(usage); }
private static T DoStandardExceptionHandling <T>(ArgException ex, ArgHook.HookContext context, CommandLineArgumentsDefinition definition) where T : class { Console.WriteLine(ex.Message); if (definition.ExceptionBehavior.UsageTemplateFile != null) { var template = File.ReadAllText(definition.ExceptionBehavior.UsageTemplateFile); ArgUsage.GenerateUsageFromTemplate(definition, template).Write(); } else { UsageTemplateProvider.GetUsage(definition.ExceptionBehavior.UsageTemplateProviderType, definition).Write(); } return(CreateEmptyResult <T>(context, ex)); }
/// <summary> /// Writes the help as long as WriteHelp is true /// </summary> /// <param name="context">Context passed by the parser</param> public override void AfterCancel(ArgHook.HookContext context) { base.AfterCancel(context); if (WriteHelp == false) { return; } ArgUsage.GetStyledUsage(context.Definition, EXEName, new ArgUsageOptions() { ShowPosition = ShowPositionColumn, ShowType = ShowTypeColumn, ShowPossibleValues = ShowPossibleValues, }).Write(); }
/// <summary> /// Creates a new ArgLongForm attirbute using the given long form string. /// </summary> /// <param name="value">The long form value. You can provide the two dashes in this string or not. The --long-form pattern will enforced at runtime either way.</param> public ArgLongForm(string value) { this.BeforeParsePriority = 100; if (value == null) { throw new InvalidArgDefinitionException("Values for long form arguments cannot be null", new ArgumentNullException("value")); } if (value.StartsWith("--")) { value = value.Substring(2); } this.value = value; var myUsageHook = new UsageHook(); myUsageHook.HookExecuting += (usageInfo) => { if (target == null) { // This should ensure that the target should be populated if the current property is the target try { Args.Parse(usageInfo.Property.PropertyType); } catch (Exception) { } } if (target == null) { return; } if (usageInfo.Property == target) { usageInfo.Aliases.Add("--" + this.value); } }; ArgUsage.RegisterHook(null, myUsageHook); }