Introduction
Every administrator or developer who has worked with ADAM knows the command-line interface.
But did you know that ADAM provides you with an intuitive interface for creating your own command-line tool?
In this series of posts we will show you exactly what we provide.
We will do this by a simple example of a command-line tool that shows what your culture and language is.
Setting log listeners
ADAM provides you with a LogManager that allows you to write important information to both the command-line and the event log with one single method.
To enable the LogManager, you need to setup the app.config of your command-line.
Do this by creating a new App.config (if it doesn't already exists) in your command-line project.
Copy paste the content below into your App.config.
| XML |
1
2
3
4
5
6
7
8
9
10
|
<configSections>
<section name="Adam.Core" type="Adam.Core.ConfigurationHandler, Adam.Core, Version=4.5.0.0, Culture=neutral, PublicKeyToken=63f11f167f68d05b" />
<section name="Adam.Tools" type="Adam.Tools.ConfigurationHandler, Adam.Tools, Version=4.5.0.0, Culture=neutral, PublicKeyToken=63f11f167f68d05b" />
</configSections>
<Adam.Tools>
<logListeners>
<add type="Adam.Tools.LogHandler.ConsoleLogListener,Adam.Tools" severity="Normal" displayAdditionalInfo="false" displayStackTrace="false"/>
<add type="Adam.Tools.LogHandler.EventLogListener,Adam.Tools" severity="Normal"/>
</logListeners>
</Adam.Tools>
|
Writing the Main method
We start from a basic command-line structure.
| C# |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
using System;
using System.Reflection;
using Adam.Tools.CommandLine;
using Adam.Tools.LogHandler;
class Program
{
private static Arguments _Arguments;
static void Main(string[] args)
{
int returnCode = 0;
try
{
// Making sure that we use a proper event log source...
EventLogListener.Source = "Blog " +
Assembly.GetExecutingAssembly().GetName().Version.ToString(2) + " CommandLine";
// Commands will come here
}
catch (Exception ex)
{
LogManager.Write(LogSeverity.Error, "The specified operation failed:", ex);
returnCode = 1;
}
Environment.Exit(returnCode);
}
}
|
Notice how we put a try/catch around the code that will be used to execute the command.
We do this so we can write the unexpected exception to the event log and the command-line.
Introducing commands
When you run ADAM command-line tools, you always have a choice of multiple commands you can execute.
You can create your own commands by using the Command class in Adam.Tools.CommandLine.
Copy the lines below to your new project and paste it right below "// Commands will come here".
| C# |
1
2
3
4
|
Command[] commands = new[]
{
new Command("WhoAmI", ExecuteWhoAmI, ShowHelpWhoAmI),
};
|
The constructor of Command takes three parameters:
- name: the string that will be used when calling the command-line from the prompt
- executeCommand: the delegate that will be called when the command should be executed
- displayHelp: the delegate that will be called when the help of the command needs to be shown
The code to execute commands
Now that you have created one command that is named "WhoAmI", the command-line still doesn't know how to execute the command.
We need to introduce something that will read the arguments that have been passed and link the arguments to the right command.
Obviously, ADAM has provided some methods to do this.
Just put the code below beneath the commands array.
| C# |
1
2
|
_Arguments = Arguments.Parse(args);
_Arguments.ExecuteCommand(commands, DisplayGeneralHelp);
|
Implementing the Install command
For sample purposes we will add some dummy code into the install command so you can try it out.
| C# |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
private static void ShowHelpWhoAmI(string command, Arguments args)
{
Console.WriteLine("");
Console.WriteLine("Some help with command '{0}'", command);
Console.WriteLine("");
}
private static void ExecuteWhoAmI(string command, Arguments args)
{
Console.WriteLine("");
LogManager.Write(LogSeverity.Normal, "Starting WhoAmI...");
Thread.Sleep(1000);
LogManager.Write(LogSeverity.Normal, "Finished WhoAmI");
Console.WriteLine("");
}
private static void DisplayGeneralHelp(Arguments args, string[] commands)
{
Console.WriteLine("");
Console.WriteLine("Some general command-line help.");
Console.WriteLine("Below you can find the available commands:");
foreach (string command in commands)
{
Console.WriteLine(command);
}
Console.WriteLine("");
}
|
Trying it out
Open a command prompt as Administrator and try the following commands:
Adam.Blog.CommandLine.exe
Adam.Blog.CommandLine.exe -?
Adam.Blog.CommandLine.exe WhoAmI -?
Adam.Blog.CommandLine.exe WhoAmI
If you should run in to an exception that tells you that you can't write to the event log, close the command prompt and open it as Administrator.
What's next
We've only created a command-line tool, but it doesn't really do anything.
In a next post we will be showing you what ADAM has provided for using arguments and displaying help.
Sample Code
The article contains sample code project(s).
You must be logged in to view or download sample code.
Sign in now