def main(argv=None):
    """Start the command line interface."""
    parser = ArgumentParser(prog="pygenstub")
    parser.add_argument("--version", action="version", version="%(prog)s " + __version__)
    parser.add_argument("files", nargs="*", help="generate stubs for given files")
    parser.add_argument(
        "-m",
        "--module",
        action="append",
        metavar="MODULE",
        dest="modules",
        default=[],
        help="generate stubs for given modules",
    )
    parser.add_argument(
        "-o", "--output", metavar="PATH", dest="out_dir", help="change the output directory"
    )
    parser.add_argument(
        "--generic", action="store_true", default=False, help="generate generic stubs"
    )
    parser.add_argument("--debug", action="store_true", help="enable debug messages")

    argv = argv if argv is not None else sys.argv
    arguments = parser.parse_args(argv[1:])

    # set debug mode
    if arguments.debug:
        logging.basicConfig(level=logging.DEBUG)
        _logger.debug("running in debug mode")

    out_dir = arguments.out_dir if arguments.out_dir is not None else ""
    if (out_dir == "") and (len(arguments.modules) > 0):
        print("Output directory must be given when generating stubs for modules.")
        sys.exit(1)

    modules = []
    for path in arguments.files:
        paths = Path(path).glob("**/*.py") if Path(path).is_dir() else [Path(path)]
        for source in paths:
            if str(source).startswith(os.path.pardir):
                source = source.absolute().resolve()
            if (out_dir != "") and source.is_absolute():
                source = source.relative_to(source.root)
            destination = Path(out_dir, source.with_suffix(".pyi"))
            modules.append((source, destination))

    for mod_name in arguments.modules:
        modules.extend(get_pkg_paths(mod_name, out_dir))

    for source, destination in modules:
        _logger.info("generating stub for %s to path %s", source, destination)
        with source.open() as f:
            code = f.read()
        try:
            stub = get_stub(code, generic=arguments.generic)
        except Exception as e:
            print(source, "-", e, file=sys.stderr)
            continue

        if stub != "":
            if not destination.parent.exists():
                destination.parent.mkdir(parents=True)
            with destination.open("w") as f:
                f.write("# " + EDIT_WARNING + "\n\n" + stub)
def main ( argv = None ) : parser = ArgumentParser ( prog = "pygenstub" ) parser . add_argument ( "--version" , action = "version" , version = "%(prog)s " + __version__ ) parser . add_argument ( "files" , nargs = "*" , help = "generate stubs for given files" ) parser . add_argument ( "-m" , "--module" , action = "append" , metavar = "MODULE" , dest = "modules" , default = [ ] , help = "generate stubs for given modules" , ) parser . add_argument ( "-o" , "--output" , metavar = "PATH" , dest = "out_dir" , help = "change the output directory" ) parser . add_argument ( "--generic" , action = "store_true" , default = False , help = "generate generic stubs" ) parser . add_argument ( "--debug" , action = "store_true" , help = "enable debug messages" ) argv = argv if argv is not None else sys . argv arguments = parser . parse_args ( argv [ 1 : ] ) if arguments . debug : logging . basicConfig ( level = logging . DEBUG ) _logger . debug ( "running in debug mode" ) out_dir = arguments . out_dir if arguments . out_dir is not None else "" if ( out_dir == "" ) and ( len ( arguments . modules ) > 0 ) : print ( "Output directory must be given when generating stubs for modules." ) sys . exit ( 1 ) modules = [ ] for path in arguments . files : paths = Path ( path ) . glob ( "**/*.py" ) if Path ( path ) . is_dir ( ) else [ Path ( path ) ] for source in paths : if str ( source ) . startswith ( os . path . pardir ) : source = source . absolute ( ) . resolve ( ) if ( out_dir != "" ) and source . is_absolute ( ) : source = source . relative_to ( source . root ) destination = Path ( out_dir , source . with_suffix ( ".pyi" ) ) modules . append ( ( source , destination ) ) for mod_name in arguments . modules : modules . extend ( get_pkg_paths ( mod_name , out_dir ) ) for source , destination in modules : _logger . info ( "generating stub for %s to path %s" , source , destination ) with source . open ( ) as f : code = f . read ( ) try : stub = get_stub ( code , generic = arguments . generic ) except Exception as e : print ( source , "-" , e , file = sys . stderr ) continue if stub != "" : if not destination . parent . exists ( ) : destination . parent . mkdir ( parents = True ) with destination . open ( "w" ) as f : f . write ( "# " + EDIT_WARNING + "\n\n" + stub )
Start the command line interface.