Skip to main content

Digging through SignalR - Command Line Arguments

SignalR contains a console application that can be used to make stress tests. The solution that was used to parse the command parameters was ‘cmdline – Command Line Parser’. This is a pretty nice library that can be installed using NuGet. This command line parses works perfectly, this is why the last time when it was updated in in September 2012.
Beside this solution, you can find another solution on Codeplex and github called very similar “Command Line Parser Library”. Both solutions are great and resolve the same problem in a very similar way. First time when I saw this two solutions I could swear that are the same solution (from the available API).
I saw a lot of projects, where people started to implement their own arguments parsers, even if we have plenty of them on the marker. It is not so important what command line parser you use it, as long you use it and don’t rewrite it again and again. In general, the use cases that we need to support are pretty simple and are covered by almost all the current solutions.

Because I prefer Command Line Parser library, I will present this one. I’m a fan of it because this stack is available from 2009 and it is still updated and maintained. This is a key feature of any framework.
When you are using this stack you have the possibility to declare a class that will represent the parameters that are send by the user to you. Each input argument from the console application can be mapped to a property of the class. In this way, it will be pretty easily to check what arguments were introduce by the user, what is the value and so on.
This mapping between input arguments and your class property is made through attributes. There is a base attribute called ‘Option’ that is used in most common cases. For each attribute you have the ability to set a normal name of the option and also a short name. For example you can have an argument that has the full name ‘help’ and a short name called ‘?’ (for power user).
protected class MyFooOptions
{
  [Option("c", "copy", Required = true, HelpText = "Copy file.")]
  public bool Copy { get; set; }

  [Option("d", "delete", DefaultValue = false, HelpText = "Delete file.")]
  public bool Delete { get; set; };

  [Option(null, "input", HelpText = "Location of input file.")]
  public string InputFilePath { get; set; };
}
For each argument we have the ability to specify if the argument is optionality or required. When we set the ‘HelpText’ property we will have the ability to generate automatically the help legend for all arguments without needing to format it and so on. The only thing that we need to do is to use ‘HelpText’ class that can render automatically our help messages pretty nice.
[HelpOption]
public string GetHelp()
{
    HelpText help = new HelpText {
        Heading = new HeadingInfo("<>", "<>"),
        AdditionalNewLineAfterOption = true,
        AddDashesToOption = true };
    help.AddPreOptionsLine("<>");

    // Add our options.
    help.AddOptions(this);
    return help;
  }
To be able to parse the input arguments and map them to our class we need to call the following method:
MyFooOptions myOpt = new MyFooOptions();
ICommandLineParser parser = new CommandLineParser();
parser.ParseArguments(args, myOpt)
Magic.
Other features are supported like having a list of items for a specific option or specify a method that will be used when the user wants to access the help.
protected class MyFooOptions
{
  [Option("c", "copy", Required = true, HelpText = "Copy file.")]
  public bool Copy { get; set; }

  [Option("d", "delete", DefaultValue = false, HelpText = "Delete file.")]
  public bool Delete { get; set; };

  [Option(null, "input", HelpText = "Location of input file.")]
  public string InputFilePath { get; set; };

  [OptionList("inputlist", "inputlist", Separator = ',', HelpText = "List of files.")]
  public IList<string> InputList { get; set; };
}
The last thing that I like to this solution is ‘ValueListAttribute’. This attribute is useful when user start to enter or use arguments that are not mapped. In this case, we can have a property of type lList<string> where all this arguments will be added. We can process this list if we want or we can ignore it.
protected class MyFooOptions
{
  [Option("c", "copy", Required = true, HelpText = "Copy file.")]
  public bool Copy { get; set; }

  [Option("d", "delete", DefaultValue = false, HelpText = "Delete file.")]
  public bool Delete { get; set; };

  [Option(null, "input", HelpText = "Location of input file.")]
  public string InputFilePath { get; set; };

  [OptionList("inputlist", "inputlist", Separator = ',', HelpText = "List of files.")]
  public IList<string> InputList { get; set; };

  [ValueList(typeof(List<string>), MaximumElements = 10)]
  public IList<string> Items { get; set; };
}
Cases like missing required options are already managed by this solution. Of course you can set custom behavior to it.

Enjoy!

Comments

Popular posts from this blog

Windows Docker Containers can make WIN32 API calls, use COM and ASP.NET WebForms

After the last post , I received two interesting questions related to Docker and Windows. People were interested if we do Win32 API calls from a Docker container and if there is support for COM. WIN32 Support To test calls to WIN32 API, let’s try to populate SYSTEM_INFO class. [StructLayout(LayoutKind.Sequential)] public struct SYSTEM_INFO { public uint dwOemId; public uint dwPageSize; public uint lpMinimumApplicationAddress; public uint lpMaximumApplicationAddress; public uint dwActiveProcessorMask; public uint dwNumberOfProcessors; public uint dwProcessorType; public uint dwAllocationGranularity; public uint dwProcessorLevel; public uint dwProcessorRevision; } ... [DllImport("kernel32")] static extern void GetSystemInfo(ref SYSTEM_INFO pSI); ... SYSTEM_INFO pSI = new SYSTEM_INFO(

Azure AD and AWS Cognito side-by-side

In the last few weeks, I was involved in multiple opportunities on Microsoft Azure and Amazon, where we had to analyse AWS Cognito, Azure AD and other solutions that are available on the market. I decided to consolidate in one post all features and differences that I identified for both of them that we should need to take into account. Take into account that Azure AD is an identity and access management services well integrated with Microsoft stack. In comparison, AWS Cognito is just a user sign-up, sign-in and access control and nothing more. The focus is not on the main features, is more on small things that can make a difference when you want to decide where we want to store and manage our users.  This information might be useful in the future when we need to decide where we want to keep and manage our users.  Feature Azure AD (B2C, B2C) AWS Cognito Access token lifetime Default 1h – the value is configurable 1h – cannot be modified

What to do when you hit the throughput limits of Azure Storage (Blobs)

In this post we will talk about how we can detect when we hit a throughput limit of Azure Storage and what we can do in that moment. Context If we take a look on Scalability Targets of Azure Storage ( https://azure.microsoft.com/en-us/documentation/articles/storage-scalability-targets/ ) we will observe that the limits are prety high. But, based on our business logic we can end up at this limits. If you create a system that is hitted by a high number of device, you can hit easily the total number of requests rate that can be done on a Storage Account. This limits on Azure is 20.000 IOPS (entities or messages per second) where (and this is very important) the size of the request is 1KB. Normally, if you make a load tests where 20.000 clients will hit different blobs storages from the same Azure Storage Account, this limits can be reached. How we can detect this problem? From client, we can detect that this limits was reached based on the HTTP error code that is returned by HTTP