Skip to content

SEDY's Library to parse command line arguments - attributes-based approach. C# and dotnetcore

License

Notifications You must be signed in to change notification settings

sebastiandymel/CLI-Argument-Parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 

Repository files navigation

CLI-Argument-Parser (dotnetcore)

SEDY's Library to parse command line arguments - attributes based approach. C# and dotnetcore

How to use it?

Create a class to hold desired configuration settings, mark with attributes:

    public class ProgramConfig
    {
        [Option]
        [OptionAlias("--help")]
        [OptionAlias("-h")]
        public bool ShowHelp { get; set; }

        [Option]
        [OptionAlias("--version")]
        [OptionAlias("-v")]
        public bool ShowVersionDetails { get; set; }

        [Option]
        [OptionAlias("--age-of-client")]
        [OptionAlias("-a")]
        public int Age { get; set; }
        
        [Option]
        [OptionAlias("--date")]
        [OptionAlias("-d")]
        public DateTime Date { get; set; }
        
        [Option]
        [OptionAlias("--some-value")]
        [OptionAlias("-val")]
        [OptionAlias("-x")]
        public double Value { get; set; }
    }

Parse it with IAttributeParser:

    public interface IAttributeParser
    {
        T Parse<T>(params string[] arguments) where T : new();
        void RegisterCustomConverter<T>(Func<string, T> converter) where T : class;
    }

Example:

    class Program
    {
        static void Main(string[] args)
        {
            var parser = new AttributeParser();
            var configuration = parser.Parse<ProgramConfig>(args);
        }
    }

Use it in application

if (configuration.ShowHelp)
    {
        Console.WriteLine("HEEELP");
    }

How to add new options?

Standard CLR types

Mark property with [Option] attribute. This will create a default alias with the name of property so it can be used right away by executing

C:>ProgramName -PropertyName value

To change the alias, mark property with [OptionAlias] attribute. For example [OptionAlias("--x")], then it can be used by executing

C:>ProgramName --x value

Boolean flags

Boolean properties does not require passing value (it is optional when executing program) So executing

C:>ProgramName -SomeFlag is equivalen to C:>ProgramName -SomeFlag=true To add boolean flag, simply add new property of type bool:

[Option]
[OptionAlias("--help")]
[OptionAlias("-h")]
public bool ShowHelp { get; set; }

Registering custom converters

When you want to define a propty of custom type, you can register a converter function that will parse command line argument value to this custom type.

For example, having custom type

        private class CustomType
        {
            public int SomeValue { get; set; }
        } 

You can parse it by registering converter:

            var parser = new AttributeParser();

            parser.RegisterCustomConverter<CustomType>(i =>
            {
                if (i == "mystringvalue")
                {
                    return new CustomType { SomeValue = 100 };
                }
                return null;
            });

About

SEDY's Library to parse command line arguments - attributes-based approach. C# and dotnetcore

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published