Skip to content

cli

  • Name: cognitivefactory.interactive_clustering_gui.cli
  • Description: Module that contains the command line application.
  • Author: Erwan Schild
  • Created: 22/10/2021
  • Licence: CeCILL-C License v1.0 (https://cecill.info/licences.fr.html)

Why does this file exist, and why not put this in __main__?

You might be tempted to import things from __main__ later, but that will cause problems: the code will get executed twice: - When you run python -m cognitivefactory.interactive_clustering_gui python will execute __main__.py as a script. That means there won't be any cognitivefactory.interactive_clustering_gui.__main__ in sys.modules. - When you import __main__ it will get executed again (as a module) because there's no cognitivefactory.interactive_clustering_gui.__main__ in sys.modules.

get_parser()

Define possible arguments of the CLI argument parser.

Returns:

Type Description
ArgumentParser

An argparse parser.

Source code in src\cognitivefactory\interactive_clustering_gui\cli.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def get_parser() -> argparse.ArgumentParser:
    """
    Define possible arguments of the CLI argument parser.

    Returns:
        An argparse parser.
    """
    parser = argparse.ArgumentParser(
        prog="cognitivefactory-interactive-clustering-gui",
        description="A web application designed for NLP data annotation using Interactive Clustering methodology.",
        epilog="For more details, https://cognitivefactory.github.io/interactive-clustering-gui/",
    )
    parser.add_argument(
        "--host",
        type=str,
        default="127.0.0.1",
        help="The host to bind. Defaults to `127.0.0.1`.",
    )
    parser.add_argument(
        "--port",
        type=int,
        default=8080,
        help="The port to use. Defaults to `8080`.",
    )
    parser.add_argument(
        "--log-level",
        type=str,
        choices=["critical", "error", "warning", "info", "debug", "trace"],
        default="info",
        help="The log level. Defaults to `info`.",
    )
    return parser

main(args=None)

Run the main program.

This function is executed when you type cognitivefactory-interactive-clustering-gui or python -m cognitivefactory.interactive_clustering_gui.

Parameters:

Name Type Description Default
args Optional[List[str]]

Arguments passed from the command line.

None

Returns:

Type Description
int

A default exit code.

Source code in src\cognitivefactory\interactive_clustering_gui\cli.py
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
def main(args: Optional[List[str]] = None) -> int:
    """
    Run the main program.

    This function is executed when you type `cognitivefactory-interactive-clustering-gui` or `python -m cognitivefactory.interactive_clustering_gui`.

    Args:
        args: Arguments passed from the command line.

    Returns:
        A default exit code.
    """
    # Parse CLI arguments.
    parser = get_parser()
    opts = parser.parse_args(args=args)

    # Config the serveur.
    config = Config(  # pragma: nocover
        "cognitivefactory.interactive_clustering_gui.app:app",
        host=opts.host,
        port=opts.port,
        log_level=opts.log_level,
    )
    server = Server(config)  # pragma: nocover

    # Launch the server.
    server.run()  # pragma: nocover

    # Return a default exit code.
    return 0  # pragma: nocover