public Argument(Argument argument, IEnumerable<string> optionStrings = null) : this(optionStrings ?? (argument != null ? argument.OptionStrings : null)) { if (ReferenceEquals(argument, null)) return; ActionFactory = argument.ActionFactory; ActionName = argument.ActionName; Choices = argument.Choices != null ? argument.Choices.ToList() : null; ConstValue = argument.ConstValue; DefaultValue = argument.DefaultValue; Destination = argument.Destination; HelpText = argument.HelpText; IsRemainder = argument.IsRemainder; IsRequired = argument.IsRequired; MetaVariable = argument.MetaVariable; SuppressDefaultValue = argument.SuppressDefaultValue; TypeFactory = argument.TypeFactory; Type = argument.Type; TypeName = argument.TypeName; ValueCount = argument.ValueCount; }
private Argument PrepareOptionalArgument(Argument argument) { var res = new Argument(argument, (argument.OptionStrings ?? new string[] {}).Where(StartsWithPrefix)); if (!res.OptionStrings.Any()) throw new Exception("Optional argument should have name starting with prefix"); // infer destination if (string.IsNullOrWhiteSpace(res.Destination) && res.OptionStrings.Any()) res.Destination = StripPrefix(res.OptionStrings.FirstOrDefault(StartsWithLongPrefix) ?? res.OptionStrings.FirstOrDefault()); if (string.IsNullOrWhiteSpace(res.Destination)) throw new ParserException("Destination should be specified for options like " + res.OptionStrings[0]); res.Destination = res.Destination.Replace('-', '_'); return res; }
private Argument PreparePositionalArgument(Argument argument) { var res = new Argument(argument, new string[] {}); // mark positional arguments as required if at least one is always required ValueCount valueCount = res.ValueCount; if (valueCount == null || valueCount.Min.HasValue && valueCount.Min > 0) res.IsRequired = true; else { if (valueCount.Max == 1) res.IsRequired = false; else if (ReferenceEquals(res.DefaultValue, null)) res.IsRequired = true; } if (string.IsNullOrEmpty(res.Destination) && !argument.OptionStrings.IsNullOrEmpty()) res.Destination = argument.OptionStrings.FirstOrDefault(); return res; }
private Action CreateAction(Argument argument) { Func<Argument, Action> argumentActionFactory = argument.ActionFactory ?? ActionFactories.SafeGetValue( !string.IsNullOrWhiteSpace(argument.ActionName) ? argument.ActionName : DefaultAction); return argumentActionFactory != null ? argumentActionFactory(argument) : null; }
private bool IsOptionalArgument(Argument argument) { bool ret = !argument.OptionStrings.IsNullOrEmpty() && (argument.OptionStrings.Count != 1 || StartsWithPrefix(argument.OptionStrings[0])); return ret; }
public Action AddArgument(Argument argument) { Argument preparedArgument = !IsOptionalArgument(argument) ? PreparePositionalArgument(argument) : PrepareOptionalArgument(argument); Action argumentAction = CreateAction(preparedArgument); if (ReferenceEquals(argumentAction, null)) throw new ParserException("Unregistered action exception"); return AddAction(argumentAction); }