Skip to content

Commit 4dfa6a9

Browse files
Copilotmsis
andcommitted
Fix Run() to use actual CMOOSApp::Run(app_name, argc, argv) signature
Co-authored-by: msis <577139+msis@users.noreply.github.com>
1 parent cdbf00e commit 4dfa6a9

4 files changed

Lines changed: 54 additions & 43 deletions

File tree

Documentation/examples/simpleapp.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ def main():
6565
app.set_on_new_mail_callback(on_new_mail)
6666
app.set_iterate_callback(iterate)
6767

68-
# Standard MOOS pattern: Run with mission file
68+
# Standard MOOS pattern: app_name and mission file
6969
# The mission file contains ServerHost, ServerPort, and Community
70-
# app.run('simpleapp.moos')
70+
# app.run('pymoos_simple_app', 'simpleapp.moos')
7171

72-
# Alternative: Run without mission file (provide server, port, name manually)
73-
app.run('localhost', 9000, 'pymoos_simple_app')
72+
# Alternative: Run without mission file (provide server and port explicitly)
73+
app.run('pymoos_simple_app', '', 'localhost', 9000)
7474

7575
if __name__ == "__main__":
7676
main()

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,16 @@ app.set_on_start_up_callback(on_startup)
7373
app.set_on_connect_to_server_callback(on_connect_to_server)
7474
app.set_iterate_callback(iterate)
7575

76-
# Standard MOOS pattern: Run with mission file
76+
# Standard MOOS pattern: Run with app name and mission file
7777
# ServerHost, ServerPort, and Community are read from the mission file
78-
app.run('my_mission.moos')
78+
app.run('my_app', 'my_mission.moos')
7979

