From 9b77138b71e04bb127ec1fb3ae280bca33c59ae1 Mon Sep 17 00:00:00 2001 From: RomantsovS Date: Mon, 23 Dec 2024 14:39:13 -0800 Subject: [PATCH] examples/cpp/route_guide add missing command line parsing (#37857) The gRPC basic turorial says to call the following command to run the server `./route_guide_server --db_path=path/to/route_guide_db.json` But the server and client do not parse command line arguments and they always use default value `route_guide_db.json`. Thus the example only works if we run it from `examples/cpp/route_guide` dir. Otherwise we get the following error: `helper.cc:49] Failed to open route_guide_db.json Aborted (core dumped)` Add `absl::ParseCommandLine(argc, argv);` to both server and client `main` similar to `route_guide_callback_server.cc` Closes #37857 PiperOrigin-RevId: 709154601 --- examples/cpp/route_guide/BUILD | 2 ++ examples/cpp/route_guide/Makefile | 4 ++-- examples/cpp/route_guide/route_guide_client.cc | 2 ++ examples/cpp/route_guide/route_guide_server.cc | 2 ++ 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/examples/cpp/route_guide/BUILD b/examples/cpp/route_guide/BUILD index 2ea012ca2ee62..9a19d895a8c92 100644 --- a/examples/cpp/route_guide/BUILD +++ b/examples/cpp/route_guide/BUILD @@ -39,6 +39,7 @@ cc_binary( ":route_guide_helper", "//:grpc++", "//examples/protos:route_guide", + "@com_google_absl//absl/flags:parse", ], ) @@ -53,6 +54,7 @@ cc_binary( ":route_guide_helper", "//:grpc++", "//examples/protos:route_guide", + "@com_google_absl//absl/flags:parse", ], ) diff --git a/examples/cpp/route_guide/Makefile b/examples/cpp/route_guide/Makefile index ae254230f68d6..0b4940dd4c329 100644 --- a/examples/cpp/route_guide/Makefile +++ b/examples/cpp/route_guide/Makefile @@ -25,7 +25,7 @@ PROTOBUF_UTF8_RANGE_LINK_LIBS = -lutf8_validity HOST_SYSTEM = $(shell uname | cut -f 1 -d_) SYSTEM ?= $(HOST_SYSTEM) CXX = g++ -CPPFLAGS += `pkg-config --cflags protobuf grpc` +CPPFLAGS += `pkg-config --cflags protobuf grpc absl_flags absl_flags_parse` CXXFLAGS += -std=c++17 ifeq ($(SYSTEM),Darwin) LDFLAGS += -L/usr/local/lib `pkg-config --libs --static protobuf grpc++ $(PROTOBUF_ABSL_DEPS)`\ @@ -35,7 +35,7 @@ LDFLAGS += -L/usr/local/lib `pkg-config --libs --static protobuf grpc++ $(PROTOB -lgrpc++_reflection\ -ldl else -LDFLAGS += -L/usr/local/lib `pkg-config --libs --static protobuf grpc++ $(PROTOBUF_ABSL_DEPS)`\ +LDFLAGS += -L/usr/local/lib `pkg-config --libs --static protobuf grpc++ absl_flags absl_flags_parse $(PROTOBUF_ABSL_DEPS)`\ $(PROTOBUF_UTF8_RANGE_LINK_LIBS) \ -pthread\ -Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed\ diff --git a/examples/cpp/route_guide/route_guide_client.cc b/examples/cpp/route_guide/route_guide_client.cc index 73983f458d934..af9f2fdda3069 100644 --- a/examples/cpp/route_guide/route_guide_client.cc +++ b/examples/cpp/route_guide/route_guide_client.cc @@ -29,6 +29,7 @@ #include #include +#include "absl/flags/parse.h" #include "helper.h" #ifdef BAZEL_BUILD #include "examples/protos/route_guide.grpc.pb.h" @@ -216,6 +217,7 @@ class RouteGuideClient { }; int main(int argc, char** argv) { + absl::ParseCommandLine(argc, argv); // Expect only arg: --db_path=path/to/route_guide_db.json. std::string db = routeguide::GetDbFileContent(argc, argv); RouteGuideClient guide( diff --git a/examples/cpp/route_guide/route_guide_server.cc b/examples/cpp/route_guide/route_guide_server.cc index 7a6a1c8ecaa13..b037ca0815aa9 100644 --- a/examples/cpp/route_guide/route_guide_server.cc +++ b/examples/cpp/route_guide/route_guide_server.cc @@ -29,6 +29,7 @@ #include #include +#include "absl/flags/parse.h" #include "helper.h" #ifdef BAZEL_BUILD #include "examples/protos/route_guide.grpc.pb.h" @@ -182,6 +183,7 @@ void RunServer(const std::string& db_path) { } int main(int argc, char** argv) { + absl::ParseCommandLine(argc, argv); // Expect only arg: --db_path=path/to/route_guide_db.json. std::string db = routeguide::GetDbFileContent(argc, argv); RunServer(db);