Skip to content

Args module

Provide the classes and functions to handle CLI arguments and options.

This module allows the handle CLI arguments and options.

The module contains the following classes and functions: - Args(NamedTuple) - A class to create a namedtuple to handle arguments for CLI - parse_args - Returns type handled tuple with information about the players and initial Mark.

Args

Bases: NamedTuple

A class that handle arguments for CLI. Extends NamedTuple

Attributes:

Name Type Description
player1 Player

Player An instance of subclass of the Player class that represents a human or computer.

player2 Player

Player An instance of subclass of the Player class that represents a human or computer.

renderer Player

Renderer An instance of subclass of the Renderer class that handles UI rendering.

Source code in src\frontend\console\args.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Args(NamedTuple):
    """A class that handle arguments for CLI. Extends NamedTuple

    Attributes:
        player1: Player
            An instance of subclass of the Player class that represents a human or computer.
        player2: Player
            An instance of subclass of the Player class that represents a human or computer.
        renderer: Renderer
            An instance of subclass of the Renderer class that handles UI rendering.
    """
    player1: Player
    player2: Player
    starting_mark: Mark

parse_args()

Returns type handled tuple with information about the players and initial Mark.

Returns:

Name Type Description
Args Args

tuple[Player, Player, Mark] tuple with players and Mark

Source code in src\frontend\console\args.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def parse_args() -> Args:
    """Returns type handled tuple with information about the players and initial Mark.

    Returns:
        Args: tuple[Player, Player, Mark] tuple with players and Mark
    """

    parser = argparse.ArgumentParser()
    parser.add_argument(
        "-X",
        dest="player_x",
        choices=PLAYER_CLASSES.keys(),
        default="human",
    )
    parser.add_argument(
        "-O",
        dest="player_o",
        choices=PLAYER_CLASSES.keys(),
        default="minimax",
    )
    parser.add_argument(
        "-s",
        "--starting",
        dest="starting_mark",
        choices=Mark,
        type=Mark,
        default="X",
    )
    args = parser.parse_args()

    player1 = PLAYER_CLASSES[args.player_x](Mark("X"))
    player2 = PLAYER_CLASSES[args.player_o](Mark("O"))

    if args.starting_mark == "O":
        player1, player2 = player2, player1

    return Args(player1, player2, args.starting_mark)