예제 #1
0
    public CommandLineArgs Parse(string[] args)
    {
        if (args.IsNullOrEmpty())
        {
            return(CommandLineArgs.Empty());
        }

        var argumentList = args.ToList();

        //Command

        var command = argumentList[0];

        argumentList.RemoveAt(0);

        if (!argumentList.Any())
        {
            return(new CommandLineArgs(command));
        }

        //Target

        var target = argumentList[0];

        if (target.StartsWith("-"))
        {
            target = null;
        }
        else
        {
            argumentList.RemoveAt(0);
        }

        if (!argumentList.Any())
        {
            return(new CommandLineArgs(command, target));
        }

        //Options

        var commandLineArgs = new CommandLineArgs(command, target);

        while (argumentList.Any())
        {
            var optionName = ParseOptionName(argumentList[0]);
            argumentList.RemoveAt(0);

            if (!argumentList.Any())
            {
                commandLineArgs.Options[optionName] = null;
                break;
            }

            if (IsOptionName(argumentList[0]))
            {
                commandLineArgs.Options[optionName] = null;
                continue;
            }

            commandLineArgs.Options[optionName] = argumentList[0];
            argumentList.RemoveAt(0);
        }

        return(commandLineArgs);
    }