Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use boost program_options in example #288

Merged
merged 8 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/viam/examples/dial_api_key/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ add_executable(example_dial_api_key
)

target_link_libraries(example_dial_api_key
Boost::program_options
viam-cpp-sdk::viamsdk
)

Expand Down
37 changes: 25 additions & 12 deletions src/viam/examples/dial_api_key/example_dial_api_key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <vector>

#include <boost/optional.hpp>
#include <boost/program_options.hpp>
#include <grpcpp/channel.h>
#include <grpcpp/client_context.h>
#include <grpcpp/grpcpp.h>
Expand All @@ -24,21 +25,33 @@
using viam::robot::v1::Status;
using namespace viam::sdk;

int main() {
const char* uri = "<your robot URI here>";
DialOptions dial_options;
std::string type = "api-key";
std::string entity = "<your api key id>";
std::string payload = "<your api key value>";
dial_options.set_entity(entity);
Credentials credentials(type, payload);
dial_options.set_credentials(credentials);
boost::optional<DialOptions> opts(dial_options);
std::string address(uri);
namespace po = boost::program_options;

int main(int argc, char* argv[]) {
po::options_description desc("Allowed options");
desc.add_options()("help", "List options and exit")(
"uri", po::value<std::string>(), "URI of robot")(
"entity", po::value<std::string>(), "api key ID")(
"api-key", po::value<std::string>(), "api key secret");
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
if (vm.count("help")) {
std::cout << desc << std::endl;
return 0;
}
boost::optional<DialOptions> opts;
if (vm.count("entity") && vm.count("api-key")) {
DialOptions dial_options;
dial_options.set_entity(vm["entity"].as<std::string>());
Credentials credentials("api-key", vm["api-key"].as<std::string>());
dial_options.set_credentials(credentials);
opts = dial_options;
}
Options options(1, opts);

// connect to robot, ensure we can refresh it
std::shared_ptr<RobotClient> robot = RobotClient::at_address(address, options);
std::shared_ptr<RobotClient> robot =
RobotClient::at_address(vm["uri"].as<std::string>(), options);

// ensure we can query resources
std::vector<Name> resource_names = robot->resource_names();
Expand Down