Skip to content

Commit b2142a6

Browse files
committed
Improve the argparse handling
* Simplify the logic for common options
1 parent f1c795e commit b2142a6

File tree

1 file changed

+28
-36
lines changed

1 file changed

+28
-36
lines changed

src/objdictgen/__main__.py

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -91,45 +91,44 @@ def open_od(fname: TPath|str, validate=True, fix=False) -> Node:
9191
def main(debugopts: DebugOpts, args: Sequence[str]|None = None):
9292
""" Main command dispatcher """
9393

94+
# -- COMMON OPTIONS --
95+
common_opts = argparse.ArgumentParser(add_help=False)
96+
common_opts.add_argument('-D', '--debug', action='store_true',
97+
help="Debug: enable tracebacks on errors")
98+
common_opts.add_argument('--no-color', action='store_true',
99+
help="Disable colored output")
100+
common_opts.add_argument('--novalidate', action='store_true',
101+
help="Don't validate input files")
102+
103+
# -- MAIN PARSER --
94104
parser = argparse.ArgumentParser(
95105
prog=ODG_PROGRAM,
96106
description="""
97107
A tool to read and convert object dictionary files for the
98108
CAN festival library
99109
""",
100110
add_help=True,
111+
parents=[common_opts],
101112
)
102-
103-
# FIXME: New options: new file, add parameter, delete parameter, copy parameter
104-
113+
parser.add_argument('--version', action='version', version='%(prog)s ' + __version__)
105114
subparser = parser.add_subparsers(title="command", dest="command", metavar="command", help="""
106115
Commands
107116
""", required=True)
108117

109-
110-
# -- COMMON --
111-
opt_debug = dict(action='store_true', help="Debug: enable tracebacks on errors")
112-
opt_od = dict(metavar='od', default=None, help="Object dictionary")
113-
opt_novalidate = dict(action='store_true', help="Don't validate input files")
114-
opt_nocolor = dict(action='store_true', help="Disable colored output")
115-
116-
parser.add_argument('--version', action='version', version='%(prog)s ' + __version__)
117-
parser.add_argument('--no-color', action='store_true', help="Disable colored output")
118-
parser.add_argument('-D', '--debug', **opt_debug) # type: ignore[arg-type]
118+
# FIXME: New options: new file, add parameter, delete parameter, copy parameter
119119

120120
# -- HELP --
121-
subp = subparser.add_parser('help', help="""
121+
subp = subparser.add_parser('help', parents=[common_opts], help="""
122122
Show help of all commands
123123
""")
124124
subp.add_argument('subcommand', nargs='?', help="Show help of specific command")
125-
subp.add_argument('-D', '--debug', **opt_debug) # type: ignore[arg-type]
126125

127126
# -- CONVERT --
128-
subp = subparser.add_parser('convert', help="""
127+
subp = subparser.add_parser('convert', parents=[common_opts], help="""
129128
Generate
130129
""", aliases=['gen', 'conv'])
131-
subp.add_argument('od', **opt_od) # type: ignore[arg-type]
132-
subp.add_argument('out', default=None, help="Output file")
130+
subp.add_argument('od', help="Object dictionary")
131+
subp.add_argument('out', help="Output file")
133132
subp.add_argument('-i', '--index', action="append",
134133
help="OD Index to include. Filter out the rest.")
135134
subp.add_argument('-x', '--exclude', action="append", help="OD Index to exclude.")
@@ -142,32 +141,26 @@ def main(debugopts: DebugOpts, args: Sequence[str]|None = None):
142141
help="Store in internal format (json only)")
143142
subp.add_argument('--no-sort', action="store_true",
144143
help="Don't order of parameters in output OD")
145-
subp.add_argument('--novalidate', **opt_novalidate) # type: ignore[arg-type]
146-
subp.add_argument('-D', '--debug', **opt_debug) # type: ignore[arg-type]
147144

148145
# -- DIFF --
149-
subp = subparser.add_parser('diff', help="""
146+
subp = subparser.add_parser('diff', parents=[common_opts], help="""
150147
Compare OD files
151148
""", aliases=['compare'])
152-
subp.add_argument('od1', **opt_od) # type: ignore[arg-type]
153-
subp.add_argument('od2', **opt_od) # type: ignore[arg-type]
149+
subp.add_argument('od1', help="Object dictionary")
150+
subp.add_argument('od2', help="Object dictionary")
154151
subp.add_argument('--show', action="store_true", help="Show difference data")
155152
subp.add_argument('--internal', action="store_true", help="Diff internal object")
156153
subp.add_argument('--data', action="store_true", help="Show difference as data")
157154
subp.add_argument('--raw', action="store_true", help="Show raw difference")
158-
subp.add_argument('--no-color', **opt_nocolor) # type: ignore[arg-type]
159-
subp.add_argument('--novalidate', **opt_novalidate) # type: ignore[arg-type]
160-
subp.add_argument('-D', '--debug', **opt_debug) # type: ignore[arg-type]
161155

162156
# -- EDIT --
163-
subp = subparser.add_parser('edit', help="""
157+
subp = subparser.add_parser('edit', parents=[common_opts], help="""
164158
Edit OD (UI)
165159
""")
166160
subp.add_argument('od', nargs="*", help="Object dictionary")
167-
subp.add_argument('-D', '--debug', **opt_debug) # type: ignore[arg-type]
168161

169162
# -- LIST --
170-
subp = subparser.add_parser('list', help="""
163+
subp = subparser.add_parser('list', parents=[common_opts], help="""
171164
List
172165
""", aliases=['cat'])
173166
subp.add_argument('od', nargs="+", help="Object dictionary")
@@ -180,29 +173,28 @@ def main(debugopts: DebugOpts, args: Sequence[str]|None = None):
180173
subp.add_argument('--unused', action="store_true", help="Include unused profile parameters")
181174
subp.add_argument('--internal', action="store_true", help="Show internal data")
182175
subp.add_argument('--minus', help="Show only parameters that are not in this OD")
183-
subp.add_argument('--no-color', **opt_nocolor) # type: ignore[arg-type]
184-
subp.add_argument('--novalidate', **opt_novalidate) # type: ignore[arg-type]
185-
subp.add_argument('-D', '--debug', **opt_debug) # type: ignore[arg-type]
186176

187177
# -- NETWORK --
188-
subp = subparser.add_parser('network', help="""
178+
subp = subparser.add_parser('network', parents=[common_opts], help="""
189179
Edit network (UI)
190180
""")
191181
subp.add_argument('dir', nargs="?", help="Project directory")
192-
subp.add_argument('-D', '--debug', **opt_debug) # type: ignore[arg-type]
193182

194183
# -- NODELIST --
195-
subp = subparser.add_parser('nodelist', help="""
184+
subp = subparser.add_parser('nodelist', parents=[common_opts], help="""
196185
List project nodes
197186
""")
198187
subp.add_argument('dir', nargs="?", help="Project directory")
199-
subp.add_argument('-D', '--debug', **opt_debug) # type: ignore[arg-type]
200188

201189

202190
# -- COMMON --
203191

204192
# Parse command-line arguments
193+
common = common_opts.parse_known_args(args)[0]
205194
opts = parser.parse_args(args)
195+
# Copy any options prior to the command into the final opts
196+
for k, v in vars(common).items():
197+
setattr(opts, k, v)
206198

207199
# Enable debug mode
208200
if opts.debug:

0 commit comments

Comments
 (0)