Main function.

    This function is the command line entry point.

    Args:
        args (list of str): the arguments passed to the program.

    Returns:
        int: return code being 0 (OK), 1 (dsm empty) or 2 (error).

def main(args=None):
    """
    Main function.

    This function is the command line entry point.

    Args:
        args (list of str): the arguments passed to the program.

    Returns:
        int: return code being 0 (OK), 1 (dsm empty) or 2 (error).
    """
    parser = get_parser()
    args = parser.parse_args(args=args)

    if not (args.matrix or args.dependencies or args.treemap or args.graph):
        args.matrix = True

    # split comma-separated args
    packages = []
    for arg in args.packages:
        if ',' in arg:
            for package in arg.split(','):
                if package not in packages:
                    packages.append(package)
        elif arg not in packages:
            packages.append(arg)

    # guess convenient depth
    depth = args.depth
    if depth is None:
        depth = guess_depth(packages)

    # open file if not stdout
    output = args.output
    if isinstance(output, str):
        output = open(output, 'w')

    dsm = DSM(*packages, build_tree=True, build_dependencies=True,
              enforce_init=not args.greedy)

    if dsm.empty:
        return 1

    indent = args.indent
    if indent is None:
        if args.format == CSV:
            indent = 0
        else:
            indent = 2
    elif indent < 0 and args.format == JSON:
        # special case for json.dumps indent argument
        indent = None

    try:
        if args.dependencies:
            dsm.print(format=args.format, output=output, indent=indent)
        elif args.matrix:
            dsm.print_matrix(format=args.format, output=output,
                             depth=depth, indent=indent)
        elif args.treemap:
            dsm.print_treemap(format=args.format, output=output)
        elif args.graph:
            dsm.print_graph(format=args.format, output=output,
                            depth=depth, indent=indent)
    except BrokenPipeError:
        # avoid traceback
        return 2

    return 0