Sunday, 18 January 2009

CommandLine.dll 1.5: sneaky bug introduced in latests version(s) - here the patch!

As soon as possible I will publish the fixed version on CodePlex, for the ones that can't wait follows the patch.
Replace the content of {project-root}/Core/OptionMap.cs with the one here below.


namespace CommandLine
{
using System.Collections.Generic;

sealed class OptionMap : IOptionMap
{
private Dictionary<string, string> names;
private Dictionary<string, OptionInfo> map;

public OptionMap(int capacity)
{
this.names = new Dictionary<string, string>(capacity);
this.map = new Dictionary<string, OptionInfo>(capacity * 2);
}

public OptionInfo this[string key]
{
get
{
OptionInfo option = null;
if (this.map.ContainsKey(key))
{
option = this.map[key];
}
else
{
string optionKey = null;
if (this.names.ContainsKey(key))
{
optionKey = this.names[key];
option = this.map[optionKey];
}
}
return option;
}
set
{
this.map[key] = value;
if (value.HasBothNames)
{
this.names[value.LongName] = value.ShortName;
}
}
}

public bool EnforceRules()
{
foreach (OptionInfo option in this.map.Values)
{
if (option.Required && !option.IsDefined)
{
return false;
}
}
return true;
}
}
}


The bug causes the overloads of bool CommandLineParser.ParseArguments(...) to raise an exception, instead of returning false in case of undefined option.
In the next release I will explain how these kind of thing can happen and how to avoid them.

Bye!

2 comments:

  1. Thanks, for such a great post :)

    ReplyDelete
  2. Thanks to you, for your interest!

    ReplyDelete