Replace click with argparse

This commit is contained in:
mhchia 2019-07-24 18:43:49 +08:00
parent 04b7df9fcf
commit 381f5ddc3a
No known key found for this signature in database
GPG Key ID: 389EFBEA1362589A
2 changed files with 32 additions and 18 deletions

View File

@ -1,8 +1,8 @@
import argparse
import asyncio import asyncio
import sys import sys
import urllib.request import urllib.request
import click
import multiaddr import multiaddr
from libp2p import new_node from libp2p import new_node
@ -53,8 +53,8 @@ async def run(port, destination):
print("\nWaiting for incoming connection\n\n") print("\nWaiting for incoming connection\n\n")
else: # its the client else: # its the client
m = multiaddr.Multiaddr(destination) maddr = multiaddr.Multiaddr(destination)
info = info_from_p2p_addr(m) info = info_from_p2p_addr(maddr)
# Associate the peer with local ip address # Associate the peer with local ip address
await host.connect(info) await host.connect(info)
@ -67,22 +67,37 @@ async def run(port, destination):
print("Connected to peer %s" % info.addrs[0]) print("Connected to peer %s" % info.addrs[0])
@click.command() def main():
@click.option('--port', '-p', help='source port number', default=8000) description = """
@click.option('--destination', '-d', help="Destination multiaddr string") This program demonstrates a simple p2p chat application using libp2p.
@click.option('--help', is_flag=True, default=False, help='display help') To use it, first run 'python ./chat -p <PORT>', where <PORT> is the port number.
# @click.option('--debug', is_flag=True, default=False, help='Debug generates the same node ID on every execution') Then, run another host with 'python ./chat -p <ANOTHER_PORT> -d <DESTINATION>',
def main(port, destination, help): where <DESTINATION> is the multiaddress of the previous listener host.
"""
if help: example_maddr = "/ip4/127.0.0.1/tcp/8000/p2p/QmQn4SwGkDZKkUEpBRBvTmheQycxAHJUNmVEnjA2v1qe8Q"
print("This program demonstrates a simple p2p chat application using libp2p\n\n") parser = argparse.ArgumentParser(description=description)
print("Usage: Run './chat -p <SOURCE_PORT>' where <SOURCE_PORT> can be any port number.") parser.add_argument(
print("Now run './chat -p <PORT> -d <MULTIADDR>' where <MULTIADDR> is multiaddress of previous listener host.") "--debug",
return action='store_true',
help='generate the same node ID on every execution',
)
parser.add_argument(
"-p",
"--port",
default=8000,
help="source port number",
)
parser.add_argument(
"-d",
"--destination",
type=str,
help=f"destination multiaddr string, e.g. {example_maddr}",
)
args = parser.parse_args()
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
try: try:
asyncio.ensure_future(run(port, destination)) asyncio.ensure_future(run(args.port, args.destination))
loop.run_forever() loop.run_forever()
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass

View File

@ -36,7 +36,6 @@ setuptools.setup(
classifiers=classifiers, classifiers=classifiers,
install_requires=[ install_requires=[
"pycryptodome>=3.8.2,<4.0.0", "pycryptodome>=3.8.2,<4.0.0",
"click>=7.0,<8.0",
"base58>=1.0.3,<2.0.0", "base58>=1.0.3,<2.0.0",
"pymultihash>=0.8.2", "pymultihash>=0.8.2",
"multiaddr>=0.0.8,<0.1.0", "multiaddr>=0.0.8,<0.1.0",