Skip to content

Commit

Permalink
Add aliases.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tage Johansson committed Apr 16, 2024
1 parent f7a58a6 commit 9ec7f1b
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 166 deletions.
2 changes: 1 addition & 1 deletion chess_cli/analysis_cmds.py
Expand Up @@ -83,7 +83,7 @@ class AnalysisCmds(Analysis):
),
)

@argparse_command(analysis_argparser)
@argparse_command(analysis_argparser, alias="a")
async def do_analysis(self, args) -> None:
"""Manage analysis."""
match args.subcmd:
Expand Down
12 changes: 6 additions & 6 deletions chess_cli/curr_move_cmds.py
Expand Up @@ -66,7 +66,7 @@ def show_clock(self) -> str | None:

show_argparser = ArgumentParser()

@argparse_command(show_argparser)
@argparse_command(show_argparser, alias=["s"])
def do_show(self, args) -> None:
"""Show position, comments, NAGs and more about the current move."""
self.poutput(f"FEN: {self.show_fen()}")
Expand Down Expand Up @@ -99,7 +99,7 @@ def do_fen(self, args) -> None:

board_argparser = ArgumentParser()

@argparse_command(board_argparser)
@argparse_command(board_argparser, alias=["b"])
def do_board(self, args) -> None:
"""Show the current position as an ASCII chess board."""
self.poutput(self.show_board())
Expand Down Expand Up @@ -134,7 +134,7 @@ def do_board(self, args) -> None:
)
comment_append_argparser.add_argument("comment", help="The text to append.")

@argparse_command(comment_argparser)
@argparse_command(comment_argparser, alias=["c"])
async def do_comment(self, args) -> None:
"""Show, edit or remove the comment at the current move."""
if args.starting_comment and not self.game_node.starts_variation():
Expand Down Expand Up @@ -268,7 +268,7 @@ def do_nag(self, args) -> None:
"-d", "--depth", type=int, help="The depth at which the analysis was made."
)

@argparse_command(evaluation_argparser)
@argparse_command(evaluation_argparser, alias=["eval"])
def do_evaluation(self, args) -> None:
"""Show, edit or remove evaluations at the current move."""
match args.subcmd:
Expand Down Expand Up @@ -321,7 +321,7 @@ def do_evaluation(self, args) -> None:
help="Color of the arrow. Red/yellow/green/blue can be abbreviated as r/y/g/b.",
)

@argparse_command(arrow_argparser)
@argparse_command(arrow_argparser, alias=["ar"])
def do_arrow(self, args) -> None:
"""Show, edit or remove arrows at the current move."""
color_abbreviations: dict[str, str] = {"g": "green", "y": "yellow", "r": "red", "b": "blue"}
Expand Down Expand Up @@ -357,7 +357,7 @@ def do_arrow(self, args) -> None:
)
clock_set_argparser.add_argument("time", help="Remaining time.")

@argparse_command(clock_argparser)
@argparse_command(clock_argparser, alias=["cl"])
def do_clock(self, args) -> None:
"""Show, edit or remove clock information at the current move."""
match args.subcmd:
Expand Down
2 changes: 1 addition & 1 deletion chess_cli/engine_cmds.py
Expand Up @@ -164,7 +164,7 @@ class EngineCmds(Engine):
engine_log_subcmds.add_parser("clear", help="Clear the log.")
engine_log_subcmds.add_parser("show", help="Show the log.")

