Created
June 1, 2021 07:42
-
-
Save ShenTengTu/82bcef7af51b3dc21f62d1f705fc5c07 to your computer and use it in GitHub Desktop.
Python: simple module to build simple command line tool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Run `python main.py help`: | |
Output: | |
``` | |
Use: main.py <command> | |
Commands: | |
help Helping imformation. | |
demo_cmd Demo command docstring. | |
``` | |
Run `python main.py demo_cmd`: | |
Output: | |
``` | |
Execute `demo_cmd` | |
``` | |
""" | |
from mk_cmd import cmd, parse_cmd | |
@cmd | |
def demo_cmd(): | |
"""Demo command docstring.""" | |
print("Execute `demo_cmd`") | |
if __name__ == "__main__": | |
parse_cmd() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__all__ = ["cmd", "parse_cmd"] | |
import sys | |
from typing import Callable | |
def cmd(fn: Callable): | |
"""A decorator binding the callable object as a command. | |
- Command name is callable object name. | |
- Command description is callable object docstring. | |
- Duplicate commands will not override previous command. | |
""" | |
if not hasattr(cmd, "_callables"): | |
cmd._callables = {} | |
if "help" not in cmd._callables: | |
def help(): | |
"""Helping imformation.""" | |
print("Use: {} <command>".format(sys.argv[0])) | |
print("Commands:") | |
w = max(len(k) for k in cmd._callables) | |
for k, fn in cmd._callables.items(): | |
print(f" {str(k).ljust(w)} {fn.__doc__}") | |
cmd._callables["help"] = help | |
if callable(fn): | |
cmd._callables.setdefault(fn.__name__, fn) | |
return fn | |
def parse_cmd(): | |
"""Parse command line arguments to invoke available commands. | |
Only one `<command>` argument is currently supported. | |
""" | |
if len(sys.argv) < 2: | |
print("Use: {} <command>".format(sys.argv[0])) | |
sys.exit(0) | |
cmd(...) # initialization | |
fn = cmd._callables.get(sys.argv[1]) | |
if callable(fn): | |
fn() | |
else: | |
print("Please use `{} help` to see available commands.".format(sys.argv[0])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment