Skip to content

Commit 749642f

Browse files
authored
switch from optparse to click (#236) (#394)
1 parent 287d01b commit 749642f

File tree

4 files changed

+107
-273
lines changed

4 files changed

+107
-273
lines changed

examples/compare.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
sys.path.append('..')
44

55
import canmatrix.cli.compare
6-
canmatrix.cli.compare.main()
6+
canmatrix.cli.compare.cli_compare()

examples/convert.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
sys.path.append('..')
44

55
import canmatrix.cli.convert
6-
canmatrix.cli.convert.main()
6+
canmatrix.cli.convert.cli_convert()

src/canmatrix/cli/compare.py

+24-62
Original file line numberDiff line numberDiff line change
@@ -25,75 +25,37 @@
2525
from __future__ import print_function
2626

2727
import logging
28-
import optparse
2928
import sys
3029
import typing
30+
import click
3131

3232
import attr
3333
import canmatrix.compare
3434

3535
logger = logging.getLogger(__name__)
3636

37+
@click.command()
38+
@click.option('-v', '--verbose', 'verbosity', help="Output verbosity", count=True, default=1)
39+
@click.option('-s', '--silent', is_flag=True, default=False, help="don't print status messages to stdout. (only errors)")
40+
@click.option('-f', '--frames', is_flag=True, default=False, help="show list of frames")
41+
@click.option('-c', '--comments', 'check_comments', is_flag=True, default=False, help="ignore changed comments")
42+
@click.option('-a', '--attributes', 'check_attributes', is_flag=True, default=False, help="ignore changed attributes")
43+
@click.option('-t', '--valueTable', 'ignore_valuetables', is_flag=True, default=False, help="ignore changed valuetables")
44+
@click.argument('matrix1', required=1)
45+
@click.argument('matrix2', required=1)
46+
def cli_compare(matrix1, matrix2, verbosity, silent, check_comments, check_attributes, ignore_valuetables, frames): # type: () -> int
47+
"""
48+
canmatrix.cli.compare [options] matrix1 matrix2
49+
50+
matrixX can be any of *.dbc|*.dbf|*.kcd|*.arxml|*.xls(x)|*.sym
51+
"""
3752

38-
def main(): # type: () -> int
3953
import canmatrix.log
4054
canmatrix.log.setup_logger()
4155

42-
usage = """
43-
%prog [options] cancompare matrix1 matrix2
4456

45-
matrixX can be any of *.dbc|*.dbf|*.kcd|*.arxml
46-
"""
4757

48-
parser = optparse.OptionParser(usage=usage)
49-
parser.add_option(
50-
"-s",
51-
dest="silent",
52-
action="store_true",
53-
help="don't print status messages to stdout. (only errors)",
54-
default=False)
55-
parser.add_option(
56-
"-v",
57-
dest="verbosity",
58-
action="count",
59-
help="Output verbosity",
60-
default=0)
61-
parser.add_option(
62-
"-f", "--frames",
63-
dest="frames",
64-
action="store_true",
65-
help="show list of frames",
66-
default=False)
67-
parser.add_option(
68-
"-c", "--comments",
69-
dest="check_comments",
70-
action="store_true",
71-
help="check changed comments",
72-
default=False)
73-
parser.add_option(
74-
"-a", "--attributes",
75-
dest="check_attributes",
76-
action="store_true",
77-
help="check changed attributes",
78-
default=False)
79-
parser.add_option(
80-
"-t", "--valueTable",
81-
dest="ignore_valuetables",
82-
action="store_true",
83-
help="check changed valuetables",
84-
default=False)
85-
86-
(cmdlineOptions, args) = parser.parse_args()
87-
88-
if len(args) < 2:
89-
parser.print_help()
90-
sys.exit(1)
91-
92-
matrix1 = args[0]
93-
matrix2 = args[1]
94-
95-
verbosity = cmdlineOptions.verbosity
96-
if cmdlineOptions.silent:
58+
if silent:
9759
# Only print ERROR messages (ignore import warnings)
9860
verbosity = -1
9961
canmatrix.log.set_log_level(logger, verbosity)
@@ -102,25 +64,25 @@ def main(): # type: () -> int
10264
import canmatrix.formats # due this import we need the import alias for log module
10365

10466
logger.info("Importing " + matrix1 + " ... ")
105-
db1 = next(iter(canmatrix.formats.loadp(matrix1).values()))
67+
db1 = canmatrix.formats.loadp_flat(matrix1)
10668
logger.info("%d Frames found" % (db1.frames.__len__()))
10769

10870
logger.info("Importing " + matrix2 + " ... ")
109-
db2 = next(iter(canmatrix.formats.loadp(matrix2).values()))
71+
db2 = canmatrix.formats.loadp_flat(matrix2)
11072
logger.info("%d Frames found" % (db2.frames.__len__()))
11173

11274
ignore = {} # type: typing.Dict[str, typing.Union[str, bool]]
11375

114-
if not cmdlineOptions.check_comments:
76+
if not check_comments:
11577
ignore["comment"] = "*"
11678

117-
if not cmdlineOptions.check_attributes:
79+
if not check_attributes:
11880
ignore["ATTRIBUTE"] = "*"
11981

120-
if cmdlineOptions.ignore_valuetables:
82+
if ignore_valuetables:
12183
ignore["VALUETABLES"] = True
12284

123-
if cmdlineOptions.frames:
85+
if frames:
12486
only_in_matrix1 = [
12587
frame.name
12688
for frame in db1.frames
@@ -144,4 +106,4 @@ def main(): # type: () -> int
144106

145107
# to be run as module `python -m canmatrix.compare`, NOT as script with argument `canmatrix/compare.py`
146108
if __name__ == '__main__':
147-
sys.exit(main())
109+
sys.exit(cli_compare())

0 commit comments

Comments
 (0)