@argparse_command(engine_argparser)
@argparse_command(engine_argparser, alias="e")
async def do_engine(self, args) -> None:
"""Everything related to chess engines.
Expand Down
32 changes: 16 additions & 16 deletions chess_cli/game_cmds.py
Expand Up @@ -42,7 +42,7 @@ class GameCmds(GameUtils):
help="Add this new list of moves as a sideline to the current move.",
)

@argparse_command(play_argparser)
@argparse_command(play_argparser, alias="p")
def do_play(self, args) -> None:
"""Play a sequence of moves from the current position."""
if args.sideline:
Expand All @@ -69,7 +69,7 @@ def do_play(self, args) -> None:
"-a", "--all", action="store_true", help="Print the entire game from the start."
)

@argparse_command(game_argparser)
@argparse_command(game_argparser, alias="gm")
async def do_game(self, args) -> None:
"""Print the rest of the game with sidelines and comments in a nice and readable
format."""
Expand Down Expand Up @@ -118,7 +118,7 @@ async def do_game(self, args) -> None:
"-r", "--recurse", action="store_true", help="Recurse into sidelines."
)

@argparse_command(moves_argparser)
@argparse_command(moves_argparser, alias="m")
def do_moves(self, args) -> None:
if args._from is not None:
# If the user has specified a given move as start.
Expand Down Expand Up @@ -206,7 +206,7 @@ def do_moves(self, args) -> None:
"-f", "--forwards-only", action="store_true", help="Only search the game forwards."
)

@argparse_command(goto_argparser)
@argparse_command(goto_argparser, alias="g")
def do_goto(self, args) -> None:
"""Goto a move specified by a move number or a move in standard algibraic
notation.
Expand Down Expand Up @@ -237,7 +237,7 @@ def do_goto(self, args) -> None:
return
self.game_node = node

@command()
@command(alias="del")
def do_delete(self, _) -> None:
"""Delete the current move if this is not the root of the game."""
self.delete_current_move()
Expand Down Expand Up @@ -270,7 +270,7 @@ def do_delete(self, _) -> None:
help="The index where the game should be inserted. Defaults to the end of the game list.",
)

@argparse_command(games_argparser)
@argparse_command(games_argparser, alias="gs")
def do_games(self, args) -> None:
"""List, select, delete or create new games."""
match args.subcmd:
Expand Down Expand Up @@ -303,7 +303,7 @@ def do_games(self, args) -> None:
help="Save only the current game and discard any changes in the other games.",
)

@argparse_command(save_argparser)
@argparse_command(save_argparser, alias="sv")
def do_save(self, args) -> None:
"""Save the games to a PGN file."""
if args.file is None:
Expand All @@ -320,7 +320,7 @@ def do_save(self, args) -> None:
load_argparser = ArgumentParser()
load_argparser.add_argument("file", help="PGN file to read.")

@argparse_command(load_argparser)
@argparse_command(load_argparser, alias="ld")
def do_load(self, args) -> None:
"""Load games from a PGN file.
Expand All @@ -337,7 +337,7 @@ def do_load(self, args) -> None:
"-n", "--steps", type=int, help="Promote this variation n number of steps."
)

@argparse_command(promote_argparser)
@argparse_command(promote_argparser, alias="pr")
def do_promote(self, args) -> None:
"""If current move is a side line, promote it so that it'll be closer to main
variation."""
Expand All @@ -360,7 +360,7 @@ def do_promote(self, args) -> None:
"-n", "--steps", type=int, help="Demote this variation n number of steps."
)

@argparse_command(demote_argparser)
@argparse_command(demote_argparser, alias="de")
def do_demote(self, args) -> None:
"""If current move is the main variation or if it isn't the last variation,
demote it so it'll be far from the main variation."""
Expand All @@ -374,12 +374,12 @@ def do_demote(self, args) -> None:
for _ in range(n):
self.game_node.parent.demote(self.game_node)

@command()
@command(alias="v")
def do_variations(self, _) -> None:
"""Print all variations following this move."""
self.show_variations(self.game_node)

@command()
@command(alias="sl")
def do_sidelines(self, _) -> None:
"""Show all sidelines to this move."""
if self.game_node.parent is not None:
Expand All @@ -393,7 +393,7 @@ def do_sidelines(self, _) -> None:
"-s", "--start", action="store_true", help="Setup the starting position."
)

@argparse_command(setup_argparser)
@argparse_command(setup_argparser, alias="st")
async def do_setup(self, args) -> None:
"""Setup a starting position."""
board: chess.Board
Expand Down Expand Up @@ -472,7 +472,7 @@ async def do_put(self, args) -> None:
help="Set the turn to play. " "Note that this will reset the current game.",
)

@argparse_command(turn_argparser)
@argparse_command(turn_argparser, alias="t")
async def do_turn(self, args) -> None:
"""Get or set the turn to play."""
if args.set_color is None:
Expand Down Expand Up @@ -501,7 +501,7 @@ async def do_turn(self, args) -> None:
"where each letter denotes king- or queenside castling for white or black respectively.",
)

