Add CLI run options, update README
This commit is contained in:
+30
-16
@@ -1,4 +1,5 @@
|
||||
import asyncio
|
||||
import argparse
|
||||
|
||||
from cmd_chat.server.server import app
|
||||
from cmd_chat.client.client import Client
|
||||
@@ -28,23 +29,36 @@ async def run_client(
|
||||
|
||||
|
||||
async def run() -> None:
|
||||
action: int = int(
|
||||
input("Choose action:\n1. Run server\n2. Run client\nAction: ")
|
||||
parser = argparse.ArgumentParser(
|
||||
description='Command-line chat application'
|
||||
)
|
||||
if action == 1:
|
||||
await run_server(
|
||||
input("IP: "),
|
||||
int(input("PORT: "))
|
||||
)
|
||||
if action == 2:
|
||||
await run_client(
|
||||
input("USERNAME: "),
|
||||
input("IP: "),
|
||||
int(input("PORT: "))
|
||||
)
|
||||
parser.add_argument(
|
||||
'command',
|
||||
choices=['serve', 'connect'],
|
||||
help='Command to execute'
|
||||
)
|
||||
parser.add_argument(
|
||||
'ip_address',
|
||||
help='IP address to serve or connect'
|
||||
)
|
||||
parser.add_argument(
|
||||
'port',
|
||||
help='PORT of server'
|
||||
)
|
||||
parser.add_argument(
|
||||
'username',
|
||||
nargs='?',
|
||||
default='',
|
||||
help='Username for connection (required for connect command)'
|
||||
)
|
||||
args = parser.parse_args()
|
||||
if args.command == 'serve':
|
||||
await run_server(args.ip_address, int(args.port))
|
||||
elif args.command == 'connect':
|
||||
if not args.username:
|
||||
parser.error("Username is required for 'connect' command")
|
||||
await run_client(args.username, args.ip_address, int(args.port))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(
|
||||
run()
|
||||
)
|
||||
asyncio.run(run())
|
||||
Reference in New Issue
Block a user