BadScript 2
Loading...
Searching...
No Matches
ParserSettings.cs
Go to the documentation of this file.
1// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
2
3using System;
4using System.Globalization;
5using System.IO;
6
8
9using CSharpx;
10
11namespace CommandLine
12{
16 public class ParserSettings : IDisposable
17 {
18 public const int DefaultMaximumLength = 80; // default console width
20 private bool autoHelp;
21 private bool autoVersion;
23 private bool caseSensitive;
24
25 private bool disposed;
27 private bool getoptMode;
28 private TextWriter helpWriter;
30 private CultureInfo parsingCulture;
32
36 public ParserSettings(int? maxDisplayWidth = null)
37 {
38 caseSensitive = true;
40 autoHelp = true;
41 autoVersion = true;
42 parsingCulture = CultureInfo.InvariantCulture;
43 MaximumDisplayWidth = maxDisplayWidth ?? GetWindowWidth();
44 getoptMode = false;
45 enableDashDash = Maybe.Nothing<bool>();
46 allowMultiInstance = Maybe.Nothing<bool>();
47 posixlyCorrect = Maybe.Nothing<bool>();
48 }
49
55 public bool CaseSensitive
56 {
57 get => caseSensitive;
58 set => PopsicleSetter.Set(Consumed, ref caseSensitive, value);
59 }
60
66 {
69 }
70
77 public CultureInfo ParsingCulture
78 {
79 get => parsingCulture;
80 set
81 {
82 if (value == null)
83 {
84 throw new ArgumentNullException("value");
85 }
86
87 PopsicleSetter.Set(Consumed, ref parsingCulture, value);
88 }
89 }
90
98 public TextWriter HelpWriter
99 {
100 get => helpWriter;
101 set => PopsicleSetter.Set(Consumed, ref helpWriter, value);
102 }
103
118 {
120 set => PopsicleSetter.Set(Consumed, ref ignoreUnknownArguments, value);
121 }
122
126 public bool AutoHelp
127 {
128 get => autoHelp;
129 set => PopsicleSetter.Set(Consumed, ref autoHelp, value);
130 }
131
135 public bool AutoVersion
136 {
137 get => autoVersion;
138 set => PopsicleSetter.Set(Consumed, ref autoVersion, value);
139 }
140
147 public bool EnableDashDash
148 {
149 get => enableDashDash.MatchJust(out bool value) ? value : getoptMode;
150 set => PopsicleSetter.Set(Consumed, ref enableDashDash, Maybe.Just(value));
151 }
152
156 public int MaximumDisplayWidth { get; set; }
157
164 {
165 get => allowMultiInstance.MatchJust(out bool value) ? value : getoptMode;
166 set => PopsicleSetter.Set(Consumed, ref allowMultiInstance, Maybe.Just(value));
167 }
168
173 public bool GetoptMode
174 {
175 get => getoptMode;
176 set => PopsicleSetter.Set(Consumed, ref getoptMode, value);
177 }
178
185 public bool PosixlyCorrect
186 {
187 get => posixlyCorrect.MapValueOrDefault(val => val,
188 () => Environment.GetEnvironmentVariable("POSIXLY_CORRECT")
189 .ToBooleanLoose()
190 );
191 set => PopsicleSetter.Set(Consumed, ref posixlyCorrect, Maybe.Just(value));
192 }
193
194 internal StringComparer NameComparer => CaseSensitive
195 ? StringComparer.Ordinal
196 : StringComparer.OrdinalIgnoreCase;
197
198 internal bool Consumed { get; set; }
199
200#region IDisposable Members
201
205 public void Dispose()
206 {
207 Dispose(true);
208
209 GC.SuppressFinalize(this);
210 }
211
212#endregion
213
214 public static ParserSettings CreateDefault(int? maxDisplayWidth = null)
215 {
216 return new ParserSettings(maxDisplayWidth);
217 }
218
219 private int GetWindowWidth()
220 {
221#if !NET40
222 if (Console.IsOutputRedirected)
223 {
225 }
226#endif
227 int width = 1;
228
229 try
230 {
231 width = Console.WindowWidth;
232
233 if (width < 1)
234 {
235 width = DefaultMaximumLength;
236 }
237 }
238 catch (Exception e) when (e is IOException ||
239 e is PlatformNotSupportedException ||
240 e is ArgumentOutOfRangeException)
241 {
242 width = DefaultMaximumLength;
243 }
244
245 return width;
246 }
247
252 {
253 Dispose(false);
254 }
255
256 private void Dispose(bool disposing)
257 {
258 if (disposed)
259 {
260 return;
261 }
262
263 if (disposing)
264 {
265 // Do not dispose HelpWriter. It is the caller's responsibility.
266
267 disposed = true;
268 }
269 }
270 }
271}
The Maybe type models an optional value. A value of type Maybe a either contains a value of type a (r...
Definition Maybe.cs:33
bool MatchJust(out T value)
Matches a value returning true and value itself via output parameter.
Definition Maybe.cs:49
Provides settings for CommandLine.Parser. Once consumed cannot be reused.
bool PosixlyCorrect
Whether getopt-like processing should follow the POSIX rules (the equivalent of using the "+" prefix ...
int MaximumDisplayWidth
Gets or sets the maximum width of the display. This determines word wrap when displaying the text.
bool EnableDashDash
Gets or sets a value indicating whether enable double dash '–' syntax, that forces parsing of all sub...
ParserSettings(int? maxDisplayWidth=null)
Initializes a new instance of the ParserSettings class.
bool AllowMultiInstance
Gets or sets a value indicating whether options are allowed to be specified multiple times....
bool CaseInsensitiveEnumValues
Gets or sets a value indicating whether perform case sensitive comparisons of values....
~ParserSettings()
Finalizes an instance of the CommandLine.ParserSettings class.
bool AutoHelp
Gets or sets a value indicating whether implicit option or verb 'help' should be supported.
void Dispose()
Frees resources owned by the instance.
bool IgnoreUnknownArguments
Gets or sets a value indicating whether the parser shall move on to the next argument and ignore the ...
CultureInfo ParsingCulture
Gets or sets the culture used when parsing arguments to typed properties.
bool GetoptMode
Whether strict getopt-like processing is applied to option values; if true, AllowMultiInstance and En...
bool CaseSensitive
Gets or sets a value indicating whether perform case sensitive comparisons. Note that case insensitiv...
void Dispose(bool disposing)
TextWriter HelpWriter
Gets or sets the System.IO.TextWriter used for help method output. Setting this property to null,...
static ParserSettings CreateDefault(int? maxDisplayWidth=null)
bool AutoVersion
Gets or sets a value indicating whether implicit option or verb 'version' should be supported.