Skip to content

Commit

Permalink
use boost program_options in example (#288)
Browse files Browse the repository at this point in the history
Co-authored-by: lia <[email protected]>
  • Loading branch information
abe-winter and lia-viam authored Sep 23, 2024
1 parent f4259e1 commit 9b2a576
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
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

0 comments on commit 9b2a576

Please sign in to comment.