Last active
May 2, 2024 03:38
-
-
Save mariocesar/3fcac4bf7fa1a563bdb5a9551f8cda11 to your computer and use it in GitHub Desktop.
An example on how to Parse commands from Slack using argparse
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
import argparse | |
import shlex | |
from typing import List, Optional, TypedDict | |
class Command(TypedDict): | |
prefix: str | |
name: str | |
args: List[str] | |
def parse_command(command_text: str) -> Optional[Command]: | |
prefix, *args = shlex.split(command_text) | |
if not prefix.startswith("/"): | |
return None | |
parser = argparse.ArgumentParser() | |
parser.add_argument("command", type=str, help="The command to execute") | |
parser.add_argument("args", nargs="*", help="Additional arguments for the command") | |
try: | |
options = parser.parse_args(args) | |
return Command(prefix=prefix, name=options.command, args=options.args) | |
except SystemExit: | |
return None | |
def test_parse_command(): | |
command_text = '/tesorio create_invoice 123 "Sample Invoice"' | |
command = parse_command(command_text) | |
assert command == { | |
"prefix": "/tesorio", | |
"name": "create_invoice", | |
"args": ["123", "Sample Invoice"], | |
} | |
command_text = "/prefix command arg1 arg2 arg3" | |
command = parse_command(command_text) | |
assert command == { | |
"prefix": "/prefix", | |
"name": "command", | |
"args": ["arg1", "arg2", "arg3"], | |
} | |
command_text = "/prefix command" | |
command = parse_command(command_text) | |
assert command == {"prefix": "/prefix", "name": "command", "args": []} | |
command_text = " /prefix command arg1 arg2 'Mi long text with spaces' " | |
command = parse_command(command_text) | |
assert command == { | |
"prefix": "/prefix", | |
"name": "command", | |
"args": ["arg1", "arg2", "Mi long text with spaces"], | |
} | |
command_text = "command arg1 arg2" | |
command = parse_command(command_text) | |
assert command is None | |
command_text = "/prefix command arg1 arg2 --invalid-arg" | |
command = parse_command(command_text) | |
assert command is None |
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
""" | |
TODO: Use subparsers to also validate arguments by commands | |
""" | |
parser = argparse.ArgumentParser(description='Tesorio Slack Bot') | |
subparsers = parser.add_subparsers(dest='command') | |
retrieve_invoice_parser = subparsers.add_parser('invoice', help='Retrieve an invoice') | |
retrieve_invoice_parser.add_argument('invoice_id', type=int, help='Invoice ID') | |
retrieve_customer_parser = subparsers.add_parser('customer', help='Retrieve a cutomer summary') | |
retrieve_customer_parser.add_argument('customer_id', type=int, help='Customer ID') | |
watch_parser = subparsers.add_parser('watch', help='Watch an invoice') | |
watch_parser.add_argument('invoice_id', type=int, help='Invoice ID') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment