def main(args=None, iterable=None, output=None, results=None, opts=None):
    import logging
    import sys

    prs = get_option_parser()

    argv = args = list(args) if args is not None else [] # sys.argv[1:]
    if opts is None:
        (opts, args) = prs.parse_args(args)
    optsdict = None
    if hasattr(opts, '__dict__'):
        optsdict = opts.__dict__
    elif hasattr(opts, 'items'):
        optsdict = opts
    else:
        raise ValueError(opts)
    if not optsdict:
        optsdict = {}
    opts = optsdict

    log = logging.getLogger(DEFAULT_LOGGER)
    # if -q/--quiet is not specified
    if not opts.get('quiet'):
        #logging.basicConfig(
        #)
        log.setLevel(logging.WARN)

        # if -v/--verbose is specified
        if opts.get('verbose'):
            log.setLevel(logging.DEBUG)
    # if -q/--quiet is specified
    else:
        log.setLevel(logging.ERROR)
    log.info(('pyline.version', __version__))
    log.info(('argv', argv))
    log.info(('args', args))

    if opts.get('version'):
        print(__version__)
        return 0, None

    opts['col_map'] = collections.OrderedDict()
    if opts.get('col_mapstr'):
        opts['col_map'] = build_column_map(opts.get('col_mapstr'))

    sortfunc = None
    if opts.get('sort_asc') and opts.get('sort_desc'):
        prs.error("both sort-asc and sort-desc are specified")

    if 'cmd' not in opts:
        cmd = ' '.join(args)
        if not cmd.strip():
            if opts.get('regex'):
                if (opts.get('_output_format') == 'json'
                    and '<' in opts.get('regex')):
                    cmd = 'rgx and rgx.groupdict()'
                else:
                    cmd = 'rgx and rgx.groups()'
            else:
                cmd = 'obj'
        opts['cmd'] = cmd.strip()

    log.info(('cmd', opts['cmd']))
    # opts['attrs'] = PylineResult._fields # XX
    opts['attrs'] = list(opts['col_map'].keys()) if 'col_map' in opts else None

    try:
        if iterable is not None:
            opts['_file'] = iterable
        else:
            if opts.get('file') is '-':
                # opts._file = sys.stdin
                opts['_file'] = codecs.getreader('utf8')(sys.stdin)
            else:
                opts['_file'] = codecs.open(opts['file'], 'r', encoding='utf8')

        if output is not None:
            opts['_output'] = output
        else:
            if opts.get('output') is '-':
                # opts._output = sys.stdout
                opts['_output'] = codecs.getwriter('utf8')(sys.stdout)
            elif opts.get('output'):
                opts['_output'] = codecs.open(opts['output'], 'w', encoding='utf8')
            else:
                # opts._output = sys.stdout
                opts['_output'] = codecs.getwriter('utf8')(sys.stdout)

        if opts.get('_output_format') is None:
            #opts._output_format = DEFAULTS['_output_format']
            #opts['_output_format'] = 'csv'
            opts['_output_format'] = 'json'
            #TODO
        log.info(('_output_format', opts['_output_format']))

        log.info(('opts', opts))

        writer = ResultWriter.get_writer(
            opts['_output'],
            output_format=opts['_output_format'],
            number_lines=opts.get('number_lines'),
            attrs=opts['attrs'])
        writer.header()

        sortfunc = get_sort_function(**opts)
        # if sorting, collect and sort before printing
        if sortfunc:
            _results = []
            for result in pyline(opts['_file'], **opts):
                if not result.result:
                    # skip result if not bool(result.result)
                    continue
                _results.append(result)
            sorted_results = sortfunc(_results)
            if results is not None:
                results.extend(sorted_results)
            # import pdb; pdb.set_trace()  # XXX BREAKPOINT
            for result in sorted_results:
                writer.output_func(result)
        # if not sorting, return a result iterator
        else:
            for result in pyline(opts['_file'], **opts):
                if not result.result:
                    # skip result if not bool(result.result)
                    continue
                writer.output_func(result)
                if results is not None:
                    results.append(result)

        writer.footer()
    finally:
        if (getattr(opts.get('_file', codecs.EncodedFile),
                'fileno', int)() not in (0, 1, 2)):
            opts['_file'].close()

    # opts
    # results
    # sorted_results
    # if passed, results are .append-ed to results
    return 0, results
Return a formatted string for a list of constructions.
dataset/ETHPy150Open westurner/pyline/pyline.py/main--crossover with--dataset/ETHPy150Open aalto-speech/morfessor/morfessor/io.py/MorfessorIO.format_constructions