Exemple #1
0
 public CommaJoinedOption(
     OptSpecifier id,
     IReadOnlyList <string> prefixes,
     string name,
     string helpText      = null,
     OptSpecifier?aliasId = null,
     OptSpecifier?groupId = null,
     string metaVar       = null)
     : base(id, prefixes, name, helpText: helpText, metaVar: metaVar,
            aliasId: aliasId, groupId: groupId)
 {
     if (!id.IsValid)
     {
         throw new ArgumentException("Invalid id");
     }
     if (prefixes == null)
     {
         throw new ArgumentNullException(nameof(prefixes));
     }
     if (prefixes.Count == 0)
     {
         throw new ArgumentException("At least one prefix must be specified");
     }
     if (prefixes.Any(string.IsNullOrWhiteSpace))
     {
         throw new ArgumentException("All prefixes must be non-empty");
     }
     if (name == null)
     {
         throw new ArgumentNullException(nameof(name));
     }
 }
Exemple #2
0
 public MultiArgOption(
     OptSpecifier id,
     string prefix,
     string name,
     int argCount,
     string helpText      = null,
     OptSpecifier?aliasId = null,
     OptSpecifier?groupId = null,
     string metaVar       = null)
     : base(id, prefix, name, helpText: helpText, metaVar: metaVar,
            aliasId: aliasId, groupId: groupId)
 {
     if (!id.IsValid)
     {
         throw new ArgumentException("Invalid id");
     }
     if (prefix == null)
     {
         throw new ArgumentNullException(nameof(prefix));
     }
     if (string.IsNullOrWhiteSpace(prefix))
     {
         throw new ArgumentException("Contract violated: !string.IsNullOrWhiteSpace(prefix)");
     }
     if (name == null)
     {
         throw new ArgumentNullException(nameof(name));
     }
     if (argCount <= 0)
     {
         throw new ArgumentOutOfRangeException(nameof(argCount), "Contract violated: argCount > 0");
     }
     ArgCount = argCount;
 }
Exemple #3
0
 public RemainingArgsOption(
     OptSpecifier id,
     string prefix,
     string name,
     string helpText      = null,
     OptSpecifier?aliasId = null,
     OptSpecifier?groupId = null,
     string metaVar       = null)
     : base(id, prefix, name, helpText: helpText, metaVar: metaVar,
            aliasId: aliasId, groupId: groupId)
 {
     if (!id.IsValid)
     {
         throw new ArgumentException("Invalid id");
     }
     if (prefix == null)
     {
         throw new ArgumentNullException(nameof(prefix));
     }
     if (string.IsNullOrWhiteSpace(prefix))
     {
         throw new ArgumentException("Contract violated: !string.IsNullOrWhiteSpace(prefix)");
     }
     if (name == null)
     {
         throw new ArgumentNullException(nameof(name));
     }
 }
Exemple #4
0
        internal Option(
            OptSpecifier id,
            string name,
            OptSpecifier?aliasId = null,
            OptSpecifier?groupId = null,
            string helpText      = null,
            string metaVar       = null)
        {
            if (!id.IsValid)
            {
                throw new ArgumentException("Invalid id");
            }
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            Id           = id.Id;
            prefixes     = new string[0];
            this.name    = name;
            this.aliasId = aliasId.GetValueOrDefault().Id;
            this.groupId = groupId.GetValueOrDefault().Id;
            HelpText     = helpText;
            MetaVar      = metaVar;
        }
Exemple #5
0
 public CommaJoinedOption(
     OptSpecifier id,
     string prefix,
     string name,
     string helpText      = null,
     OptSpecifier?aliasId = null,
     OptSpecifier?groupId = null,
     string metaVar       = null)
     : base(id, prefix, name, helpText: helpText, metaVar: metaVar,
            aliasId: aliasId, groupId: groupId)
 {
     if (!id.IsValid)
     {
         throw new ArgumentException("Invalid id");
     }
     if (prefix == null)
     {
         throw new ArgumentNullException(nameof(prefix));
     }
     if (string.IsNullOrWhiteSpace(prefix))
     {
         throw new ArgumentException("Prefix must not be empty.");
     }
     if (name == null)
     {
         throw new ArgumentNullException(nameof(name));
     }
 }
