Skip to content

Commit bb7cbbc

Browse files
committed
Add argument parser to new examples
1 parent ea2b2a7 commit bb7cbbc

3 files changed

Lines changed: 36 additions & 4 deletions

File tree

README.rst

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,17 @@ StreamWriter objects:
157157
158158
159159
async def main():
160-
reader, writer = await asyncio.open_connection('localhost', 502)
160+
# Parse command line arguments
161+
parser = ArgumentParser()
162+
parser.add_argument("-a", "--address", default="localhost:502")
163+
164+
args = parser.parse_args()
165+
if ":" not in args.address:
166+
args.address += ":502"
167+
host, port = args.address.rsplit(":", 1)
168+
port = int(port)
169+
170+
reader, writer = await asyncio.open_connection(host, port)
161171
162172
# Returns a message or Application Data Unit (ADU) specific for doing
163173
# Modbus TCP/IP.

scripts/examples/simple_asyncio_tcp_client.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
#!/usr/bin/env python
22
# scripts/examples/simple_async_tcp_client.py
33
import asyncio
4+
from argparse import ArgumentParser
45

56
from umodbus import conf
67
from umodbus.client import tcp
7-
from umodbus.client.asynch import send_message
8+
from umodbus.client.tcp.asynch import send_message
89

910

1011
# Enable values to be signed (default is False).
1112
conf.SIGNED_VALUES = True
1213

1314

1415
async def main():
15-
reader, writer = await asyncio.open_connection('localhost', 502)
16+
# Parse command line arguments
17+
parser = ArgumentParser()
18+
parser.add_argument("-a", "--address", default="localhost:502")
19+
20+
args = parser.parse_args()
21+
if ":" not in args.address:
22+
args.address += ":502"
23+
host, port = args.address.rsplit(":", 1)
24+
port = int(port)
25+
26+
reader, writer = await asyncio.open_connection(host, port)
1627

1728
# Returns a message or Application Data Unit (ADU) specific for doing
1829
# Modbus TCP/IP.

scripts/examples/simple_curio_tcp_client.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22
# scripts/examples/simple_curio_tcp_client.py
33
import curio
4+
from argparse import ArgumentParser
45

56
from umodbus import conf
67
from umodbus.client import tcp
@@ -31,7 +32,17 @@ async def readexactly(self, n):
3132

3233

3334
async def main():
34-
sock = await curio.open_connection('localhost', 502)
35+
# Parse command line arguments
36+
parser = ArgumentParser()
37+
parser.add_argument("-a", "--address", default="localhost:502")
38+
39+
args = parser.parse_args()
40+
if ":" not in args.address:
41+
args.address += ":502"
42+
host, port = args.address.rsplit(":", 1)
43+
port = int(port)
44+
45+
sock = await curio.open_connection(host, port)
3546
stream = CurioStream(sock)
3647

3748
# Returns a message or Application Data Unit (ADU) specific for doing

0 commit comments

Comments
 (0)