Esempio n. 1
0
        private static T Execute <T>(Func <T> argsProcessingCode) where T : class
        {
            ArgHook.HookContext.Current = new ArgHook.HookContext();

            try
            {
                return(argsProcessingCode());
            }
            catch (ArgCancelProcessingException ex)
            {
                return(CreateEmptyResult <T>(ArgHook.HookContext.Current, cancelled: true));
            }
            catch (ArgException ex)
            {
                ex.Context = ArgHook.HookContext.Current;
                var definition = ArgHook.HookContext.Current.Definition;
                if (definition.ExceptionBehavior.Policy == ArgExceptionPolicy.StandardExceptionHandling)
                {
                    ex.Message.ToRed().WriteLine();
                    UsageTemplateProvider.GetUsage(definition.ExceptionBehavior.UsageTemplateProviderType, definition).Write();
                    return(CreateEmptyResult <T>(ArgHook.HookContext.Current, ex));
                }
                else
                {
                    throw;
                }
            }
            finally
            {
                ArgHook.HookContext.Current = null;
            }
        }
Esempio n. 2
0
        /// <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;
            }
            var usage = UsageTemplateProvider.GetUsage(UsageTemplateProviderType, context.Definition);

            usage.Write();
            FireUsageWritten(usage);
        }
Esempio n. 3
0
        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));
        }