#region License // // Copyright 2015-2013 Giacomo Stelluti Scala // // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. #endregion #region Using Directives using System; using System.Globalization; using System.IO; using System.Threading; using CommandLine.Infrastructure; #endregion namespace CommandLine { /// /// Provides settings for . Once consumed cannot be reused. /// public sealed class ParserSettings { private const bool CaseSensitiveDefault = true; private bool _disposed; private bool _caseSensitive; private bool _mutuallyExclusive; private bool _ignoreUnknownArguments; private TextWriter _helpWriter; private CultureInfo _parsingCulture; /// /// Initializes a new instance of the class. /// public ParserSettings() : this(CaseSensitiveDefault, false, false, null) { } /// /// Initializes a new instance of the class, /// setting the case comparison behavior. /// /// If set to true, parsing will be case sensitive. public ParserSettings(bool caseSensitive) : this(caseSensitive, false, false, null) { } /// /// Initializes a new instance of the class, /// setting the used for help method output. /// /// Any instance derived from , /// default . Setting this argument to null, will disable help screen. public ParserSettings(TextWriter helpWriter) : this(CaseSensitiveDefault, false, false, helpWriter) { } /// /// Initializes a new instance of the class, /// setting case comparison and help output options. /// /// If set to true, parsing will be case sensitive. /// Any instance derived from , /// default . Setting this argument to null, will disable help screen. public ParserSettings(bool caseSensitive, TextWriter helpWriter) : this(caseSensitive, false, false, helpWriter) { } /// /// Initializes a new instance of the class, /// setting case comparison and mutually exclusive behaviors. /// /// If set to true, parsing will be case sensitive. /// If set to true, enable mutually exclusive behavior. public ParserSettings(bool caseSensitive, bool mutuallyExclusive) : this(caseSensitive, mutuallyExclusive, false, null) { } /// /// Initializes a new instance of the class, /// setting case comparison, mutually exclusive behavior and help output option. /// /// If set to true, parsing will be case sensitive. /// If set to true, enable mutually exclusive behavior. /// Any instance derived from , /// default . Setting this argument to null, will disable help screen. public ParserSettings(bool caseSensitive, bool mutuallyExclusive, TextWriter helpWriter) : this(caseSensitive, mutuallyExclusive, false, helpWriter) { } /// /// Initializes a new instance of the class, /// setting case comparison, mutually exclusive behavior and help output option. /// /// If set to true, parsing will be case sensitive. /// If set to true, enable mutually exclusive behavior. /// If set to true, allow the parser to skip unknown argument, otherwise return a parse failure /// Any instance derived from , /// default . Setting this argument to null, will disable help screen. public ParserSettings(bool caseSensitive, bool mutuallyExclusive, bool ignoreUnknownArguments, TextWriter helpWriter) { CaseSensitive = caseSensitive; MutuallyExclusive = mutuallyExclusive; HelpWriter = helpWriter; IgnoreUnknownArguments = ignoreUnknownArguments; ParsingCulture = Thread.CurrentThread.CurrentCulture; } /// /// Finalizes an instance of the class. /// ~ParserSettings() { Dispose(false); } /// /// Gets or sets a value indicating whether perform case sensitive comparisons. /// public bool CaseSensitive { get { return _caseSensitive; } set { PopsicleSetter.Set(Consumed, ref _caseSensitive, value); } } /// /// Gets or sets a value indicating whether set a mutually exclusive behavior. /// Default is set to false. /// public bool MutuallyExclusive { get { return _mutuallyExclusive; } set { PopsicleSetter.Set(Consumed, ref _mutuallyExclusive, value); } } /// /// Gets or sets the used for help method output. /// Setting this property to null, will disable help screen. /// public TextWriter HelpWriter { get { return _helpWriter; } set { PopsicleSetter.Set(Consumed, ref _helpWriter, value); } } /// /// Gets or sets a value indicating whether the parser shall move on to the next argument and ignore the given argument if it /// encounter an unknown arguments /// /// /// true to allow parsing the arguments with different class options that do not have all the arguments. /// /// /// This allows fragmented version class parsing, useful for project with add-on where add-ons also requires command line arguments but /// when these are unknown by the main program at build time. /// public bool IgnoreUnknownArguments { get { return _ignoreUnknownArguments; } set { PopsicleSetter.Set(Consumed, ref _ignoreUnknownArguments, value); } } /// /// Gets or sets the culture used when parsing arguments to typed properties. /// /// /// Default is CurrentCulture of . /// public CultureInfo ParsingCulture { get { return _parsingCulture; } set { PopsicleSetter.Set(Consumed, ref _parsingCulture, value); } } internal bool Consumed { get; set; } /// /// Frees resources owned by the instance. /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } private void Dispose(bool disposing) { if (_disposed) { return; } if (disposing) { if (_helpWriter != null) { _helpWriter.Dispose(); _helpWriter = null; } _disposed = true; } } } }