How to use the plac.import_main function in plac

To help you get started, we’ve selected a few plac examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github micheles / plac / doc / test_plac.py View on Github external
def check_help(name):
    sys.argv[0] = name + '.py'  # avoid issue with pytest
    plac_core._parser_registry.clear()  # makes different imports independent
    try:
        try:
            main = plac.import_main(name + '.py')
        except SyntaxError:
            if sys.version < '3':  # expected for Python 2.X
                return
            else:  # not expected for Python 3.X
                raise
        p = plac.parser_from(main)
        expected = fix_today(open(name + '.help').read()).strip()
        got = p.format_help().strip()
        assert got == expected, got
    finally:
        sys.argv[0] = sys_argv0
github micheles / plac / plac_runner.py View on Github external
def run(fnames, cmd, verbose):
    "Run batch scripts and tests"
    for fname in fnames:
        with open(fname) as f:
            lines = list(f)
        if not lines[0].startswith('#!'):
            sys.exit('Missing or incorrect shebang line!')
        firstline = lines[0][2:]  # strip the shebang
        init_args = shlex.split(firstline)
        tool = plac.import_main(*init_args)
        command = getattr(plac.Interpreter(tool), cmd)  # doctest or execute
        if verbose:
            sys.stdout.write('Running %s with %s' % (fname, firstline))
        command(lines[1:], verbose=verbose)
github micheles / plac / plac_runner.py View on Github external
*extra):
    "Runner for plac tools, plac batch files and plac tests"
    baseparser = plac.parser_from(main)
    if fname is None:
        baseparser.print_help()
    elif sys.argv[1] == fname:  # script mode
        plactool = plac.import_main(fname)
        plactool.prog = os.path.basename(sys.argv[0]) + ' ' + fname
        out = plac.call(plactool, sys.argv[2:], eager=False)
        if plac.iterable(out):
            for output in out:
                print(output)
        else:
            print(out)
    elif interactive or multiline or serve:
        plactool = plac.import_main(fname, *extra)
        plactool.prog = ''
        i = plac.Interpreter(plactool)
        if interactive:
            i.interact(verbose=verbose)
        elif multiline:
            i.multiline(verbose=verbose)
        elif serve:
            i.start_server(serve)
    elif batch:
        run((fname,) + extra, 'execute', verbose)
    elif test:
        run((fname,) + extra, 'doctest', verbose)
        print('run %s plac test(s)' % (len(extra) + 1))
    else:
        baseparser.print_usage()
main.add_help = False
github micheles / plac / plac_runner.py View on Github external
def main(verbose, interactive, multiline, serve, batch, test, fname=None,
         *extra):
    "Runner for plac tools, plac batch files and plac tests"
    baseparser = plac.parser_from(main)
    if fname is None:
        baseparser.print_help()
    elif sys.argv[1] == fname:  # script mode
        plactool = plac.import_main(fname)
        plactool.prog = os.path.basename(sys.argv[0]) + ' ' + fname
        out = plac.call(plactool, sys.argv[2:], eager=False)
        if plac.iterable(out):
            for output in out:
                print(output)
        else:
            print(out)
    elif interactive or multiline or serve:
        plactool = plac.import_main(fname, *extra)
        plactool.prog = ''
        i = plac.Interpreter(plactool)
        if interactive:
            i.interact(verbose=verbose)
        elif multiline:
            i.multiline(verbose=verbose)
        elif serve:

plac

The smartest command line arguments parser in the world

BSD-2-Clause
Latest version published 2 months ago

Package Health Score

80 / 100
Full package analysis