Exemple #6
0
 public JoinedAndSeparateOption(
     OptSpecifier id,
     IReadOnlyList <string> prefixes,
     string name,
     string helpText      = null,
     OptSpecifier?aliasId = null,
     OptSpecifier?groupId = null,
     string metaVar       = null)
     : base(id, prefixes, name, helpText: helpText, metaVar: metaVar,
            aliasId: aliasId, groupId: groupId)
 {
     if (!id.IsValid)
     {
         throw new ArgumentException("Invalid id");
     }
     if (prefixes == null)
     {
         throw new ArgumentNullException(nameof(prefixes));
     }
     if (prefixes.Count == 0)
     {
         throw new ArgumentException("Contract violated: prefixes.Count != 0");
     }
     if (prefixes.Any(string.IsNullOrWhiteSpace))
     {
         throw new ArgumentException("Contract violated: !string.IsNullOrWhiteSpace(prefix)");
     }
     if (name == null)
     {
         throw new ArgumentNullException(nameof(name));
     }
 }
Exemple #7
0
        public static OptTableBuilder AddUnknown(this OptTableBuilder builder, OptSpecifier id)
        {
            if (!id.IsValid)
            {
                throw new ArgumentException("Invalid id");
            }

            builder.Add(new UnknownOption(id.Id));
            return(builder);
        }
Exemple #8
0
        public string GetLastArgValue(OptSpecifier id, string defaultValue = null)
        {
            Arg lastArg = GetLastArg(id);

            if (lastArg == null)
            {
                return(defaultValue);
            }
            return(lastArg.Value);
        }
Exemple #9
0
        public bool GetFlag(OptSpecifier id, bool defaultValue = false)
        {
            Arg arg = GetLastArg(id);

            if (arg != null)
            {
                return(arg.Option.Matches(id));
            }
            return(defaultValue);
        }
Exemple #10
0
 public void ClaimAllArgs(OptSpecifier id)
 {
     foreach (var arg in args)
     {
         if (arg.Option.Matches(id))
         {
             arg.Claim();
         }
     }
 }
Exemple #11
0
        public bool GetFlagNoClaim(
            OptSpecifier positiveId, OptSpecifier negativeId, bool defaultValue = false)
        {
            Arg arg = GetLastArgNoClaim(positiveId, negativeId);

            if (arg != null)
            {
                return(arg.Option.Matches(positiveId));
            }
            return(defaultValue);
        }
Exemple #12
0
 public Arg GetLastArgNoClaim(OptSpecifier id)
 {
     for (int i = args.Count - 1; i >= 0; --i)
     {
         var arg = args[i];
         if (arg.Option.Matches(id))
         {
             return(arg);
         }
     }
     return(null);
 }
Exemple #13
0
 /// <summary>
 ///   Gets the <see cref="Option"/> for the specified id.
 /// </summary>
 /// <param name="id">
 ///   The id of the option to get.
 /// </param>
 /// <returns>
 ///   The specified <see cref="Option"/> or <see langword="null"/> if no
 ///   such option exists.
 /// </returns>
 public Option GetOption(OptSpecifier id)
 {
     if (!id.IsValid)
     {
         return(null);
     }
     if (!optionMap.TryGetValue(id.Id, out var opt))
     {
         return(null);
     }
     return(opt);
 }
Exemple #14
0
        public Arg GetLastArg(OptSpecifier id)
        {
            Arg lastArg = null;

            foreach (var arg in args)
            {
                if (arg.Option.Matches(id))
                {
                    arg.Claim();
                    lastArg = arg;
                }
            }
            return(lastArg);
        }
Exemple #15
0
        public IList <string> GetAllArgValues(OptSpecifier id)
        {
            var allValues = new List <string>();

            foreach (var arg in args)
            {
                if (arg.Option.Matches(id))
                {
                    arg.Claim();
                    allValues.AddRange(arg.Values);
                }
            }
            return(allValues);
        }