8080
# Alternative: Run without mission file (provide connection details manually)
81-
# app.run('localhost', 9000, 'my_app')
81+
# app.run('my_app', '', 'localhost', 9000)
8282
```
8383

84-
The `app` class automatically reads `ServerHost`, `ServerPort`, `AppTick`, and `CommsTick` from the mission file.
84+
The `app` class uses the standard CMOOSApp::Run(app_name, argc, argv) pattern internally.
85+
When you provide a mission file, it automatically reads `ServerHost`, `ServerPort`, `AppTick`, and `CommsTick`.
8586
For custom configuration parameters, use:
8687
- `get_configuration_string(param)` - Read string parameters
8788
- `get_configuration_double(param)` - Read numeric parameters

src/pyMOOS.cpp

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -229,25 +229,37 @@ class AppWrapper : public CMOOSApp {
229229
// Clean shutdown
230230
}
231231

232-
// Standard MOOS pattern: Run with mission file (reads server/port/name from file)
233-
bool Run(const std::string & sMissionFile) {
234-
SetMissionFile(sMissionFile);
235-
return BASE::Run();
236-
}
237-
238-
// Alternative: Run with explicit parameters (for when mission file is not used)
239-
bool Run(const std::string & sServer, int Port, const std::string & sMyName) {
240-
SetAppName(sMyName);
241-
SetServer(sServer, Port);
242-
return BASE::Run();
243-
}
244-
245-
// Run with explicit parameters and mission file
246-
bool Run(const std::string & sServer, int Port, const std::string & sMyName, const std::string & sMissionFile) {
247-
SetAppName(sMyName);
248-
SetServer(sServer, Port);
249-
SetMissionFile(sMissionFile);
250-
return BASE::Run();
232+
// Python-friendly wrapper that creates command-line arguments for CMOOSApp::Run()
233+
// Standard MOOS pattern: Run(app_name, mission_file, [additional args])
234+
bool Run(const std::string & sAppName, const std::string & sMissionFile = "",
235+
const std::string & sServerHost = "", int nServerPort = 0) {
236+
237+
// Build argc/argv from parameters
238+
std::vector<std::string> args;
239+
args.push_back(sAppName); // argv[0] is typically the program name
240+
241+
if (!sMissionFile.empty()) {
242+
args.push_back(sMissionFile);
243+
}
244+
245+
if (!sServerHost.empty()) {
246+
args.push_back("--moos_host=" + sServerHost);
247+
}
248+
249+
if (nServerPort > 0) {
250+
args.push_back("--moos_port=" + std::to_string(nServerPort));
251+
}
252+
253+
// Convert to argc/argv format
254+
int argc = static_cast<int>(args.size());
255+
std::vector<char*> argv;
256+
for (auto& arg : args) {
257+
argv.push_back(const_cast<char*>(arg.c_str()));
258+
}
259+
argv.push_back(nullptr); // NULL terminator
260+
261+
// Call the real CMOOSApp::Run(app_name, argc, argv)
262+
return BASE::Run(sAppName, argc, argv.data());
251263
}
252264

253265
//we can support vectors of objects by not lists so
@@ -759,19 +771,17 @@ PYBIND11_MODULE(pymoos, m) {
759771
py::class_<MOOS::AppWrapper, CMOOSApp>(m, "app")
760772
.def(py::init<>())
761773

762-
.def("run", static_cast<bool(MOOS::AppWrapper::*)(const std::string&)>(&MOOS::AppWrapper::Run),
763-
"Run the MOOS application with mission file. "
764-
"Server, port, and app name are read from the mission file.",
765-
py::arg("mission_file"))
766-
767-
.def("run", static_cast<bool(MOOS::AppWrapper::*)(const std::string&, int, const std::string&)>(&MOOS::AppWrapper::Run),
768-
"Run the MOOS application with explicit server, port, and name. "
769-
"Use this when not using a mission file.",
770-
py::arg("server"), py::arg("port"), py::arg("name"))
771-
772-
.def("run", static_cast<bool(MOOS::AppWrapper::*)(const std::string&, int, const std::string&, const std::string&)>(&MOOS::AppWrapper::Run),
773-
"Run the MOOS application with both explicit parameters and mission file.",
774-
py::arg("server"), py::arg("port"), py::arg("name"), py::arg("mission_file"))
774+
.def("run", &MOOS::AppWrapper::Run,
775+
"Run the MOOS application. Uses standard MOOS Run(app_name, argc, argv) pattern.\n"
776+
"Parameters:\n"
777+
" app_name: Name of the application\n"
778+
" mission_file: Optional path to .moos mission file (reads ServerHost, ServerPort, etc.)\n"
779+
" server_host: Optional server hostname (overrides mission file)\n"
780+
" server_port: Optional server port (overrides mission file)",
781+
py::arg("app_name"),
782+
py::arg("mission_file") = "",
783+
py::arg("server_host") = "",
784+
py::arg("server_port") = 0)
775785

776786
.def("fetch", &MOOS::AppWrapper::FetchMailAsVector,
777787
"Fetch incoming mail as a vector.")

tests/test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ def iterate():
389389
a.set_iterate_callback(iterate)
390390

391391
# Run the app (will block until iterate returns False)
392-
a.run('localhost', 9000, 'test_app_basic')
392+
a.run('test_app_basic', '', 'localhost', 9000)
393393

394394
# Verify callbacks were called
395395
self.assertTrue(self.startup_called)
@@ -435,7 +435,7 @@ def iterate():
435435
a.set_iterate_callback(iterate)
436436

437437
# Run the app
438-
a.run('localhost', 9000, 'test_app_messaging')
438+
a.run('test_app_messaging', '', 'localhost', 9000)
439439

440440
# Verify we received our message
441441
self.assertTrue(self.received_mail)
@@ -507,7 +507,7 @@ def iterate():
507507
a.set_iterate_callback(iterate)
508508

509509
# Run the app with mission file
510-
a.run('localhost', 9000, 'test_app_config', mission_file)
510+
a.run('test_app_config', mission_file, 'localhost', 9000)
511511

512512
# Verify configuration was read
513513
self.assertTrue(self.config_read)

0 commit comments

Comments
 (0)