Inside Command Line Argument Parser – Required Value
In this section, I’ll explain about defining Required command-line arguments members in command line argument object. You can specify required flag to any member of Command Line object.
Cinchoo framework will validate and throw an exception if a required command-line argument is missing.
1. Add a reference to Cinchoo.Core.dll
2. Namespace: Cinchoo.Core.Shell
In a sample Command Line object below,
public class MyCommandLineArgObject : ChoCommandLineArgObject
{
[ChoCommandLineArg("N", DefaultValue = 100, Description ="No of spaces for a TAB.")]
public int Count
{
get;
set;
}
[ChoCommandLineArg("I", DefaultValue = false, FallbackValue=true, Description = "Include Sub directory.")]
public bool IncludeSubDir
{
get;
set;
}
[ChoCommandLineArg("O", IsRequired = true, Description = "Output Directory.")]
public string OutputDir
{
get;
set;
}
}
If the application containing above type executed with the below command line arguments
/N:200 /I
Or
N:200 /I -O:
When creating instance of MyCommandLineArgObject, the framework automatically parses the command line arguments, sees there is no value passed for “O” argument and throws an exception with the below message
MyCommandLineArgObject commandLineArgObject = new MyCommandLineArgObject(); Console.WriteLine(commandLineArgObject.ToString());
In this sample, framework throws a ChoCommandLineArgException with the below message
Found exception while loading `O` command line argument.
Missing arg value for 'O' required command line switch.
Cinchoo.Core.CommandLineArgs.Test.exe -N:<int> -I:{True|False} -O:[<string>]
Where
-D Business Date.
-N No of spaces for a TAB.
-I Include Sub directory.
-O Output Directory.