Exemple #16
0
 public UnknownOption(
     OptSpecifier id,
     string helpText      = null,
     OptSpecifier?aliasId = null,
     OptSpecifier?groupId = null,
     string metaVar       = null)
     : base(id, "<unknown>", helpText: helpText, metaVar: metaVar,
            aliasId: aliasId, groupId: groupId)
 {
     if (!id.IsValid)
     {
         throw new ArgumentException("Invalid id");
     }
     Kind = OptionKind.Unknown;
 }
Exemple #17
0
        public static OptTableBuilder AddGroup(
            this OptTableBuilder builder,
            OptSpecifier id,
            string name,
            string helpText = null,
            string metaVar  = null)
        {
            var option = new GroupOption(
                id.Id,
                name,
                helpText: helpText,
                metaVar: metaVar);

            builder.Add(option);
            return(builder);
        }
Exemple #18
0
 public GroupOption(
     OptSpecifier id,
     string name,
     string helpText = null,
     string metaVar  = null)
     : base(id, name, helpText: helpText, metaVar: metaVar)
 {
     if (!id.IsValid)
     {
         throw new ArgumentException("Invalid id");
     }
     if (name == null)
     {
         throw new ArgumentNullException(nameof(name));
     }
     Kind = OptionKind.Group;
 }
Exemple #19
0
        public static OptTableBuilder AddSeparate(
            this OptTableBuilder builder,
            OptSpecifier id,
            IReadOnlyList <string> prefixes,
            string name,
            string helpText      = null,
            string metaVar       = null,
            OptSpecifier?aliasId = null,
            OptSpecifier?groupId = null)
        {
            if (!id.IsValid)
            {
                throw new ArgumentException("Invalid id");
            }
            if (prefixes == null)
            {
                throw new ArgumentNullException(nameof(prefixes));
            }
            if (prefixes.Count == 0)
            {
                throw new ArgumentException("Contract violated: prefixes.Count != 0");
            }
            if (prefixes.Any(string.IsNullOrWhiteSpace))
            {
                throw new ArgumentException("Contract violated: !string.IsNullOrWhiteSpace(prefix)");
            }
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            var option = new SeparateOption(
                id.Id,
                prefixes,
                name,
                helpText: helpText,
                metaVar: metaVar,
                aliasId: aliasId,
                groupId: groupId);

            builder.Add(option);
            return(builder);
        }
Exemple #20
0
        protected Option(
            OptSpecifier id,
            IReadOnlyList <string> prefixes,
            string name,
            OptSpecifier?aliasId = null,
            OptSpecifier?groupId = null,
            string helpText      = null,
            string metaVar       = null,
            int flags            = 0)
        {
            if (!id.IsValid)
            {
                throw new ArgumentException("Invalid id");
            }
            if (prefixes == null)
            {
                throw new ArgumentNullException(nameof(prefixes));
            }
            if (prefixes.Count == 0)
            {
                throw new ArgumentException("Contract violated: prefixes.Count != 0");
            }
            if (prefixes.Any(string.IsNullOrWhiteSpace))
            {
                throw new ArgumentException("Contract violated: !string.IsNullOrWhiteSpace(prefix)");
            }
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            Id            = id.Id;
            this.prefixes = prefixes.ToArray();
            this.name     = name;
            this.aliasId  = aliasId.GetValueOrDefault().Id;
            this.groupId  = groupId.GetValueOrDefault().Id;
            HelpText      = helpText;
            MetaVar       = metaVar;
            Flags         = flags;
        }
Exemple #21
0
        public static OptTableBuilder AddJoinedOrSeparate(
            this OptTableBuilder builder,
            OptSpecifier id,
            string prefix,
            string name,
            string helpText      = null,
            string metaVar       = null,
            OptSpecifier?aliasId = null,
            OptSpecifier?groupId = null)
        {
            var option = new JoinedOrSeparateOption(
                id.Id,
                prefix,
                name,
                helpText: helpText,
                metaVar: metaVar,
                aliasId: aliasId,
                groupId: groupId);

            builder.Add(option);
            return(builder);
        }
