Repos

ECLP {Easy Command Line Parser}.

01. Verbs

A command line can have as many as different verbs you like but by preference better keep the Args in the first of the command so that you cant identify them easily.

                        
using System.Windows.Forms;
using The_Morpher;

namespace ConsoleApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            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);

            CommandResult cr = eCLP.Parse();
            Console.ReadKey();
        }
    }
}
                        
                      

Assuming it's a console project, the cr variable contain 5 collections, each one is dedicated to a specific verb.


  • Reading Verbs
  • Manually

    Here is a way to display all data by fetching them one by one

                            
    using System;
    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);
                CommandResult result = eCLP.Parse();
    
                #region Args
                foreach (var arg in result.Args)
                    Console.WriteLine("Arg " + arg.ToString() + " has a " + arg.GetType() + " type");
                #endregion
    
                Console.WriteLine(Environment.NewLine);
    
                #region Flags
                foreach (var flag in result.Flags)
                    Console.WriteLine("Flag " + flag.ToString() + " is set");
                #endregion
    
                #region Properties
                foreach (var property in result.Properties)
                {
                    string name = property.Key;
                    object value = property.Value;
                    Console.WriteLine("Property " + name + " has a value " + value + " and value type is " + value.GetType());
                }
                #endregion
    
                Console.WriteLine(Environment.NewLine);
    
                #region Collections
                foreach (var collection in result.Collections)
                {
                    string name = collection.Key;
                    object[] values = collection.Value;
                    Console.WriteLine("Collection " + name + " has " + values.Count() + " value(s)");
                    int counter = 0;
                    foreach(object value in values)
                        Console.WriteLine("\tValue " + ++counter + " is " + value + " and its type is " + value.GetType());                
                }
                #endregion
    
                Console.WriteLine(Environment.NewLine);
    
                #region ExCollections
                foreach (var exCol in result.ExCollections)
                {
                    string name = exCol.Key;
                    var values = exCol.Value;
                    Console.WriteLine("ExCollection " + name + " has " + values.Count() + " value(s)");
                    foreach (var value in values)
                    {
                        string subName = value.Key;
                        var subValue = value.Value;
                        Console.WriteLine("\tSub Property " + subName + " has value " + subValue + " and value type is " + subValue.GetType());
                    }
    
                }
                #endregion
                Console.ReadKey();
            }
        }
    }
                            
                          

  • Args
  • Args verb is different than the other patterns, because others are identified by a name like Properties, Collection, ExCollection and Flags are a string types and then we can easily associate it since the value is known (a check value is suffisant).

    But Args are different because they dont have the same type and don't have an identification name, they could be any type, like string, int, bool, float or char.

    So no way to identify an arg unless if you fix their positions and make each index for a specific task.

    Assuming this command line "James 29 false a"

    If we parse this command line we will get 4 arguments typed like so : string, int, bool and char

    You have to make your application ready to associate the first index for the username which will equal to James

    Second argument to his age which will equal to 29

    Third for activated or not for example which will be false "suspended or wait activation or whatever"

    Fourth for a user rank, in this case is a that could be for Admin, you can imagine other scenarios like m for moderator ...

                            
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using The_Morpher;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string commandLine = "James 29 false a";
    
                ECLP eCLP = new ECLP(commandLine);
                var result = eCLP.Parse();
    
                if(result.Args.Count != 4)
                {
                    Console.WriteLine("Command should have 4 arguments");
                    return;
                }
    
                string username = result.Args[0].ToString();
                int age = 20; // default value
    
                if(result.Args[1].GetType() == typeof(int))
                {
                    age = (int)result.Args[1];
                }
                else
                {
                    Console.WriteLine("Second argument for age should be an integer");
                    return;
                }
    
                bool activated;
                if(result.Args[2].GetType() == typeof(bool))
                {
                    activated = (bool)result.Args[2];
                }
                else
                {
                    Console.WriteLine("Third argument for activation should be a boolean");
                    return;
                }
    
                char rank;
                if(result.Args[3].GetType() == typeof(char))
                {
                    rank = (char)result.Args[3];
                }
                else
                {
                    Console.WriteLine("Fourth argument for account rank should be a char");
                    return;
                }
    
                Console.WriteLine("username is {0}", username);
                Console.WriteLine("age is {0}", age);
                Console.WriteLine("activated is {0}", activated);
                Console.WriteLine("Account Rank {0}", rank);
    
                Console.ReadKey();
            }
    
        }
    
    }                        
                            
                          

    If you remember i told you that it's better to keep Args verbs in the beginning of the command line.

    Well it's not an obligation, assuming this command line :

    "James 29 false -p language=En a"

    As before we used same command line as but we just add another verb as a property before last argument.

    in this case the order is not affected by the none arg verb, i mean that the last argument "a" have the index of 3 not 4 (first argument have the 0 index) as you may expect, because the -p language=En is not accounted in the arguments's index.

    So nothing will be changed in the last code we wrote because the 'a' will always have the four index in total.

    In the next chapter we will see another way to read the command using events.

    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