+static const os_subcommand_t __help_cmd;
+static const os_subcommand_t *_help_cmd = &__help_cmd;
+
+static const os_subcommand_t __main_cmd;
+static const os_subcommand_t *_main_cmd = &__main_cmd;
+static const os_subcommand_t *_internal_main_cmd = &__main_cmd;
+
+#pragma mark Main Subcommand
+static int _main_invoke(const os_subcommand_t *osc,
+ int argc,
+ const char *argv[]);
+
+static const os_subcommand_option_t _main_positional[] = {
+ [0] = {
+ .osco_version = OS_SUBCOMMAND_OPTION_VERSION,
+ .osco_flags = 0,
+ .osco_option = NULL,
+ .osco_argument_usage = "subcommand",
+ .osco_argument_human = "The subcommand to invoke",
+ },
+ OS_SUBCOMMAND_OPTION_TERMINATOR,
+};
+
+static const os_subcommand_t __main_cmd = {
+ .osc_version = OS_SUBCOMMAND_VERSION,
+ .osc_flags = OS_SUBCOMMAND_FLAG_MAIN,
+ .osc_name = "_main",
+ .osc_desc = "main command",
+ .osc_optstring = NULL,
+ .osc_options = NULL,
+ .osc_required = NULL,
+ .osc_optional = NULL,
+ .osc_positional = _main_positional,
+ .osc_invoke = &_main_invoke,
+};
+
+static int
+_main_invoke(const os_subcommand_t *osc, int argc, const char *argv[])
+{
+ return 0;
+}
+
+#pragma mark Help Subcommand
+static int _help_invoke(const os_subcommand_t *osc,
+ int argc,
+ const char *argv[]);
+
+static const os_subcommand_option_t _help_positional[] = {
+ [0] = {
+ .osco_version = OS_SUBCOMMAND_OPTION_VERSION,
+ .osco_flags = 0,
+ .osco_option = NULL,
+ .osco_argument_usage = "subcommand",
+ .osco_argument_human = "The subcommand to query for help",
+ },
+ OS_SUBCOMMAND_OPTION_TERMINATOR,
+};
+
+static const os_subcommand_t __help_cmd = {
+ .osc_version = OS_SUBCOMMAND_VERSION,
+ .osc_flags = 0,
+ .osc_name = "help",
+ .osc_desc = "prints helpful information",
+ .osc_optstring = NULL,
+ .osc_options = NULL,
+ .osc_required = NULL,
+ .osc_optional = NULL,
+ .osc_positional = _help_positional,
+ .osc_invoke = &_help_invoke,
+};
+
+static int
+_help_invoke(const os_subcommand_t *osc, int argc, const char *argv[])
+{
+ int xit = -1;
+ const char *cmdname = NULL;
+ const os_subcommand_t *target = NULL;
+ FILE *f = stdout;
+
+ if (argc > 1) {
+ cmdname = argv[1];
+ }
+
+ if (cmdname) {
+ // Print usage information for the requested subcommand.
+ target = _os_subcommand_find(argv[1]);
+ if (!target) {
+ os_subcommand_fprintf(osc, stderr, "unrecognized subcommand: %s",
+ argv[1]);
+ xit = EX_USAGE;
+ } else {
+ xit = 0;
+ }
+ } else {
+ // Print general, top-level usage followed by a list of subcommands.
+ target = _main_cmd;
+ xit = 0;
+ }
+
+ if (xit) {
+ f = stderr;
+ }
+
+ _os_subcommand_print_usage(target, f);
+
+ if (target == _main_cmd) {
+ const os_subcommand_t **oscip = NULL;
+ const os_subcommand_t *osci = NULL;
+ bool header_printed = false;
+
+ LINKER_SET_FOREACH(oscip, const os_subcommand_t **,
+ SUBCOMMAND_LINKER_SET) {
+ osci = *oscip;
+
+ _print_header(f, "subcommands", &header_printed);
+ _os_subcommand_print_help_line(osci, f);
+ }
+
+ // Print the help subcommand last.
+ _os_subcommand_print_help_line(osc, f);
+ }
+
+ return xit;
+}
+
+#pragma mark Utilities
+static void
+_print_header(FILE *f, const char *hdr, bool *already_done)
+{
+ if (*already_done) {
+ return;
+ }
+
+ crfprintf_np(f, "");
+ crfprintf_np(f, "%s:", hdr);
+ *already_done = true;
+}