Skip to content

Commit 6002c8e

Browse files
authored
Move get_commandline into setup_x. (#1440)
1 parent 6b27cd1 commit 6002c8e

15 files changed

+416
-508
lines changed

examples/client_async.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@
4040
_logger = logging.getLogger()
4141

4242

43-
def setup_async_client(args):
43+
def setup_async_client(description=None, cmdline=None):
4444
"""Run client setup."""
45+
args = get_commandline(server=False, description=description, cmdline=cmdline)
4546
_logger.info("### Create client object")
4647
if args.comm == "tcp":
4748
client = AsyncModbusTcpClient(
@@ -128,9 +129,5 @@ async def run_async_client(client, modbus_calls=None):
128129

129130

130131
if __name__ == "__main__":
131-
cmd_args = get_commandline(
132-
server=False,
133-
description="Run asynchronous client.",
134-
)
135-
testclient = setup_async_client(cmd_args)
132+
testclient = setup_async_client(description="Run asynchronous client.")
136133
asyncio.run(run_async_client(testclient), debug=True)

examples/client_calls.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import pymodbus.other_message as req_other
4343
from examples.client_async import run_async_client, setup_async_client
4444
from examples.client_sync import run_sync_client, setup_sync_client
45-
from examples.helper import get_commandline
4645
from pymodbus.exceptions import ModbusException
4746
from pymodbus.pdu import ExceptionResponse
4847

@@ -289,11 +288,11 @@ def run_sync_calls(client):
289288

290289

291290
if __name__ == "__main__":
292-
cmd_args = get_commandline(
293-
server=False,
294-
description="Run modbus calls in asynchronous client.",
291+
testclient = setup_async_client(
292+
description="Run modbus calls in asynchronous client."
295293
)
296-
testclient = setup_async_client(cmd_args)
297294
asyncio.run(run_async_client(testclient, modbus_calls=run_async_calls))
298-
testclient = setup_sync_client(cmd_args)
295+
testclient = setup_sync_client(
296+
description="Run modbus calls in synchronous client."
297+
)
299298
run_sync_client(testclient, modbus_calls=run_sync_calls)

examples/client_payload.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from collections import OrderedDict
1313

1414
from examples.client_async import run_async_client, setup_async_client
15-
from examples.helper import get_commandline
1615
from pymodbus.constants import Endian
1716
from pymodbus.payload import BinaryPayloadBuilder, BinaryPayloadDecoder
1817

@@ -138,8 +137,5 @@ async def run_payload_calls(client):
138137

139138

140139
if __name__ == "__main__":
141-
cmd_args = get_commandline(
142-
description="Run payload client.",
143-
)
144-
testclient = setup_async_client(cmd_args)
140+
testclient = setup_async_client(description="Run payload client.")
145141
asyncio.run(run_async_client(testclient, modbus_calls=run_payload_calls))

examples/client_sync.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,13 @@
3939
_logger = logging.getLogger()
4040

4141

42-
def setup_sync_client(args):
42+
def setup_sync_client(description=None, cmdline=None):
4343
"""Run client setup."""
44+
args = get_commandline(
45+
server=False,
46+
description=description,
47+
cmdline=cmdline,
48+
)
4449
_logger.info("### Create client object")
4550
if args.comm == "tcp":
4651
client = ModbusTcpClient(
@@ -126,9 +131,5 @@ def run_sync_client(client, modbus_calls=None):
126131

127132

128133
if __name__ == "__main__":
129-
cmd_args = get_commandline(
130-
server=False,
131-
description="Run synchronous client.",
132-
)
133-
testclient = setup_sync_client(cmd_args)
134+
testclient = setup_sync_client(description="Run synchronous client.")
134135
run_sync_client(testclient)

examples/client_test.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import pymodbus.diag_message as req_diag
1414
import pymodbus.mei_message as req_mei
1515
from examples.client_async import run_async_client, setup_async_client
16-
from examples.helper import get_commandline
1716
from pymodbus.pdu import ExceptionResponse
1817

1918

@@ -86,17 +85,15 @@ async def _execute_diagnostic_requests(client):
8685

8786
async def run_async_calls(client):
8887
"""Demonstrate basic read/write calls."""
89-
# await _handle_coils(client)
90-
# await _handle_discrete_input(client)
91-
# await _handle_holding_registers(client)
88+
await _handle_coils(client)
89+
await _handle_discrete_input(client)
90+
await _handle_holding_registers(client)
9291
await _execute_information_requests(client)
93-
# await _execute_diagnostic_requests(client)
92+
await _execute_diagnostic_requests(client)
9493

9594

9695
if __name__ == "__main__":
97-
cmd_args = get_commandline(
98-
server=False,
99-
description="Run modbus calls in asynchronous client.",
96+
testclient = setup_async_client(
97+
description="Run modbus calls in asynchronous client."
10098
)
101-
testclient = setup_async_client(cmd_args)
10299
asyncio.run(run_async_client(testclient, modbus_calls=run_async_calls))

examples/helper.py

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
get_command_line
66
"""
77
import argparse
8-
import dataclasses
98
import logging
109

1110
from pymodbus import pymodbus_apply_logging_config
@@ -21,59 +20,40 @@
2120
_logger = logging.getLogger()
2221

2322

24-
@dataclasses.dataclass
25-
class Commandline:
26-
"""Simulate commandline parameters.
27-
28-
Replaces get_commandline() and allows application to set arguments directly.
29-
"""
30-
31-
comm = None
32-
framer = None
33-
host = "127.0.0.1"
34-
port = None
35-
baudrate = 9600
36-
store = "sequential"
37-
identity = None
38-
context = None
39-
slaves = None
40-
client_port = None
41-
client = None
42-
log = "debug"
43-
44-
@classmethod
45-
def copy(cls):
46-
"""Copy kl"""
47-
to_copy = cls()
48-
return dataclasses.replace(to_copy)
49-
50-
5123
def get_commandline(server=False, description=None, extras=None, cmdline=None):
5224
"""Read and validate command line arguments"""
5325
parser = argparse.ArgumentParser(description=description)
5426
parser.add_argument(
27+
"-c",
5528
"--comm",
5629
choices=["tcp", "udp", "serial", "tls"],
5730
help="set communication, default is tcp",
31+
dest="comm",
5832
default="tcp",
5933
type=str,
6034
)
6135
parser.add_argument(
36+
"-f",
6237
"--framer",
6338
choices=["ascii", "binary", "rtu", "socket", "tls"],
6439
help="set framer, default depends on --comm",
40+
dest="framer",
6541
type=str,
6642
)
6743
parser.add_argument(
44+
"-l",
6845
"--log",
6946
choices=["critical", "error", "warning", "info", "debug"],
7047
help="set log level, default is info",
48+
dest="log",
7149
default="info",
7250
type=str,
7351
)
7452
parser.add_argument(
53+
"-p",
7554
"--port",
7655
help="set port",
56+
dest="port",
7757
type=str,
7858
)
7959
parser.add_argument(
@@ -106,10 +86,10 @@ def get_commandline(server=False, description=None, extras=None, cmdline=None):
10686
parser.add_argument(
10787
"--host",
10888
help="set host, default is 127.0.0.1",
89+
dest="host",
10990
default="127.0.0.1",
11091
type=str,
11192
)
112-
11393
if extras:
11494
for extra in extras:
11595
parser.add_argument(extra[0], **extra[1])

0 commit comments

Comments
 (0)