Exemplo n.º 1
0
        private void ParseMultiOptionsArg(string curArg, ArgumentScanner argsScanner, List <CommandLineArgument> result)
        {
            for (var i = 1; i < curArg.Length; ++i)
            {
                var ch     = curArg[i];
                var option = GetOptionOrNull(ch);
                if (option == null)
                {
                    throw new InvalidArgumentsException($"Argument at position {argsScanner.Index} contains unrecognized character: {DecorateCharacter( curArg, i )}.");
                }

                if (!option.HasValue)
                {
                    AddCharOption(result, option, argsScanner.Index, curArg, i, null);
                    continue;
                }

                if (i != curArg.Length - 1 || !argsScanner.HasNext || argsScanner.Next.StartsWith("-"))
                {
                    throw new InvalidArgumentsException($"Error in argument at position {argsScanner.Index}: flag -{curArg[i]} requires a value.");
                }

                argsScanner.MoveNext();
                AddCharOption(result, option, argsScanner.Index - 1, curArg, i, argsScanner.Current);
            }
        }
Exemplo n.º 2
0
        public CommandLineArgument[] Parse(string[] args)
        {
            if (args.Length < 1)
            {
                throw new ArgumentException("The args[0] must contain the initiator command.");
            }

            var result = new List <CommandLineArgument>();

            var argsScanner = new ArgumentScanner(args);

            argsScanner.MoveNext();

            while (argsScanner.MoveNext())
            {
                var curArg = argsScanner.Current;

                if (curArg == null)
                {
                    throw new ArgumentNullException($"{nameof( args )}[{argsScanner.Index}]");
                }

                if (curArg.StartsWith("--"))
                {
                    ParseOptionArg(curArg, argsScanner, result);
                    continue;
                }

                if (curArg.StartsWith("-"))
                {
                    ParseMultiOptionsArg(curArg, argsScanner, result);
                    continue;
                }

                throw new InvalidArgumentsException($"Unrecognized argument at position {argsScanner.Index}: \"{curArg}\".");
            }

            for (var i = 0; i < result.Count; ++i)
            {
                var opi = result[i];
                if (!opi.Option.Many)
                {
                    continue;
                }

                for (var j = i + 1; j < result.Count; ++j)
                {
                    var opj = result[j];
                    if (opj.Option == opi.Option)
                    {
                        throw new InvalidArgumentsException($"Flag {opi.Nickname} can be specified only once, but was found at positions {opi.ArgIndex} and {opj.ArgIndex}.");
                    }
                }
            }

            return(result.ToArray());
        }
Exemplo n.º 3
0
        private void ParseOptionArg(string curArg, ArgumentScanner argsScanner, List <CommandLineArgument> result)
        {
            var option = GetOptionOrNull(curArg.Substring(2));

            if (option == null)
            {
                throw new InvalidArgumentsException($"Unrecognized argument at position {argsScanner.Index}: \"{curArg}\".");
            }

            if (!option.HasValue)
            {
                AddNamedOption(result, option, argsScanner.Index, curArg, null);
                return;
            }

            if (!argsScanner.HasNext || argsScanner.Next.StartsWith("-"))
            {
                throw new InvalidArgumentsException($"Error in argument at position {argsScanner.Index}: flag {curArg} requires a value.");
            }

            argsScanner.MoveNext();
            AddNamedOption(result, option, argsScanner.Index - 1, curArg, argsScanner.Current);
        }