Home Basic usage Parse argument into object
Post
Cancel

Parse argument into object

Cliargs.NET allows to parse all arguments into a given object properties, this operation is done in several steps.

Set argument properties

Each property that corresponds to an argument must have the same type as the argument and has an attribute CliArgNameAttribute.

Example

If you have set an argument Age with type uint like the following:

1
2
3
4
5
container.Register(
                CliArg.New<uint>("UserAge")
                .WithLongName("age")
                .WithShortName("a")
            );

Then the property where the data is parsed it should be similar than in the following code:

1
2
3
4
5
public class MyArgs
    {
        [CliArgName("UserAge")]
        public uint? Age {get; set;}
    }

The attribute CliArgName has an optional parameter string? name = default which is the name of the argument (in the example above is UserAge). Setting the argument name is not required if the property has the same name than the argument.

Parse the data

Once Cliargs initiliazation is completed without errors, you can start the parsing by calling the method GetArgsParsed<TObj>().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
MyArgs myArgs;

try {
    AppCliArgs.Initialize<CliArgsSetup>();
    // Parse the data into a give args object (MyArgs)
    myArgs = AppCliArgs.GetArgsParsed<MyArgs>();
}
catch(CliArgsException exception){
    Console.WriteLine($"Error: {exception.Message}");
    return;
} 

// Later you access to the property from everywhere 
Console.WriteLine($"User age: {myArgs.Age}.");