Tag Archives: event

Cinchoo – Command Line Argument Parser, Part 9

Inside Command Line Argument Parser – Events

In this section, I’ll brief you about list of callback event mechanism available during Command-Line argument parsing, where you can carry out custom actions to meet your needs.

1. Add a reference to Cinchoo.Core.dll

2. Namespace: Cinchoo.Core.Shell

Below are the events fired by Cinchoo framework while loading the command line arguments

Object level events
  • BeforeCommandLineArgObjectLoaded – Event fired before command line arguments are parsed and loaded.
[ChoBeforeCommandLineArgObjectLoadedHandler]
public void OnBeforeCommandLineArgObjectLoadedHandler(object sender, ChoPreviewCommandLineArgObjectEventArgs args)
{
    args.Cancel = false;
}
  • AfterCommandLineArgObjectLoaded – Event fired after command line arguments are parsed and loaded successfully.
[ChoAfterCommandLineArgObjectLoadedHandler]
public void OnAfterCommandLineArgObjectLoadedHandler(object sender, ChoCommandLineArgObjectEventArgs args)
{
}
  • CommandLineArgObjectLoadError – Event fired when there was exception found while parsing the command line arguments.
[ChoCommandLineArgObjectLoadErrorHandler]
public void OnCommandLineArgObjectLoadError(object sender, ChoCommandLineArgObjectErrorEventArgs args)
{
    Exception ex = args.Exception;
    args.Handled = true;
}
Member level events
  • BeforeCommandLineArgLoaded – Event fired before each command-line argument member loaded.
[ChoBeforeCommandLineArgLoadedHandler]
public void OnBeforeCommandLineArgLoadedHandler(object sender, ChoPreviewCommandLineArgEventArgs args)
{
    if (args.MemberName == "Folders")
    {
        if (args.Value is string)
        {
            string folders = args.Value as string;

            if (!folders.IsNullOrWhiteSpace())
            {
                Folders = folders.SplitNTrim(";");
                args.Cancel = true;
            }
        }
    }
}
  • AfterCommandLineArgLoaded – Event fired after each command-line argument member loaded successfully.
[ChoAfterCommandLineArgLoadedHandler]
public void OnAfterCommandLineArgLoadedHandler(object sender, ChoCommandLineArgEventArgs args)
{
    if (args.MemberName == "Folders")
    {
    }
}
  • CommandLineArgLoadError – Event fired when there was error while loading command-line argument member.
[ChoCommandLineArgObjectLoadErrorHandler]
public void OnCommandLineArgObjectLoadError(object sender, ChoCommandLineArgObjectErrorEventArgs args)
{
    Exception ex = args.Exception;
    args.Handled = true;
}
  • CommandLineArgMemberNotFound – Event fired when there is no command-line member defined for a command-line switch.
[ChoCommandLineArgMemberNotFoundHandler]
public void OnCommandLineArgMemberNotFoundHandler(object sender, ChoCommandLineArgNotFoundEventArgs args)
{
    string cmdLineArgSwitch = args.CmdLineArgSwitch;
    string cmdLineArgValue = args.CmdLineArgValue;
}
  • UnrecognizedCommandLineArgFound – Event fired when there is an unrecognized command line switch found.
[ChoUnrecognizedCommandLineArgFoundHandler]
public void OnUnrecognizedCommandLineArgFoundHandler(object sender, ChoUnrecognizedCommandLineArgEventArg args)
{
    string cmdLineArgValue = args.CmdLineArgValue;
}

Cinchoo – Configuration framework, part 10

Inside Configuration Object Load Events

Download sample source files (Require .NET 4.0 / Visual Studio 2010)

In this section, I’ll go over the details of possible configuration object events you can define and their usage.

Load Events

Following are the events fired while loading  each Configuration object member values from underlying configuration source
  • BeforeConfigurationObjectMemberLoaded – Event fired before each configuration object member loaded.
  • AfterConfigurationObjectMemberLoaded – Event fired after each configuration object member loaded.
  • ConfigurationObjectMemberLoadError – Event fired when configuration object member had some error while loading.
Following are the events fired while loading  Configuration object from underlying configuration source
  • AfterConfigurationObjectLoaded - Event fired when configuration object loaded successfully.
  • ConfigurationObjectLoadError - Event fired when configuration object had some error while loading.

Order of events fired

Below diagram depicts the order of events fired while loading configuration parameters from the sources.

While loading each configuration member value from underlying source, framework fires BeforeConfigurationObjectMemberLoaded event when the configuration member value is different from underlying source value. In there you have chance to do validate, modify, massage the new value or do anything in here.

[ChoBeforeConfigurationObjectMemberLoadedHandler]
void OnBeforeConfigurationObjectMemberLoaded(object sender, ChoPreviewConfigurationObjectMemberEventArgs e)
{
}

Set ChoPreviewConfigurationObjectMemberEventArgs.Cancel to true/false, whether to continue the process of loading the value or not. Default is false.

If the value of ChoPreviewConfigurationObjectMemberEventArgs.Cancel is false, the framework will try to load the ChoPreviewConfigurationObjectMemberEventArgs.State (usually it carries the converted configuration member source value, which you have chance to inspect / modify it in this event handler) to the corresponding configuration object member. ChoPreviewConfigurationObjectMemberEventArgs.OriginalState contains the raw (unconverted) configuration member source value.  ChoPreviewConfigurationObjectMemberEventArgs.MemberName contains the configuration member name.

Then framework will fire AfterConfigurationObjectMemberLoaded event, where you have a chance to perform any post member related operations.

Above steps repeated for each member of the configuration object.

Any error will be captured and reported in the configuration log file. (Will talk about it later.)

Finally framework will fire AfterConfigurationObjectLoaded event after successful loading of configuration object. If for any reason finds any failure or exception during the loading operation, framework will fire ConfigurationObjectLoadError event, where you have chance to perform any recovery steps based on the error/exception.


Follow

Get every new post delivered to your Inbox.