jbuchermn / newm

Wayland compositor
MIT License
960 stars 30 forks source link

Doubt about the meaning of each command #88

Closed CRAG666 closed 2 years ago

CRAG666 commented 2 years ago

I am writing a newm-cmd, this in order for the command to have help, documented commands etc. I have already started to write this but I have doubts about the description of each command, could you tell me where to find that information or leave it in this issue, here below I leave you what I am writing:


#!/usr/bin/env python
from typing import Union
import argparse
from newm import cmd
from collections.abc import Sequence

# Declare new commands, in the following format "command": "description"
# See main function
command_without_args: dict[str, str] = {
    "lock": "Call newm's screen lock",
    "lock-pre": "",
    "lock-post": "",
    "config": "Print the current configuration",
    "debug": "Obtain debugging information (layout and wordspaces)",
    "inhibit-idle": "",
    "finish-inhibit-idle": "",
    "close-launcher": "Close launcher",
    "clean": "Waste disposal",
    "unlock": "Unlock newm",
}

# Declare new commands, in the following format:
# "command": {
#   "help": "description",
#   "args": {
#       "arg_name": "description",
#       ...
#   }
# }
# See main function
command_needs_args: dict[str, dict[str, Union[str, dict[str, str]]]] = {
    "open-virtual-output": {"help": "", "args": {"arg": ""}},
    "close-virtual-output": {"help": "", "args": {"arg": ""}},
}

def main(argv: Sequence[str] | None = None) -> int:
    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers(dest="command", required=True)

    for command, help in command_without_args.items():
        subparsers.add_parser(command, help=help)

    for command, meta in command_needs_args.items():
        command_parser = subparsers.add_parser(command, help=meta["help"])
        for argument, help in meta["args"].items():
            command_parser.add_argument(argument, nargs="?", help=help)

    args = parser.parse_args(argv)

    if args.command in command_without_args.keys():
        return call_without_args(args.command)
    elif args.command in command_needs_args.keys():
        dict_args = vars(args)
        # dict_args[a] for a in command_needs_args[args.command]["args"].keys()
        call_with_args(
            args.command,
            *tuple(
                filter(
                    lambda v: v is not None,
                    map(
                        lambda k: dict_args.get(k, None),
                        command_needs_args[args.command]["args"].keys(),
                    ),
                )
            ),
        )
    else:
        raise NotImplementedError(
            f"Command {args.command} does not exist.",
        )

def call_without_args(command: str) -> int:
    cmd(command)
    return 0

def call_with_args(command: str, *args) -> int:
    # cmd(command, *args)
    return 0

if __name__ == "__main__":
    raise SystemExit(main())

``
CRAG666 commented 2 years ago

Some commands I have already documented (if you have any suggestions I could use them) and others I honestly don't know. I would also like to know what exactly those commands that need parameters receive, so that I can implement and document them.