NTP Analyzer  0.8.2
Analyze the operation of time servers
Ntp.Common.IO.Option Class Referenceabstract
Inheritance diagram for Ntp.Common.IO.Option:

Public Member Functions

string[] GetNames ()
 
string[] GetValueSeparators ()
 
void Invoke (OptionContext c)
 
override string ToString ()
 

Protected Member Functions

 Option (string prototype, string description)
 
 Option (string prototype, string description, int maxValueCount)
 
 Option (string prototype, string description, int maxValueCount, bool hidden)
 
abstract void OnParseComplete (OptionContext c)
 

Static Protected Member Functions

static T Parse< T > (string value, OptionContext c)
 

Properties

string Description [get]
 
bool Hidden [get]
 
int MaxValueCount [get]
 
string[] Names [get]
 
OptionValueType OptionValueType [get]
 
string Prototype [get]
 
string[] ValueSeparators [get, private set]
 

Private Member Functions

OptionValueType ParsePrototype ()
 

Static Private Member Functions

static void AddSeparators (string name, int end, ICollection< string > seps)
 

Static Private Attributes

static readonly char[] NameTerminator = {'=', ':'}
 

Detailed Description

Definition at line 450 of file Options.cs.

Constructor & Destructor Documentation

Ntp.Common.IO.Option.Option ( string  prototype,
string  description 
)
inlineprotected

Definition at line 452 of file Options.cs.

453  : this(prototype, description, 1, false)
454  {
455  }
Ntp.Common.IO.Option.Option ( string  prototype,
string  description,
int  maxValueCount 
)
inlineprotected

Definition at line 457 of file Options.cs.

458  : this(prototype, description, maxValueCount, false)
459  {
460  }
Ntp.Common.IO.Option.Option ( string  prototype,
string  description,
int  maxValueCount,
bool  hidden 
)
inlineprotected

Definition at line 463 of file Options.cs.

464  {
465  if (prototype == null)
466  throw new ArgumentNullException(nameof(prototype));
467  if (prototype.Length == 0)
468  throw new ArgumentException("Cannot be the empty string.", nameof(prototype));
469  if (maxValueCount < 0)
470  throw new ArgumentOutOfRangeException(nameof(maxValueCount));
471 
472  Prototype = prototype;
473  Description = description;
474  MaxValueCount = maxValueCount;
475  Names = this is OptionSet.Category
476  // append GetHashCode() so that "duplicate" categories have distinct
477  // names, e.g. adding multiple "" categories should be valid.
478  // ReSharper disable once VirtualMemberCallInConstructor
479  ? new[] {prototype + GetHashCode()}
480  : prototype.Split('|');
481 
482  if (this is OptionSet.Category)
483  return;
484 
486  Hidden = hidden;
487 
488  if (MaxValueCount == 0 && OptionValueType != OptionValueType.None)
489  throw new ArgumentException(
490  "Cannot provide maxValueCount of 0 for OptionValueType.Required or " +
491  "OptionValueType.Optional.",
492  nameof(maxValueCount));
493 
494  if (OptionValueType == OptionValueType.None && maxValueCount > 1)
495  throw new ArgumentException(
496  $"Cannot provide maxValueCount of {maxValueCount} for OptionValueType.None.",
497  nameof(maxValueCount));
498 
499  if (Array.IndexOf(Names, "<>") >= 0 &&
500  ((Names.Length == 1 && OptionValueType != OptionValueType.None) ||
501  (Names.Length > 1 && MaxValueCount > 1)))
502  throw new ArgumentException(
503  "The default option handler '<>' cannot require values.",
504  nameof(prototype));
505  }
OptionValueType ParsePrototype()
Definition: Options.cs:611
OptionValueType OptionValueType
Definition: Options.cs:513

Member Function Documentation

static void Ntp.Common.IO.Option.AddSeparators ( string  name,
int  end,
ICollection< string >  seps 
)
inlinestaticprivate

Definition at line 582 of file Options.cs.

583  {
584  var start = -1;
585  for (var i = end + 1; i < name.Length; ++i)
586  {
587  switch (name[i])
588  {
589  case '{':
590  if (start != -1)
591  throw new ArgumentException($"Ill-formed name/value separator found in \"{name}\".");
592  start = i + 1;
593  break;
594  case '}':
595  if (start == -1)
596  throw new ArgumentException($"Ill-formed name/value separator found in \"{name}\".");
597  seps.Add(name.Substring(start, i - start));
598  start = -1;
599  break;
600  default:
601  if (start == -1)
602  seps.Add(name[i].ToString());
603  break;
604  }
605  }
606 
607  if (start != -1)
608  throw new ArgumentException($"Ill-formed name/value separator found in \"{name}\".");
609  }
override string ToString()
Definition: Options.cs:543
string [] Ntp.Common.IO.Option.GetNames ( )
inline

Definition at line 523 of file Options.cs.

524  {
525  return (string[]) Names.Clone();
526  }
string [] Ntp.Common.IO.Option.GetValueSeparators ( )
inline

Definition at line 528 of file Options.cs.

529  {
530  if (ValueSeparators == null)
531  return new string[0];
532  return (string[]) ValueSeparators.Clone();
533  }
string[] ValueSeparators
Definition: Options.cs:521
void Ntp.Common.IO.Option.Invoke ( OptionContext  c)
inline

