Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancements to ArgvConfigSource #8

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions Source/Config/ArgvConfigSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using Nini.Util;

namespace Nini.Config
Expand All @@ -22,13 +23,14 @@ public class ArgvConfigSource : ConfigSourceBase
#region Private variables
ArgvParser parser = null;
string[] arguments = null;
List<string> parameterlessArgs = new List<string>();
#endregion

#region Constructors
/// <include file='ArgvConfigSource.xml' path='//Constructor[@name="Constructor"]/docs/*' />
public ArgvConfigSource (string[] arguments)
{
parser = new ArgvParser (arguments);
parser = new ArgvParser (arguments, parameterlessArgs);
this.arguments = arguments;
}
#endregion
Expand Down Expand Up @@ -57,7 +59,7 @@ public void AddSwitch (string configName, string longName)

/// <include file='ArgvConfigSource.xml' path='//Method[@name="AddSwitchShort"]/docs/*' />
public void AddSwitch (string configName, string longName,
string shortName)
string shortName, bool parameterless = false)
{
IConfig config = GetConfig (configName);

Expand All @@ -66,6 +68,17 @@ public void AddSwitch (string configName, string longName,
throw new ArgumentException ("Short name may only be 1 or 2 characters");
}

if (parameterless)
{
parameterlessArgs.Add(longName);
if (shortName != null)
parameterlessArgs.Add(shortName);

// Re-parse because there is a new parameterless argument. This will change
// the meaning of non-option args
parser = new ArgvParser (arguments, parameterlessArgs);
}

// Look for the long name first
if (parser[longName] != null) {
config.Set (longName, parser[longName]);
Expand All @@ -82,6 +95,16 @@ public string[] GetArguments ()

return result;
}

public int GetPositionalArgumentCount()
{
return parser.Count();
}

public string GetPositionalArgument(int index)
{
return parser[index];
}
#endregion

#region Private methods
Expand All @@ -103,4 +126,4 @@ private IConfig GetConfig (string name)
}
#endregion
}
}
}
33 changes: 32 additions & 1 deletion Source/Util/ArgvParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text.RegularExpressions;

Expand All @@ -22,6 +23,8 @@ public class ArgvParser
{
#region Private variables
StringDictionary parameters;
List<string> positional;
List<string> parameterlessArgs;
#endregion

#region Constructors
Expand All @@ -43,11 +46,14 @@ public ArgvParser(string args)
{
parts[i-1] = matches[i].Value.Trim ();
}

Extract(parts);
}

/// <include file='ArgvParser.xml' path='//Constructor[@name="ConstructorArray"]/docs/*' />
public ArgvParser (string[] args)
public ArgvParser (string[] args, List<string> paramlessArgs)
{
parameterlessArgs = paramlessArgs;
Extract (args);
}
#endregion
Expand All @@ -60,13 +66,29 @@ public string this [string param]
return parameters[param];
}
}

public string this [int number]
{
get {
if (number < 0 || number >= positional.Count)
return String.Empty;

return positional[number];
}
}

public int Count()
{
return positional.Count;
}
#endregion

#region Private methods
// Extract command line parameters and values stored in a string array
private void Extract(string[] args)
{
parameters = new StringDictionary();
positional = new List<string>();
Regex splitter = new Regex (@"^([/-]|--){1}(?<name>\w+)([:=])?(?<value>.+)?$",
RegexOptions.Compiled);
char[] trimChars = {'"','\''};
Expand All @@ -83,12 +105,21 @@ private void Extract(string[] args)
// Found a value (for the last parameter found (space separator))
if (parameter != null) {
parameters[parameter] = arg.Trim (trimChars);
parameter = null;
} else {
positional.Add(arg.Trim(trimChars));
}
} else {
// Matched a name, optionally with inline value
parameter = part.Groups["name"].Value;
parameters.Add (parameter,
part.Groups["value"].Value.Trim (trimChars));
if (parameterlessArgs != null && parameterlessArgs.Contains(parameter))
{
// Make it true and don't look for an argument
parameters[parameter] = "True";
parameter = null;
}
}
}
}
Expand Down