@argparse_command(castling_argparser)
@argparse_command(castling_argparser, alias=["csl"])
async def do_castling(self, args) -> None:
"""Get or set castling rights."""
board: chess.Board = self.game_node.board()
Expand Down Expand Up @@ -552,7 +552,7 @@ def castling_descr(color: chess.Color) -> str:
"clear", help="Remove en passant possibilities in the current position."
)

@argparse_command(en_passant_argparser)
@argparse_command(en_passant_argparser, alias="ep")
async def do_en_passant(self, args) -> None:
"""Get, set or clear en passant square in the current position."""
board: chess.Board = self.game_node.board()
Expand Down
22 changes: 10 additions & 12 deletions chess_cli/repl.py
Expand Up @@ -278,12 +278,14 @@ def _get_cmd_name(func: Callable) -> str:

def command[T: ReplBase](
name: str | None = None,
aliases: list[str] | None = None,
alias: list[str] | str | None = None,
summary: str | None = None,
long_help: str | None = None,
) -> Callable[[CmdFunc[T]], Command[T]]:
"""A decorator for methods of `Repl` to add them as commands."""

aliases: list[str] = [alias] if isinstance(alias, str) else (alias or [])

def decorator(func: CmdFunc[T]) -> Command[T]:
nonlocal summary
nonlocal long_help
Expand All @@ -295,7 +297,7 @@ def decorator(func: CmdFunc[T]) -> Command[T]:
name: str = _get_cmd_name(func)
if asyncio.iscoroutinefunction(func):
cmd: Command[T] = Command(
name=name, aliases=aliases or [], func=func, summary=summary, long_help=long_help
name=name, aliases=aliases, func=func, summary=summary, long_help=long_help
)
else:

Expand All @@ -304,11 +306,7 @@ async def async_func(*args, **kwargs) -> None:
func(*args, **kwargs)

cmd = Command(
name=name,
aliases=aliases or [],
func=async_func,
summary=summary,
long_help=long_help,
name=name, aliases=aliases, func=async_func, summary=summary, long_help=long_help
)
functools.update_wrapper(cmd, func)
return cmd
Expand All @@ -317,7 +315,7 @@ async def async_func(*args, **kwargs) -> None:


def argparse_command[T: ReplBase](
argparser: ArgumentParser, aliases: list[str] | None = None
argparser: ArgumentParser, alias: list[str] | str | None = None
) -> Callable[[ArgparseCmdFunc[T]], Command[T]]:
"""Returns a decorator for methods of `Repl` to add them as commands with an
argparser."""
Expand All @@ -340,7 +338,7 @@ async def cmd_func(repl: T, prompt: str) -> None:
func(repl, parsed_args)

return command(
aliases=aliases,
alias=alias,
summary=argparser.description if not func.__doc__ else None,
long_help=argparser.format_help(),
)(cmd_func)
Expand Down Expand Up @@ -396,15 +394,15 @@ async def yes_no_dialog(self, question: str) -> bool:
case _:
print("Error: Please answer yes or no.")

@command(aliases=["q", "exit"])
@command(alias=["q", "exit"])
def do_quit(self, _) -> None:
"""Exit the REPL."""
raise QuitRepl()

help_argparser = ArgumentParser()
help_argparser.add_argument("command", nargs="?", help="Get help for this specific command.")

@argparse_command(help_argparser, aliases=["h"])
@argparse_command(help_argparser, alias="h")
def do_help(self, args: ParsedArgs) -> None:
"""Get a list of all possible commands or get help for a specific command."""
if args.command is not None:
Expand Down Expand Up @@ -438,7 +436,7 @@ def do_py(self, expr: str) -> None:
local_vars = {"self": self}
code.interact(local=local_vars)

@command(aliases=["kb"])
@command(alias="kb")
def do_key_bindings(self, _) -> None:
"""Print a list of all active key bindings."""
for kb in self._key_bindings:
Expand Down

0 comments on commit 9ec7f1b

Please sign in to comment.