Definition at line 535 of file Options.cs.

References Ntp.Common.IO.OptionValueCollection.Clear(), Ntp.Common.IO.OptionContext.Option, Ntp.Common.IO.OptionContext.OptionName, and Ntp.Common.IO.OptionContext.OptionValues.

Referenced by Ntp.Common.IO.OptionSet.Invoke(), Ntp.Common.IO.OptionSet.Parse(), and Ntp.Common.IO.OptionSet.Unprocessed().

536  {
538  c.OptionName = null;
539  c.Option = null;
540  c.OptionValues.Clear();
541  }
abstract void OnParseComplete(OptionContext c)
var c
Definition: bootstrap.min.js:6

Here is the call graph for this function:

Here is the caller graph for this function:

static T Ntp.Common.IO.Option.Parse< T > ( string  value,
OptionContext  c 
)
inlinestaticprotected

Definition at line 550 of file Options.cs.

References e, Ntp.Common.IO.OptionSet.MessageLocalizer, Ntp.Common.IO.OptionContext.OptionName, and Ntp.Common.IO.OptionContext.OptionSet.

551  {
552  var tt = typeof(T);
553  var ti = tt;
554 
555  var nullable =
556  ti.IsValueType &&
557  ti.IsGenericType &&
558  !ti.IsGenericTypeDefinition &&
559  ti.GetGenericTypeDefinition() == typeof(Nullable<>);
560 
561  var targetType = nullable ? tt.GetGenericArguments()[0] : tt;
562  var t = default(T);
563  try
564  {
565  if (value != null)
566  {
567  var conv = TypeDescriptor.GetConverter(targetType);
568  t = (T) conv.ConvertFromString(value);
569  }
570  }
571  catch (Exception e)
572  {
573  throw new OptionException(
574  string.Format(
575  c.OptionSet.MessageLocalizer("Could not convert string `{0}' to type {1} for option `{2}'."),
576  value, targetType.Name, c.OptionName),
577  c.OptionName, e);
578  }
579  return t;
580  }
var e
Definition: bootstrap.min.js:6
var c
Definition: bootstrap.min.js:6
OptionValueType Ntp.Common.IO.Option.ParsePrototype ( )
inlineprivate

Definition at line 611 of file Options.cs.

612  {
613  var type = '\0';
614  var seps = new List<string>();
615 
616  for (var i = 0; i < Names.Length; ++i)
617  {
618  var name = Names[i];
619  if (name.Length == 0)
620  throw new ArgumentException("Empty option names are not supported.");
621 
622  var end = name.IndexOfAny(NameTerminator);
623  if (end == -1)
624  continue;
625  Names[i] = name.Substring(0, end);
626  if (type == '\0' || type == name[end])
627  type = name[end];
628  else
629  throw new ArgumentException($"Conflicting option types: '{type}' vs. '{name[end]}'.");
630  AddSeparators(name, end, seps);
631  }
632 
633  if (type == '\0')
634  return OptionValueType.None;
635 
636  if (MaxValueCount <= 1 && seps.Count != 0)
637  throw new ArgumentException(
638  $"Cannot provide key/value separators for Options taking {MaxValueCount} value(s).");
639 
640  if (MaxValueCount > 1)
641  {
642  if (seps.Count == 0)
643  ValueSeparators = new[] {":", "="};
644  else if (seps.Count == 1 && seps[0].Length == 0)
645  ValueSeparators = null;
646  else
647  ValueSeparators = seps.ToArray();
648  }
649 
650  return type == '=' ? OptionValueType.Required : OptionValueType.Optional;
651  }
static readonly char[] NameTerminator
Definition: Options.cs:507
OptionValueType OptionValueType
Definition: Options.cs:513
string[] ValueSeparators
Definition: Options.cs:521
static void AddSeparators(string name, int end, ICollection< string > seps)
Definition: Options.cs:582
override string Ntp.Common.IO.Option.ToString ( )
inline

Definition at line 543 of file Options.cs.

544  {
545  return Prototype;
546  }

Member Data Documentation

readonly char [] Ntp.Common.IO.Option.NameTerminator = {'=', ':'}
staticprivate

Definition at line 507 of file Options.cs.

Property Documentation

string Ntp.Common.IO.Option.Description
get

Definition at line 511 of file Options.cs.

Referenced by Ntp.Common.IO.OptionSet.WriteOptionPrototype().

bool Ntp.Common.IO.Option.Hidden
get

Definition at line 517 of file Options.cs.

int Ntp.Common.IO.Option.MaxValueCount
get

Definition at line 515 of file Options.cs.

Referenced by Ntp.Common.IO.OptionSet.WriteOptionPrototype().

string [] Ntp.Common.IO.Option.Names
getpackage
OptionValueType Ntp.Common.IO.Option.OptionValueType
get

Definition at line 513 of file Options.cs.

Referenced by Ntp.Common.IO.OptionSet.WriteOptionPrototype().

string Ntp.Common.IO.Option.Prototype
get

Definition at line 509 of file Options.cs.

string [] Ntp.Common.IO.Option.ValueSeparators
getprivate setpackage

Definition at line 521 of file Options.cs.

Referenced by Ntp.Common.IO.OptionSet.WriteOptionPrototype().


The documentation for this class was generated from the following file: