Your own command-line: Copyright and confirmation

Previously

In our previous posts we succeeded in creating a command-line tool, supporting and using arguments and showing help. We are now going to provide the finishing touch to our command-line tool by providing copyright info and asking the user for confirmation.

Copyright info

In Visual Studio, you can add copyright information to every project. You can do this by going to the properties of your project and then click the "Assembly Information..." in the "Application" tab. Wouldn't it be nice if you could use that information in your command-line tool. You've guessed it, ADAM provides an API call to use this. Simply add the following line at the very top of your Main method of the Program class:

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
static void Main(string[] args)
{
 ConsoleHelper.WriteCopyright();
 int returnCode = 0;
 
 try
 {
  // Making sure that we use a proper event log source...
  EventLogListener.Source = "Blog " +
   Assembly.GetExecutingAssembly().GetName().Version.ToString(2) + " CommandLine";

  Command[] commands = new[]
    {
     new Command("WhoAmI", ExecuteWhoAmI, ShowHelpWhoAmI), 
    };

  _Arguments = Arguments.Parse(args);
  _Arguments.ExecuteCommand(commands, DisplayGeneralHelp);
 }
 catch (Exception ex)
 {
  LogManager.Write(LogSeverity.Error, "The specified operation failed:", ex);
  returnCode = 1;
 }
 
 Environment.Exit(returnCode);
}

Ask for confirmation

Some tasks you allow in your command-line tool might not be revertible. For those tasks it might be useful to ask for the user's explicit confirmation that the task can be executed. Without ADAM you would:

  • Write a message to the console
  • Use Console.ReadLine to read user input
  • Validate the user input to Y or N
  • If the user input wasn't Y or N you would execute the steps above again
  • If the user entered Y you would finally execute
With ADAM you would write one statement to get the user input to a boolean value and one if statement to determine whether to proceed or not. No need to validate the input or to write loops or ... As an example we will ask the user for confirmation before (s)he can execute WhoAmI.

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
private static void ExecuteWhoAmI(string command, Arguments args)
{
 bool userConfirmed = ConsoleHelper.AskYesNo("Are you sure you wish to continue?");
 if (!userConfirmed)
 {
  return;
 }
 Console.WriteLine();
 LogManager.Write(LogSeverity.Normal, "Starting WhoAmI...");
 Application application = LogonToAdam(args);
 ConsoleHelper.WriteParagraph(string.Format("Your username is: {0}", application.UserName));
 ConsoleHelper.WriteParagraph(string.Format("Your culture is: {0}", application.Culture));
 ConsoleHelper.WriteParagraph(string.Format("Your language is: {0}", application.LanguageName));
 ConsoleHelper.WriteParagraph(string.Format("Your UI language is: {0}", application.LanguageNameForUI));
 Thread.Sleep(1000);
 LogManager.Write(LogSeverity.Normal, "Finished WhoAmI");
 Console.WriteLine();
}

More options then Y or N

When you need more then just a yes-no question, you can use the Ask method instead of the AskYesNo method. The Ask method allows you to pass the possible options and they will be displayed to the user.

Conclusion

ADAM provides an intuitive API for creating command-line tools. By providing you with this API, we allow you to create intuitive applications. If you are interested in the final version of the application, you can find it attached to this post.

Sample Code

The article contains sample code project(s).
You must be logged in to view or download sample code.
Sign in now

Comments

Leave a comment
You must be logged in to post comments.
Sign in now
 
 
Technical
Business
rss feed