April 9, 2012

Command line parser like stsadmin in sharepoint console app


In our recent console application we decided to provide sharepoint Consultant administrators similar experience like stsadmin while providing custom features for administration and analysis. So we used an open source command line parser (codeplex) in order to implement command line parsing.

It has been proven a good choice. Here is how we can easily use it.

Source code:

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Text;
   4:  using CommandLine;
   5:  using CommandLine.Text;
   6:  using Microsoft.SharePoint;
   7:  using Microsoft.SharePoint.WebPartPages;
   8:   
   9:   
  10:  namespace SPAdmin
  11:  {
  12:      class Program
  13:      {
  14:          static Options options = new Options();
  15:          static StringBuilder fileOutput;
  16:          static void Main(string[] args)
  17:          {
  18:              ICommandLineParser parser = new CommandLineParser(new CommandLineParserSettings(Console.Error));
  19:              if (!parser.ParseArguments(args, options))
  20:                  Environment.Exit(1);
  21:   
  22:              if (string.IsNullOrEmpty(options.operation))
  23:              {
  24:                  Console.WriteLine(options.GetUsage());
  25:                  Console.WriteLine("Press any key to exit...");
  26:                  Console.ReadKey();
  27:                  Environment.Exit(1);
  28:              }
  29:   
  30:              switch (options.operation.ToLower())
  31:              {
  32:                  case "Operation1":
  33:                      DoOperation1();
  34:                      break;
  35:                  case "Operation2":
  36:                      DoOperation2();
  37:                      break;
  38:              }
  39:   
  40:              Console.WriteLine("Press any key to exit");
  41:              Console.ReadKey();
  42:          }
  43:          private static void DoOperation1()
  44:          {
  45:             
  46:          }
  47:          private static void DoOperation1()
  48:          {
  49:             
  50:          }
  51:          private sealed class Options
  52:          {
  53:              [CommandLine.Option("o", "operation",
  54:                  Required = true,
  55:                  HelpText = "Name of operation\n ")]
  56:              public string operation;
  57:            
  58:   
  59:              [HelpOption(HelpText = "Dispaly this help screen.")]
  60:              public string GetUsage()
  61:              {
  62:                  var help = new HelpText("Test");
  63:                  help.AddOptions(this);
  64:                  return help;
  65:              }
  66:          }
  67:   
  68:      }
  69:   
  70:   
  71:  }

Details:
1. Create a new console app. (If you have CSK templates installed, you can create new sharepoint console app project.)
2. For defining options, add options class as an inner class to your Program.cs
3. In main method, parse the passed parameters.
4. try it with SPAdmin -o "operation1" and see what happens
Add options as you need! Nice and clean way to implement various operations.

If you have any questions you can reach out our SharePoint Consulting team here.

No comments:

Post a Comment