Skip to content

Commit

Permalink
add support for long named options
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisBiryukov91 committed Dec 5, 2024
1 parent 430fa97 commit 7bd9726
Show file tree
Hide file tree
Showing 27 changed files with 254 additions and 386 deletions.
69 changes: 40 additions & 29 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 Down Expand Up @@ -236,18 +262,3 @@ z_priority_t parse_priority(const char* arg) {
}
return (z_priority_t)p;
}

#define _Z_PARSE_ARG(VALUE, ID, FUNC, DEFAULT_VALUE) \
do { \
const char* arg_val = parse_opt(argc, argv, ID, true); \
if (!arg_val) { \
VALUE = DEFAULT_VALUE; \
} else { \
VALUE = FUNC(arg_val); \
} \
} while (0)

#define _Z_CHECK_FLAG(VALUE, ID) \
do { \
VALUE = (parse_opt(argc, argv, ID, false) != NULL); \
} while (0)
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;
}
32 changes: 13 additions & 19 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 Down Expand Up @@ -105,27 +105,21 @@ 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);
}
_Z_CHECK_HELP;
struct args_t args;
_Z_PARSE_ARG(args.selector, "s", (char*), (char*)DEFAULT_SELECTOR);
_Z_PARSE_ARG(args.value, "p", (char*), (char*)DEFAULT_VALUE);
_Z_PARSE_ARG(args.timeout_ms, "o", atoi, DEFAULT_TIMEOUT_MS);
_Z_PARSE_ARG(args.target, "t", parse_query_target, z_query_target_default());
_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);
Expand Down
20 changes: 7 additions & 13 deletions examples/z_get_liveliness.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
#define DEFAULT_TIMEOUT_MS 10000

struct args_t {
char* keyexpr; // -k
uint64_t timeout_ms; // -o
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 Down Expand Up @@ -76,23 +76,17 @@ void print_help() {
"\
Usage: z_get_liveliness [OPTIONS]\n\n\
Options:\n\
-k <KEY> (optional, string, default='%s'): The key expression to query\n\
-o <TIMEOUT_MS> (optional, number, default = '%d'): Query timeout in milliseconds\n",
-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);
}
_Z_CHECK_HELP;
struct args_t args;
_Z_PARSE_ARG(args.keyexpr, "k", (char*), (char*)DEFAULT_KEYEXPR);
_Z_PARSE_ARG(args.timeout_ms, "o", atoi, DEFAULT_TIMEOUT_MS);
_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);
Expand Down
32 changes: 13 additions & 19 deletions examples/z_get_shm.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; // -p
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 Down Expand Up @@ -137,27 +137,21 @@ 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);
}
_Z_CHECK_HELP;
struct args_t args;
_Z_PARSE_ARG(args.selector, "s", (char*), (char*)DEFAULT_SELECTOR);
_Z_PARSE_ARG(args.value, "p", (char*), (char*)DEFAULT_VALUE);
_Z_PARSE_ARG(args.timeout_ms, "o", atoi, DEFAULT_TIMEOUT_MS);
_Z_PARSE_ARG(args.target, "t", parse_query_target, z_query_target_default());
_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);
Expand Down
8 changes: 1 addition & 7 deletions examples/z_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,10 @@ void print_help() {
Usage: z_info [OPTIONS]\n\n\
Options:\n");
printf(COMMON_HELP);
printf(
"\
-h: print help\n");
}

void parse_args(int argc, char** argv, z_owned_config_t* config) {
if (parse_opt(argc, argv, "h", false)) {
print_help();
exit(1);
}
_Z_CHECK_HELP;
parse_zenoh_common_args(argc, argv, config);
const char* arg = check_unknown_opts(argc, argv);
if (arg) {
Expand Down
20 changes: 6 additions & 14 deletions examples/z_liveliness.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#define DEFAULT_KEYEXPR "group1/zenoh-rs"

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 @@ -69,23 +69,15 @@ void print_help() {
"\
Usage: z_liveliness [OPTIONS]\n\n\
Options:\n\
-k <KEY> (optional, string, default='%s'): The key expression the liveliness token\n",
-k, --key <KEY> (optional, string, default='%s'): The key expression the liveliness token\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 @@ -99,5 +91,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 7bd9726

Please sign in to comment.