private function createBuilderForElements(array $elements, ArgsFormat $baseFormat = null)
    {
        $builder = new ArgsFormatBuilder($baseFormat);

        foreach ($elements as $element) {
            if ($element instanceof CommandName) {
                $builder->addCommandName($element);
            } elseif ($element instanceof CommandOption) {
                $builder->addCommandOption($element);
            } elseif ($element instanceof Option) {
                $builder->addOption($element);
            } elseif ($element instanceof Argument) {
                $builder->addArgument($element);
            } else {
                throw new InvalidArgumentException(sprintf(
                    'Expected instances of CommandName, CommandOption, '.
                    'Option or Argument. Got: %s',
                    is_object($element) ? get_class($element) : gettype($element)
                ));
            }
        }

        return $builder;
    }