While creating command line exe we need to provide detail of options to end user.
This options detail can be provided by using the simple utility class called "NDesk.Options ".
Click on NDesk.Options link to download the utility.
This utility is a simple class that can be added to our project directly or can be added as a reference.
Let's see how we can use the same in our command line project.
Further lets see how we can use exe with parameters we had just created.
Open the cmd prompt and goto the directory path were exe is build, In my case it was inside debug folder of bin.
Now using this exe with options is shown below
Lastly to show help use it as shown below
Hope it helps someone :)
This options detail can be provided by using the simple utility class called "NDesk.Options ".
Click on NDesk.Options link to download the utility.
This utility is a simple class that can be added to our project directly or can be added as a reference.
Let's see how we can use the same in our command line project.
- Open VS
- Create new Console Application
- Add the reference of NDesk.Options dll to the project.
using System;
using NDesk.Options;
namespace NDeskSample
{
class Program
{
//Variables declared
private static int FirstNum=0;
private static int SecondNum=0;
static void Main(string[] args)
{
var show_help=false;
//Intializing utility class with options
OptionSet options = new OptionSet(){
{"F|FirstNum=","Value for First Number to add",(int v)=>FirstNum=v},
{"S|SecondNum=","Value for Second Number to add",(int v)=>SecondNum=v},
{"h|help","Show Help",v=> show_help=v!=null},
};
try
{
options.Parse(args); //Parsing the input arguments
}
catch (OptionException e)
{
Console.WriteLine(e.Message);
Console.WriteLine("Try --help for more information.");
ShowHelp(options);
return;
}
if (FirstNum == 0 || SecondNum == 0)
{
ShowHelp(options);
return;
}
else
{
Console.WriteLine("{0} is the sum of given numbers",sum(FirstNum, SecondNum));
}
if (show_help)
{
ShowHelp(options);
return;
}
}
static int sum(int firstNo, int secondNo)
{
return firstNo + secondNo;
}
//Displays the help for the options
static void ShowHelp(OptionSet p)
{
Console.WriteLine("NDesk Usage:");
Console.WriteLine("Options:");
p.WriteOptionDescriptions(Console.Out);
}
}
}
using NDesk.Options;
namespace NDeskSample
{
class Program
{
//Variables declared
private static int FirstNum=0;
private static int SecondNum=0;
static void Main(string[] args)
{
var show_help=false;
//Intializing utility class with options
OptionSet options = new OptionSet(){
{"F|FirstNum=","Value for First Number to add",(int v)=>FirstNum=v},
{"S|SecondNum=","Value for Second Number to add",(int v)=>SecondNum=v},
{"h|help","Show Help",v=> show_help=v!=null},
};
try
{
options.Parse(args); //Parsing the input arguments
}
catch (OptionException e)
{
Console.WriteLine(e.Message);
Console.WriteLine("Try --help for more information.");
ShowHelp(options);
return;
}
if (FirstNum == 0 || SecondNum == 0)
{
ShowHelp(options);
return;
}
else
{
Console.WriteLine("{0} is the sum of given numbers",sum(FirstNum, SecondNum));
}
if (show_help)
{
ShowHelp(options);
return;
}
}
static int sum(int firstNo, int secondNo)
{
return firstNo + secondNo;
}
//Displays the help for the options
static void ShowHelp(OptionSet p)
{
Console.WriteLine("NDesk Usage:");
Console.WriteLine("Options:");
p.WriteOptionDescriptions(Console.Out);
}
}
}
Further lets see how we can use exe with parameters we had just created.
Open the cmd prompt and goto the directory path were exe is build, In my case it was inside debug folder of bin.
Now using this exe with options is shown below
Lastly to show help use it as shown below
Hope it helps someone :)