From 01f854a0fa0a3cd5a4d0e4b2c13e14a9011259e3 Mon Sep 17 00:00:00 2001 From: Tom Glastonbury Date: Wed, 10 Apr 2013 16:01:25 +0100 Subject: [PATCH] Added support for "--" argument via ParserSettings.EnableDashDash, behaves as it does for many git commands. Added support for implicit "--" behaviour via ParserSettings.DoNotInterpretAfterFirstNonOption. --- src/libcmdline/Parser.cs | 14 +++++++++++-- src/libcmdline/ParserSettings.cs | 35 ++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/libcmdline/Parser.cs b/src/libcmdline/Parser.cs index 5d053847..829bd772 100644 --- a/src/libcmdline/Parser.cs +++ b/src/libcmdline/Parser.cs @@ -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()) { @@ -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); @@ -386,6 +393,9 @@ private bool DoParseArgumentsCore(string[] args, object options) { hadError = true; } + + if (_settings.DoNotInterpretAfterFirstNonOption) + hadNonOption = true; } } diff --git a/src/libcmdline/ParserSettings.cs b/src/libcmdline/ParserSettings.cs index 936645f0..a31c5c07 100644 --- a/src/libcmdline/ParserSettings.cs +++ b/src/libcmdline/ParserSettings.cs @@ -43,6 +43,8 @@ public sealed class ParserSettings private bool _ignoreUnknownArguments; private TextWriter _helpWriter; private CultureInfo _parsingCulture; + private bool _enableDashDash; + private bool _doNotInterpretAfterFirstNonOption; /// /// Initializes a new instance of the class. @@ -209,6 +211,39 @@ public bool IgnoreUnknownArguments } } + /// + /// 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. + /// + public bool EnableDashDash + { + get + { + return _enableDashDash; + } + + set + { + PopsicleSetter.Set(Consumed, ref _enableDashDash, value); + } + } + + /// + /// Gets or sets a value indicating whether arguments following the first non-option argument should be interpreted as options. + /// + public bool DoNotInterpretAfterFirstNonOption + { + get + { + return _doNotInterpretAfterFirstNonOption; + } + + set + { + PopsicleSetter.Set(Consumed, ref _doNotInterpretAfterFirstNonOption, value); + } + } + /// /// Gets or sets the culture used when parsing arguments to typed properties. ///