grpc_cli.cc
Go to the documentation of this file.
1 /*
2 
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /*
20  A command line tool to talk to a grpc server.
21  Run `grpc_cli help` command to see its usage information.
22 
23  Example of talking to grpc interop server:
24  grpc_cli call localhost:50051 UnaryCall "response_size:10" \
25  --protofiles=src/proto/grpc/testing/test.proto \
26  --channel_creds_type=insecure
27 
28  Options:
29  1. --protofiles, use this flag to provide proto files if the server does
30  does not have the reflection service.
31  2. --proto_path, if your proto file is not under current working directory,
32  use this flag to provide a search root. It should work similar to the
33  counterpart in protoc. This option is valid only when protofiles is
34  provided.
35  3. --metadata specifies metadata to be sent to the server, such as:
36  --metadata="MyHeaderKey1:Value1:MyHeaderKey2:Value2"
37  4. --channel_creds_type, whether to use tls, insecure or platform-specific
38  options.
39  5. --use_auth, if set to true, attach a GoogleDefaultCredentials to the call
40  6. --infile, input filename (defaults to stdin)
41  7. --outfile, output filename (defaults to stdout)
42  8. --binary_input, use the serialized request as input. The serialized
43  request can be generated by calling something like:
44  protoc --proto_path=src/proto/grpc/testing/ \
45  --encode=grpc.testing.SimpleRequest \
46  src/proto/grpc/testing/messages.proto \
47  < input.txt > input.bin
48  If this is used and no proto file is provided in the argument list, the
49  method string has to be exact in the form of /package.service/method.
50  9. --binary_output, use binary format response as output, it can
51  be later decoded using protoc:
52  protoc --proto_path=src/proto/grpc/testing/ \
53  --decode=grpc.testing.SimpleResponse \
54  src/proto/grpc/testing/messages.proto \
55  < output.bin > output.txt
56  10. --default_service_config, optional default service config to use
57  on the channel. Note that this may be ignored if the name resolver
58  returns a service config.
59  11. --display_peer_address, on CallMethod commands, log the peer socket
60  address of the connection that each RPC is made on to stderr.
61 */
62 
63 #include <fstream>
64 #include <functional>
65 #include <iostream>
66 
67 #include "absl/flags/flag.h"
68 
69 #include <grpcpp/support/config.h>
70 
74 
75 ABSL_FLAG(std::string, outfile, "", "Output file (default is stdout)");
76 
77 static bool SimplePrint(const std::string& outfile, const std::string& output) {
78  if (outfile.empty()) {
79  std::cout << output << std::flush;
80  } else {
81  std::ofstream output_file(outfile, std::ios::app | std::ios::binary);
82  output_file << output << std::flush;
83  output_file.close();
84  }
85  return true;
86 }
87 
88 int main(int argc, char** argv) {
89  grpc::testing::InitTest(&argc, &argv, true);
90 
92  argc, const_cast<const char**>(argv), grpc::testing::CliCredentials(),
93  std::bind(SimplePrint, absl::GetFlag(FLAGS_outfile),
94  std::placeholders::_1));
95 }
grpc::testing::InitTest
void InitTest(int *argc, char ***argv, bool remove_flags)
Definition: test_config_cc.cc:28
grpc::testing::GrpcToolMainLib
int GrpcToolMainLib(int argc, const char **argv, const CliCredentials &cred, const GrpcToolOutputCallback &callback)
Definition: grpc_tool.cc:295
ABSL_FLAG
ABSL_FLAG(std::string, outfile, "", "Output file (default is stdout)")
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc_tool.h
main
int main(int argc, char **argv)
Definition: grpc_cli.cc:88
gmock_output_test.output
output
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
grpc::testing::CliCredentials
Definition: cli_credentials.h:28
absl::GetFlag
ABSL_MUST_USE_RESULT T GetFlag(const absl::Flag< T > &flag)
Definition: abseil-cpp/absl/flags/flag.h:98
config.h
SimplePrint
static bool SimplePrint(const std::string &outfile, const std::string &output)
Definition: grpc_cli.cc:77
test_config.h
outfile
FILE * outfile
Definition: fuzz_diff.c:16
cli_credentials.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:47