0

I have a console application taking as input dozens of parameters. I'm using commandlineparser to help myself and i have no problem setting values to properties.

By the way I also have a lot of methods i need to recall.
The methods i need to call take as input any number of parameters between 0 and 7; some method is parameterless, most method take at least 3 parameters as input.

Searching for a solution for the easiest case (parameterless method) i came up with this solution: I introduced a new boolean property called IsExecuteOperation, i set this property, let commandlineparser do its job and then check the value and eventually call the method like this:

    class Program
    {
        static void Main( string[] args )
        {
            Parser.Default.ParseArguments<Options>( args )
                .WithParsed<Options>( o =>
                {
                    if( o.IsExecuteOperation )
                    {
                        o.ExecuteOperation();
                    }
                } );
        }
    
        public class Options
        {
            public bool IsExecuteOperation { get; set; }
    
            public void ExecuteOperation()
            {
                //...
            }
        }
    }

This looked like a good idea so i went this way with all the other parameterless methods.
The parameterless methods are 7 so, i have 7 options more to deal with, plus the additional code to check this flags and call the methods.

I proceeded to deal with the methods taking as input 1 param (2 methods).

The first method takes as input a number: this results in 2 new properties (1 for the method parameter + 1 property needed to signal wether to execute the method) + the additional code to check and execute the method itself.

The problem arises with the second single-param method: it takes as input an object that internally defines at least 9 properties that i absolutely need to be set to execute the method correctly.

Well i could have introduced those 9 properties too but at this point i just realized that it would have been technically possible but impractical and messy to apply the same approach for all my methods (50ish) and introduce hundreads of properties just to call those methods.

My question.. is my approach correct? Is there any better strategy to solve this? Is there a way to call methods directly?

Thank you.

1 Answers1

-1

UltraMapper.CommandLine does exactly what you want!

It allows you to call methods directly from commandline passing as many input parameters as you want.

It supports primitive and complex types as arguments; Collections as IEnumerable, List and arrays are also supported.

It allows you to drastically reduce the amount of code needed to deal with commandline args by getting rid of all the 'commandline flags' and dispatch code.

public class Program
{
    static void Main( string[] args )
    {
        CommandLine.Instance.Parse<Commands>( args );
        //just call with this args: --method1 ["my string" "mysecondstring" "etc"]
    }

    public class Commands
    {
        public void Method1( List<string> strings ) {  ///...  }
        public void Method2( SomeClass myParam, List<SomeOtherClass> mySecondParam ) {  ///...  }
    }
}

I released UltraMapper.CommandLine as an open source project on github.
Check it out, it might be helpful on your next project!

There’s also nuget package available here

Mauro Sampietro
  • 2,739
  • 1
  • 24
  • 50