@@ -91,45 +91,44 @@ def open_od(fname: TPath|str, validate=True, fix=False) -> Node:
91
91
def main (debugopts : DebugOpts , args : Sequence [str ]| None = None ):
92
92
""" Main command dispatcher """
93
93
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 --
94
104
parser = argparse .ArgumentParser (
95
105
prog = ODG_PROGRAM ,
96
106
description = """
97
107
A tool to read and convert object dictionary files for the
98
108
CAN festival library
99
109
""" ,
100
110
add_help = True ,
111
+ parents = [common_opts ],
101
112
)
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__ )
105
114
subparser = parser .add_subparsers (title = "command" , dest = "command" , metavar = "command" , help = """
106
115
Commands
107
116
""" , required = True )
108
117
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
119
119
120
120
# -- HELP --
121
- subp = subparser .add_parser ('help' , help = """
121
+ subp = subparser .add_parser ('help' , parents = [ common_opts ], help = """
122
122
Show help of all commands
123
123
""" )
124
124
subp .add_argument ('subcommand' , nargs = '?' , help = "Show help of specific command" )
125
- subp .add_argument ('-D' , '--debug' , ** opt_debug ) # type: ignore[arg-type]
126
125
127
126
# -- CONVERT --
128
- subp = subparser .add_parser ('convert' , help = """
127
+ subp = subparser .add_parser ('convert' , parents = [ common_opts ], help = """
129
128
Generate
130
129
""" , 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" )
133
132
subp .add_argument ('-i' , '--index' , action = "append" ,
134
133
help = "OD Index to include. Filter out the rest." )
135
134
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):
142
141
help = "Store in internal format (json only)" )
143
142
subp .add_argument ('--no-sort' , action = "store_true" ,
144
143
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]
147
144
148
145
# -- DIFF --
149
- subp = subparser .add_parser ('diff' , help = """
146
+ subp = subparser .add_parser ('diff' , parents = [ common_opts ], help = """
150
147
Compare OD files
151
148
""" , 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" )
154
151
subp .add_argument ('--show' , action = "store_true" , help = "Show difference data" )
155
152
subp .add_argument ('--internal' , action = "store_true" , help = "Diff internal object" )
156
153
subp .add_argument ('--data' , action = "store_true" , help = "Show difference as data" )
157
154
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]
161
155
162
156
# -- EDIT --
163
- subp = subparser .add_parser ('edit' , help = """
157
+ subp = subparser .add_parser ('edit' , parents = [ common_opts ], help = """
164
158
Edit OD (UI)
165
159
""" )
166
160
subp .add_argument ('od' , nargs = "*" , help = "Object dictionary" )
167
- subp .add_argument ('-D' , '--debug' , ** opt_debug ) # type: ignore[arg-type]
168
161
169
162
# -- LIST --
170
- subp = subparser .add_parser ('list' , help = """
163
+ subp = subparser .add_parser ('list' , parents = [ common_opts ], help = """
171
164
List
172
165
""" , aliases = ['cat' ])
173
166
subp .add_argument ('od' , nargs = "+" , help = "Object dictionary" )
@@ -180,29 +173,28 @@ def main(debugopts: DebugOpts, args: Sequence[str]|None = None):
180
173
subp .add_argument ('--unused' , action = "store_true" , help = "Include unused profile parameters" )
181
174
subp .add_argument ('--internal' , action = "store_true" , help = "Show internal data" )
182
175
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]
186
176
187
177
# -- NETWORK --
188
- subp = subparser .add_parser ('network' , help = """
178
+ subp = subparser .add_parser ('network' , parents = [ common_opts ], help = """
189
179
Edit network (UI)
190
180
""" )
191
181
subp .add_argument ('dir' , nargs = "?" , help = "Project directory" )
192
- subp .add_argument ('-D' , '--debug' , ** opt_debug ) # type: ignore[arg-type]
193
182
194
183
# -- NODELIST --
195
- subp = subparser .add_parser ('nodelist' , help = """
184
+ subp = subparser .add_parser ('nodelist' , parents = [ common_opts ], help = """
196
185
List project nodes
197
186
""" )
198
187
subp .add_argument ('dir' , nargs = "?" , help = "Project directory" )
199
- subp .add_argument ('-D' , '--debug' , ** opt_debug ) # type: ignore[arg-type]
200
188
201
189
202
190
# -- COMMON --
203
191
204
192
# Parse command-line arguments
193
+ common = common_opts .parse_known_args (args )[0 ]
205
194
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 )
206
198
207
199
# Enable debug mode
208
200
if opts .debug :
0 commit comments