Skip to content

Commit 1a5c85a

Browse files
fix(tools/driver_cli): accept --config globally and improve relative path resolution
- Allow --config before or after subcommand via common parent and top-level - Resolve config path from CWD first, then repo root, handle ../ paths Co-authored-by: openhands <openhands@all-hands.dev>
1 parent 2246ab6 commit 1a5c85a

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,13 @@ Examples
147147
- Call a method with kwargs (JSON object):
148148
python -m benchmesh_service.tools.driver_cli call --id tenmapsu-1 --method set_output true --kwargs '{"ch":1}' --config config.yaml
149149

150+
Other Examples:
151+
- python -m benchmesh_service.tools.driver_cli call --id tenmapsu-1 --method identify --config config.yaml
152+
- python -m benchmesh_service.tools.driver_cli call --id tenmapsu-1 --method read_status --config config.yaml
153+
- python -m benchmesh_service.tools.driver_cli call --id tenmapsu-1 --method set_output --config config.yaml true
154+
- python -m benchmesh_service.tools.driver_cli call --id spm-1 --method poll_status --config config.yaml
155+
- ython -m benchmesh_service.tools.driver_cli call --id some-id --method set_voltage 5.0 --kwargs '{"ch":1}' --config config.yaml
156+
150157
Notes
151158
- Only the targeted device is instantiated to keep testing isolated.
152159
- Results are printed as JSON when the return type is a collection; otherwise as text.

benchmesh-serial-service/src/benchmesh_service/tools/driver_cli.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2029
def _load_devices_from_config(config_path: str) -> List[Dict[str, Any]]:
@@ -163,18 +172,23 @@ def cmd_methods(args: argparse.Namespace) -> int:
163172

164173

165174
def 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

Comments
 (0)