Repos

ECLP {Easy Command Line Parser}.

02. EventHandler

In the previouse chapter you have seen a way to read arguments by fetching them one by one.

This approach could be expensive especially when you have many argument as you my see the code in the prevouse chapter with many loops.

So maybe we want to execute a code especially when an argument is set.

This is what we are going to do using EventHandler.

                        
using System;
using System.Collections.Generic;
using System.Linq;
using The_Morpher;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string commandLine = "start 5 3,6 true T --verbose --noClip --showStats -p driver=steave -p age=30 -c players=steave|john|clark -xc players=steave:21|john:15|clark:30";

            ECLP eCLP = new ECLP(commandLine);
            eCLP.AddedArgs += ECLP_AddedArgs;
            eCLP.AddedCollections += ECLP_AddedCollections;
            eCLP.AddedExCollections += ECLP_AddedExCollections;
            eCLP.AddedFlags += ECLP_AddedFlags;
            eCLP.AddedProperties += ECLP_AddedProperties;
            eCLP.Parse();

            Console.ReadKey();
        }

        private static void ECLP_AddedArgs(object sender, The_Morpher.Events.AddedArgsEvents e)
        {
            Console.WriteLine("Argument is " + e.Arg + " and its type is " + e.Arg.GetType());
        }
        private static void ECLP_AddedFlags(object sender, The_Morpher.Events.AddedFlagsEvents e)
        {
            // To show value
            Console.WriteLine(e.Flag);

            if (e.Flag == "--verbose")
                Console.WriteLine("Verbose mode is activated");
        }

        private static void ECLP_AddedProperties(object sender, The_Morpher.Events.AddedPropertiesEvents e)
        {
            // To show key
            Console.WriteLine(e.Property.Key);

            // to do job when a specific property is triggered
            if (e.Property.Key == "driver")
                Console.WriteLine(e.Property.Value);
            // this will show steave
        }
        private static void ECLP_AddedCollections(object sender, The_Morpher.Events.AddedCollectionsEvents e)
        {
            // To show key, Collection name
            Console.WriteLine(e.Collections.Key);

            // to loop true sub values
            foreach (object o in e.Collections.Value)
            {
                Console.WriteLine("sub property is " + o + " and its type is " + o.GetType());
            }
        }
        private static void ECLP_AddedExCollections(object sender, The_Morpher.Events.AddedExCollectionsEvents e)
        {
            // To show key, ExCollection's name
            Console.WriteLine(e.ExCollections.Key);

            // loop true the sub properties
            foreach (KeyValuePair sub in e.ExCollections.Value)
            {
                Console.WriteLine("sub property name is " + sub.Key + " and it's value is " + sub.Value + " and value type is " + sub.Value.GetType());
            }
        }
    }


}
                          
                        
                      

Summary

Description

01. Verbs

>>02. EventHandler

03. Parse

Services

App Developpement

Want me to dev an app for you ? dont hesitate to contact me.

Programming

Are you looking for a coder/teammate for your project ? Let's give it a try.

Job Invitation

Have a proposal for me ? we can discuss about it.

Donation maybe ?!

You want to buy me a coffe ? m.elharfi@gmail.com