Skip to content

Support git-style "--" argument behaviour #77

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

Closed
wants to merge 1 commit into from
Closed
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
14 changes: 12 additions & 2 deletions src/libcmdline/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,12 @@ private bool DoParseArguments(string[] args, object options)

private bool DoParseArgumentsCore(string[] args, object options)
{
var hadNonOption = false;
var hadError = false;
var optionMap = OptionMap.Create(options, _settings);
optionMap.SetDefaults();
var valueMapper = new ValueMapper(options, _settings.ParsingCulture);

var arguments = new StringArrayEnumerator(args);
while (arguments.MoveNext())
{
Expand All @@ -364,7 +365,13 @@ private bool DoParseArgumentsCore(string[] args, object options)
continue;
}

var parser = ArgumentParser.Create(argument, _settings.IgnoreUnknownArguments);
if (_settings.EnableDashDash && !hadNonOption && argument == "--")
{
hadNonOption = true;
continue;
}

var parser = hadNonOption ? null : ArgumentParser.Create(argument, _settings.IgnoreUnknownArguments);
if (parser != null)
{
var result = parser.Parse(arguments, optionMap, options);
Expand All @@ -386,6 +393,9 @@ private bool DoParseArgumentsCore(string[] args, object options)
{
hadError = true;
}

if (_settings.DoNotInterpretAfterFirstNonOption)
hadNonOption = true;
}
}

Expand Down
35 changes: 35 additions & 0 deletions src/libcmdline/ParserSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public sealed class ParserSettings
private bool _ignoreUnknownArguments;
private TextWriter _helpWriter;
private CultureInfo _parsingCulture;
private bool _enableDashDash;
private bool _doNotInterpretAfterFirstNonOption;

/// <summary>
/// Initializes a new instance of the <see cref="ParserSettings"/> class.
Expand Down Expand Up @@ -209,6 +211,39 @@ public bool IgnoreUnknownArguments
}
}

/// <summary>
/// Gets or sets a value indicating whether the "--" argument should be interpreted. If enabled,
/// the -- argument indicates that any following arguments should not be interpreted as options.
/// </summary>
public bool EnableDashDash
{
get
{
return _enableDashDash;
}

set
{
PopsicleSetter.Set(Consumed, ref _enableDashDash, value);
}
}

/// <summary>
/// Gets or sets a value indicating whether arguments following the first non-option argument should be interpreted as options.
/// </summary>
public bool DoNotInterpretAfterFirstNonOption
{
get
{
return _doNotInterpretAfterFirstNonOption;
}

set
{
PopsicleSetter.Set(Consumed, ref _doNotInterpretAfterFirstNonOption, value);
}
}

/// <summary>
/// Gets or sets the culture used when parsing arguments to typed properties.
/// </summary>
Expand Down