2424from can import Bus , BusState , Logger , SizedRotatingLogger
2525
2626
27- def main ():
27+ def _create_base_argument_parser (parser : argparse .ArgumentParser ):
28+ """Adds common options to an argument parser."""
29+
30+ parser .add_argument (
31+ "-c" ,
32+ "--channel" ,
33+ help = '''Most backend interfaces require some sort of channel.
34+ For example with the serial interface the channel might be a rfcomm device: "/dev/rfcomm0"
35+ With the socketcan interfaces valid channel examples include: "can0", "vcan0"''' ,
36+ )
37+
38+ parser .add_argument (
39+ "-i" ,
40+ "--interface" ,
41+ dest = "interface" ,
42+ help = """Specify the backend CAN interface to use. If left blank,
43+ fall back to reading from configuration files.""" ,
44+ choices = can .VALID_INTERFACES ,
45+ )
46+
47+ parser .add_argument (
48+ "-b" , "--bitrate" , type = int , help = "Bitrate to use for the CAN bus."
49+ )
50+
51+ parser .add_argument ("--fd" , help = "Activate CAN-FD support" , action = "store_true" )
52+
53+ parser .add_argument (
54+ "--data_bitrate" ,
55+ type = int ,
56+ help = "Bitrate to use for the data phase in case of CAN-FD." ,
57+ )
58+
59+
60+ def main () -> None :
2861 parser = argparse .ArgumentParser (
2962 "python -m can.logger" ,
3063 description = "Log CAN traffic, printing messages to stdout or to a given file." ,
3164 )
3265
66+ _create_base_argument_parser (parser )
67+
3368 parser .add_argument (
3469 "-f" ,
3570 "--file_name" ,
3671 dest = "log_file" ,
37- help = """ Path and base log filename, for supported types see can.Logger."" " ,
72+ help = "Path and base log filename, for supported types see can.Logger." ,
3873 default = None ,
3974 )
4075
@@ -43,7 +78,7 @@ def main():
4378 "--file_size" ,
4479 dest = "file_size" ,
4580 type = int ,
46- help = """ Maximum file size in bytes. Rotate log file when size threshold is reached."" " ,
81+ help = "Maximum file size in bytes. Rotate log file when size threshold is reached." ,
4782 default = None ,
4883 )
4984
@@ -56,23 +91,6 @@ def main():
5691 default = 2 ,
5792 )
5893
59- parser .add_argument (
60- "-c" ,
61- "--channel" ,
62- help = '''Most backend interfaces require some sort of channel.
63- For example with the serial interface the channel might be a rfcomm device: "/dev/rfcomm0"
64- With the socketcan interfaces valid channel examples include: "can0", "vcan0"''' ,
65- )
66-
67- parser .add_argument (
68- "-i" ,
69- "--interface" ,
70- dest = "interface" ,
71- help = """Specify the backend CAN interface to use. If left blank,
72- fall back to reading from configuration files.""" ,
73- choices = can .VALID_INTERFACES ,
74- )
75-
7694 parser .add_argument (
7795 "--filter" ,
7896 help = """Comma separated filters can be specified for the given CAN interface:
@@ -83,18 +101,6 @@ def main():
83101 default = "" ,
84102 )
85103
86- parser .add_argument (
87- "-b" , "--bitrate" , type = int , help = """Bitrate to use for the CAN bus."""
88- )
89-
90- parser .add_argument ("--fd" , help = "Activate CAN-FD support" , action = "store_true" )
91-
92- parser .add_argument (
93- "--data_bitrate" ,
94- type = int ,
95- help = """Bitrate to use for the data phase in case of CAN-FD.""" ,
96- )
97-
98104 state_group = parser .add_mutually_exclusive_group (required = False )
99105 state_group .add_argument (
100106 "--active" ,
@@ -105,7 +111,7 @@ def main():
105111 "--passive" , help = "Start the bus as passive." , action = "store_true"
106112 )
107113
108- # print help message when no arguments wre given
114+ # print help message when no arguments were given
109115 if len (sys .argv ) < 2 :
110116 parser .print_help (sys .stderr )
111117 raise SystemExit (errno .EINVAL )
0 commit comments