Skip to content

Commit

Permalink
examples' command line arguments aligned with those in rust. Failures…
Browse files Browse the repository at this point in the history
… in shared memory examples fixed (#844)

* align examples with rust

* fix z_get_shm and z_queryable_shm examples

* more fixes

* add support for long named options
  • Loading branch information
DenisBiryukov91 authored Dec 6, 2024
1 parent b00c9cc commit 9ced08a
Show file tree
Hide file tree
Showing 29 changed files with 722 additions and 948 deletions.
63 changes: 49 additions & 14 deletions examples/parse_args.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,37 @@

#define COMMON_HELP \
"\
-c <CONFIG> (optional, string): The path to a configuration file for the session. If this option isn't passed, the default configuration will be used.\n\
-m <MODE> (optional, string, default='peer'): The zenoh session mode. [possible values: peer, client, router]\n\
-e <CONNECT> (optional, string): endpoint to connect to. Repeat option to pass multiple endpoints. If none are given, endpoints will be discovered through multicast-scouting if it is enabled.\n\
-c, --config <CONFIG> (optional, string): The path to a configuration file for the session. If this option isn't passed, the default configuration will be used.\n\
-m, --mode <MODE> (optional, string, default='peer'): The zenoh session mode. [possible values: peer, client, router]\n\
-e, --connect <CONNECT> (optional, string): endpoint to connect to. Repeat option to pass multiple endpoints. If none are given, endpoints will be discovered through multicast-scouting if it is enabled.\n\
e.g.: '-e tcp/192.168.1.1:7447'\n\
-l <LISTEN> (optional, string): locator to listen on. Repeat option to pass multiple locators. If none are given, the default configuration will be used.\n\
-l, --listen <LISTEN> (optional, string): locator to listen on. Repeat option to pass multiple locators. If none are given, the default configuration will be used.\n\
e.g.: '-l tcp/192.168.1.1:7447'\n\
--no-multicast-scouting (optional): By default zenohd replies to multicast scouting messages for being discovered by peers and clients. This option disables this feature.\n\
-h, --help: print help\n\
"
#define _Z_PARSE_ARG(VALUE, ID_SHORT, ID_LONG, FUNC, DEFAULT_VALUE) \
do { \
const char* arg_val = parse_opt(argc, argv, ID_SHORT, true); \
if (!arg_val) { \
arg_val = parse_opt(argc, argv, ID_LONG, true); \
} \
if (!arg_val) { \
VALUE = DEFAULT_VALUE; \
} else { \
VALUE = FUNC(arg_val); \
} \
} while (0)

#define _Z_CHECK_HELP \
do { \
if (parse_opt(argc, argv, "h", false) || parse_opt(argc, argv, "help", false)) { \
print_help(); \
exit(1); \
} \
} while (0)

#define _Z_CHECK_FLAG(ID) (parse_opt(argc, argv, ID, false) != NULL)

/**
* Parse an option of format `-f`, `--flag`, `-f <value>` or `--flag <value>` from `argv`. If found, the option and its
Expand Down Expand Up @@ -136,17 +159,18 @@ char** parse_pos_args(const int argc, char** argv, const size_t nb_args) {
* @param config: address of an owned zenoh configuration
* @param config_key: zenoh configuration key under which the parsed values will be inserted
*/
void parse_zenoh_json_list_config(int argc, char** argv, const char* opt, const char* config_key,
z_owned_config_t* config) {
void parse_zenoh_json_list_config(int argc, char** argv, const char* opt_short, const char* opt_long,
const char* config_key, z_owned_config_t* config) {
char* buf = (char*)calloc(1, sizeof(char));
const char* value = parse_opt(argc, argv, opt, true);
const char* value;
_Z_PARSE_ARG(value, opt_short, opt_long, (const char*), NULL);
while (value) {
size_t len_newbuf = strlen(buf) + strlen(value) + 4; // value + quotes + comma + nullbyte
char* newbuf = (char*)malloc(len_newbuf);
snprintf(newbuf, len_newbuf, "%s'%s',", buf, value);
free(buf);
buf = newbuf;
value = parse_opt(argc, argv, opt, true);
_Z_PARSE_ARG(value, opt_short, opt_long, (const char*), NULL);
}
size_t buflen = strlen(buf);
if (buflen > 0) {
Expand Down Expand Up @@ -179,15 +203,17 @@ void parse_zenoh_json_list_config(int argc, char** argv, const char* opt, const
* @param config: address of an owned zenoh configuration
*/
void parse_zenoh_common_args(const int argc, char** argv, z_owned_config_t* config) {
// -c: A configuration file.
const char* config_file = parse_opt(argc, argv, "c", true);
// -c, --config: A configuration file.
const char* config_file;
_Z_PARSE_ARG(config_file, "c", "config", (const char*), NULL);
if (config_file) {
zc_config_from_file(config, config_file);
} else {
z_config_default(config);
}
// -m: The Zenoh session mode [default: peer].
const char* mode = parse_opt(argc, argv, "m", true);
const char* mode;
_Z_PARSE_ARG(mode, "m", "mode", (const char*), NULL);
if (mode) {
size_t buflen = strlen(mode) + 3; // mode + quotes + nullbyte
char* buf = (char*)malloc(buflen);
Expand All @@ -203,11 +229,11 @@ void parse_zenoh_common_args(const int argc, char** argv, z_owned_config_t* conf
free(buf);
}
// -e: Endpoint to connect to. Can be repeated
parse_zenoh_json_list_config(argc, argv, "e", Z_CONFIG_CONNECT_KEY, config);
parse_zenoh_json_list_config(argc, argv, "e", "connect", Z_CONFIG_CONNECT_KEY, config);
// -l: Endpoint to listen on. Can be repeated
parse_zenoh_json_list_config(argc, argv, "l", Z_CONFIG_LISTEN_KEY, config);
parse_zenoh_json_list_config(argc, argv, "l", "listen", Z_CONFIG_LISTEN_KEY, config);
// --no-multicast-scrouting: Disable the multicast-based scouting mechanism.
const char* no_multicast_scouting = parse_opt(argc, argv, "no-multicast-scouting", false);
bool no_multicast_scouting = _Z_CHECK_FLAG("no-multicast-scouting");
if (no_multicast_scouting &&
zc_config_insert_json5(z_loan_mut(*config), Z_CONFIG_MULTICAST_SCOUTING_KEY, "false") < 0) {
printf("Couldn't disable multicast-scouting.\n");
Expand All @@ -227,3 +253,12 @@ z_query_target_t parse_query_target(const char* arg) {
exit(-1);
}
}

z_priority_t parse_priority(const char* arg) {
int p = atoi(arg);
if (p < Z_PRIORITY_INTERACTIVE_HIGH || p > Z_PRIORITY_BACKGROUND) {
printf("Unsupported priority value [%s]\n", arg);
exit(-1);
}
return (z_priority_t)p;
}
20 changes: 6 additions & 14 deletions examples/z_delete.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#define DEFAULT_KEYEXPR "demo/example/zenoh-c-put"

struct args_t {
char* keyexpr; // -k
char* keyexpr; // -k, --key
};
struct args_t parse_args(int argc, char** argv, z_owned_config_t* config);

Expand Down Expand Up @@ -54,23 +54,15 @@ void print_help() {
"\
Usage: z_delete [OPTIONS]\n\n\
Options:\n\
-k <KEY> (optional, string, default='%s'): The key expression to write to\n",
-k, --key <KEY> (optional, string, default='%s'): The key expression to write to\n",
DEFAULT_KEYEXPR);
printf(COMMON_HELP);
printf(
"\
-h: print help\n");
}

struct args_t parse_args(int argc, char** argv, z_owned_config_t* config) {
if (parse_opt(argc, argv, "h", false)) {
print_help();
exit(1);
}
const char* keyexpr = parse_opt(argc, argv, "k", true);
if (!keyexpr) {
keyexpr = DEFAULT_KEYEXPR;
}
_Z_CHECK_HELP;
struct args_t args;
_Z_PARSE_ARG(args.keyexpr, "k", "key", (char*), (char*)DEFAULT_KEYEXPR);
parse_zenoh_common_args(argc, argv, config);
const char* arg = check_unknown_opts(argc, argv);
if (arg) {
Expand All @@ -84,5 +76,5 @@ struct args_t parse_args(int argc, char** argv, z_owned_config_t* config) {
exit(-1);
}
free(pos_args);
return (struct args_t){.keyexpr = (char*)keyexpr};
return args;
}
55 changes: 18 additions & 37 deletions examples/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
#define DEFAULT_TIMEOUT_MS 10000

struct args_t {
char* selector; // -s
char* value; // -v
z_query_target_t target; // -t
uint64_t timeout_ms; // -o
char* selector; // -s, --selector
char* value; // -p, --payload
z_query_target_t target; // -t, --target
uint64_t timeout_ms; // -o, --timeout
};
struct args_t parse_args(int argc, char** argv, z_owned_config_t* config);

Expand All @@ -45,8 +45,8 @@ int main(int argc, char** argv) {

z_view_keyexpr_t keyexpr;

if (z_view_keyexpr_from_str(&keyexpr, ke) < 0) {
printf("%s is not a valid key expression", ke);
if (z_view_keyexpr_from_substr(&keyexpr, ke, ke_len) < 0) {
printf("%.*s is not a valid key expression", (int)ke_len, ke);
exit(-1);
}

Expand Down Expand Up @@ -105,40 +105,22 @@ void print_help() {
"\
Usage: z_get [OPTIONS]\n\n\
Options:\n\
-s <SELECTOR> (optional, string, default='%s'): The selection of resources to query\n\
-p <PAYLOAD> (optional, string): An optional value to put in the query\n\
-t <TARGET> (optional, BEST_MATCHING | ALL | ALL_COMPLETE): Query target\n\
-o <TIMEOUT_MS> (optional, number, default = '%d'): Query timeout in milliseconds\n",
-s, --selector <SELECTOR> (optional, string, default='%s'): The selection of resources to query\n\
-p, --payload <PAYLOAD> (optional, string): An optional value to put in the query\n\
-t, --target <TARGET> (optional, BEST_MATCHING | ALL | ALL_COMPLETE): Query target\n\
-o, --timeout <TIMEOUT_MS> (optional, number, default = '%d'): Query timeout in milliseconds\n",
DEFAULT_SELECTOR, DEFAULT_TIMEOUT_MS);
printf(COMMON_HELP);
printf(
"\
-h: print help\n");
}

struct args_t parse_args(int argc, char** argv, z_owned_config_t* config) {
if (parse_opt(argc, argv, "h", false)) {
print_help();
exit(1);
}
const char* selector = parse_opt(argc, argv, "s", true);
if (!selector) {
selector = DEFAULT_SELECTOR;
}
const char* value = parse_opt(argc, argv, "p", true);
if (!value) {
value = DEFAULT_VALUE;
}
const char* timeout_arg = parse_opt(argc, argv, "o", true);
uint64_t timeout_ms = DEFAULT_TIMEOUT_MS;
if (timeout_arg) {
timeout_ms = atoi(timeout_arg);
}
const char* target_arg = parse_opt(argc, argv, "t", true);
z_query_target_t target = z_query_target_default();
if (target_arg) {
target = parse_query_target(target_arg);
}
_Z_CHECK_HELP;
struct args_t args;
_Z_PARSE_ARG(args.selector, "s", "selector", (char*), (char*)DEFAULT_SELECTOR);
_Z_PARSE_ARG(args.value, "p", "payload", (char*), (char*)DEFAULT_VALUE);
_Z_PARSE_ARG(args.timeout_ms, "o", "timeout", atoi, DEFAULT_TIMEOUT_MS);
_Z_PARSE_ARG(args.target, "t", "target", parse_query_target, z_query_target_default());

parse_zenoh_common_args(argc, argv, config);
const char* arg = check_unknown_opts(argc, argv);
if (arg) {
Expand All @@ -152,6 +134,5 @@ struct args_t parse_args(int argc, char** argv, z_owned_config_t* config) {
exit(-1);
}
free(pos_args);
return (struct args_t){
.selector = (char*)selector, .value = (char*)value, .timeout_ms = timeout_ms, .target = target};
return args;
}
32 changes: 16 additions & 16 deletions examples/z_get_liveliness.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
#include "zenoh.h"

#define DEFAULT_KEYEXPR "group1/**"
#define DEFAULT_TIMEOUT_MS 10000

struct args_t {
char* keyexpr; // -k
char* keyexpr; // -k, --key
uint64_t timeout_ms; // -o, --timeout
};
struct args_t parse_args(int argc, char** argv, z_owned_config_t* config);

Expand All @@ -47,7 +49,10 @@ int main(int argc, char** argv) {
z_owned_fifo_handler_reply_t handler;
z_owned_closure_reply_t closure;
z_fifo_channel_reply_new(&closure, &handler, 16);
z_liveliness_get(z_loan(s), z_loan(keyexpr), z_move(closure), NULL);
z_liveliness_get_options_t opts;
z_liveliness_get_options_default(&opts);
opts.timeout_ms = args.timeout_ms;
z_liveliness_get(z_loan(s), z_loan(keyexpr), z_move(closure), &opts);
z_owned_reply_t reply;
for (z_result_t res = z_recv(z_loan(handler), &reply); res == Z_OK; res = z_recv(z_loan(handler), &reply)) {
if (z_reply_is_ok(z_loan(reply))) {
Expand All @@ -71,23 +76,18 @@ void print_help() {
"\
Usage: z_get_liveliness [OPTIONS]\n\n\
Options:\n\
-k <KEY> (optional, string, default='%s'): The key expression to query\n",
DEFAULT_KEYEXPR);
-k, --key <KEY> (optional, string, default='%s'): The key expression to query\n\
-o, --timeout <TIMEOUT_MS> (optional, number, default = '%d'): Query timeout in milliseconds\n",
DEFAULT_KEYEXPR, DEFAULT_TIMEOUT_MS);
printf(COMMON_HELP);
printf(
"\
-h: print help\n");
}

struct args_t parse_args(int argc, char** argv, z_owned_config_t* config) {
if (parse_opt(argc, argv, "h", false)) {
print_help();
exit(1);
}
const char* keyexpr = parse_opt(argc, argv, "k", true);
if (!keyexpr) {
keyexpr = DEFAULT_KEYEXPR;
}
_Z_CHECK_HELP;
struct args_t args;
_Z_PARSE_ARG(args.keyexpr, "k", "key", (char*), (char*)DEFAULT_KEYEXPR);
_Z_PARSE_ARG(args.timeout_ms, "o", "timeout", atoi, DEFAULT_TIMEOUT_MS);

parse_zenoh_common_args(argc, argv, config);
const char* arg = check_unknown_opts(argc, argv);
if (arg) {
Expand All @@ -101,5 +101,5 @@ struct args_t parse_args(int argc, char** argv, z_owned_config_t* config) {
exit(-1);
}
free(pos_args);
return (struct args_t){.keyexpr = (char*)keyexpr};
return args;
}
Loading

0 comments on commit 9ced08a

Please sign in to comment.