@@ -12,9 +12,18 @@ def _resolve_config_path(config: str | None) -> str:
1212 if config is None :
1313 # default to repo-root config.yaml
1414 return os .path .join (_repo_root (), 'config.yaml' )
15- if not os .path .isabs (config ):
16- return os .path .join (_repo_root (), config )
17- return config
15+ if os .path .isabs (config ):
16+ return config
17+ # Prefer path relative to the current working directory if it exists
18+ cwd_candidate = os .path .abspath (config )
19+ if os .path .exists (cwd_candidate ):
20+ return cwd_candidate
21+ # Fallback to repo-root relative path
22+ repo_candidate = os .path .join (_repo_root (), config )
23+ if os .path .exists (repo_candidate ):
24+ return repo_candidate
25+ # As last resort, return the CWD-resolved path
26+ return cwd_candidate
1827
1928
2029def _load_devices_from_config (config_path : str ) -> List [Dict [str , Any ]]:
@@ -163,18 +172,23 @@ def cmd_methods(args: argparse.Namespace) -> int:
163172
164173
165174def build_parser () -> argparse .ArgumentParser :
175+ # Define common options so they can appear before or after subcommands
176+ common = argparse .ArgumentParser (add_help = False )
177+ common .add_argument ('--config' , help = 'Path to config.yaml (default: repo root config.yaml)' )
178+
166179 p = argparse .ArgumentParser (description = 'BenchMesh driver manual test tool' )
180+ # Also allow --config before the subcommand
167181 p .add_argument ('--config' , help = 'Path to config.yaml (default: repo root config.yaml)' )
168182 sub = p .add_subparsers (dest = 'cmd' , required = True )
169183
170- p_list = sub .add_parser ('list' , help = 'List devices from config' )
184+ p_list = sub .add_parser ('list' , parents = [ common ], help = 'List devices from config' )
171185 p_list .set_defaults (func = cmd_list )
172186
173- p_methods = sub .add_parser ('methods' , help = 'List public methods of a device driver' )
187+ p_methods = sub .add_parser ('methods' , parents = [ common ], help = 'List public methods of a device driver' )
174188 p_methods .add_argument ('--id' , required = True , help = 'Device id (from config)' )
175189 p_methods .set_defaults (func = cmd_methods )
176190
177- p_call = sub .add_parser ('call' , help = 'Call a method on a device driver' )
191+ p_call = sub .add_parser ('call' , parents = [ common ], help = 'Call a method on a device driver' )
178192 p_call .add_argument ('--id' , required = True , help = 'Device id (from config)' )
179193 p_call .add_argument ('--method' , required = True , help = 'Method name to call' )
180194 p_call .add_argument ('args' , nargs = '*' , help = 'Positional args (auto-coerced)' )
0 commit comments