import shlex


def solc_arguments(libraries=None, combined='bin,abi',
                   optimize=True, extra_args=None):
    """ Build the arguments to call the solc binary. """
    args = [
        '--combined-json', combined,
        '--add-std'
    ]

    if optimize:
        args.append('--optimize')

    if extra_args:
        try:
            args.extend(shlex.split(extra_args))
        except BaseException:  # if not a parseable string then treat it as a list
            args.extend(extra_args)

    if libraries is not None and len(libraries):
        addresses = [
            '{name}:{address}'.format(
                name=name, address=address.decode('utf8'))
            for name, address in libraries.items()
        ]
        args.extend([
            '--libraries',
            ','.join(addresses),
        ])

    return args