Exemple #22
0
        public static OptTableBuilder AddRemainingArgs(
            this OptTableBuilder builder,
            OptSpecifier id,
            IReadOnlyList <string> prefixes,
            string name,
            string helpText      = null,
            string metaVar       = null,
            OptSpecifier?aliasId = null,
            OptSpecifier?groupId = null)
        {
            var option = new RemainingArgsOption(
                id.Id,
                prefixes,
                name,
                helpText: helpText,
                metaVar: metaVar,
                aliasId: aliasId,
                groupId: groupId);

            builder.Add(option);
            return(builder);
        }
Exemple #23
0
        public static OptTableBuilder AddJoined(
            this OptTableBuilder builder,
            OptSpecifier id,
            string prefix,
            string name,
            string helpText      = null,
            string metaVar       = null,
            OptSpecifier?aliasId = null,
            OptSpecifier?groupId = null)
        {
            if (!id.IsValid)
            {
                throw new ArgumentException("Invalid id");
            }
            if (prefix == null)
            {
                throw new ArgumentNullException(nameof(prefix));
            }
            if (string.IsNullOrWhiteSpace(prefix))
            {
                throw new ArgumentException("Contract violated: !string.IsNullOrWhiteSpace(prefix)");
            }
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            var option = new JoinedOption(
                id.Id,
                prefix,
                name,
                helpText: helpText,
                metaVar: metaVar,
                aliasId: aliasId,
                groupId: groupId);

            builder.Add(option);
            return(builder);
        }
Exemple #24
0
        public virtual bool Matches(OptSpecifier opt)
        {
            Option alias = Alias;

            if (alias != null)
            {
                return(alias.Matches(opt));
            }

            if (opt.Id == Id)
            {
                return(true);
            }

            Option group = Group;

            if (group != null)
            {
                return(group.Matches(opt));
            }

            return(false);
        }
Exemple #25
0
        public static OptTableBuilder AddMultiArg(
            this OptTableBuilder builder,
            OptSpecifier id,
            IReadOnlyList <string> prefixes,
            string name,
            int argumentCount,
            string helpText      = null,
            string metaVar       = null,
            OptSpecifier?aliasId = null,
            OptSpecifier?groupId = null)
        {
            var option = new MultiArgOption(
                id.Id,
                prefixes,
                name,
                argumentCount,
                helpText: helpText,
                metaVar: metaVar,
                aliasId: aliasId,
                groupId: groupId);

            builder.Add(option);
            return(builder);
        }
Exemple #26
0
        protected Option(
            OptSpecifier id,
            string prefix,
            string name,
            OptSpecifier?aliasId = null,
            OptSpecifier?groupId = null,
            string helpText      = null,
            string metaVar       = null,
            int flags            = 0)
        {
            if (!id.IsValid)
            {
                throw new ArgumentException("Invalid id");
            }
            if (prefix == null)
            {
                throw new ArgumentNullException(nameof(prefix));
            }
            if (string.IsNullOrWhiteSpace(prefix))
            {
                throw new ArgumentException("Contract violated: !string.IsNullOrWhiteSpace(prefix)");
            }
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            Id           = id.Id;
            prefixes     = new[] { prefix };
            this.name    = name;
            HelpText     = helpText;
            MetaVar      = metaVar;
            this.aliasId = aliasId.GetValueOrDefault().Id;
            this.groupId = groupId.GetValueOrDefault().Id;
            Flags        = flags;
        }
Exemple #27
0
 public bool HasArg(OptSpecifier id)
 {
     return(GetLastArg(id) != null);
 }
Exemple #28
0
 public IEnumerable <Arg> Matching(OptSpecifier id)
 {
     return(args.Where(arg => arg.Option.Matches(id)));
 }
Exemple #29
0
 public void RemoveAllArgs(OptSpecifier id)
 {
     args.RemoveAll(a => a.Option.Matches(id));
 }