diff --git a/include/crm/common/Makefile.am b/include/crm/common/Makefile.am index deeff16fe6..6981586c59 100644 --- a/include/crm/common/Makefile.am +++ b/include/crm/common/Makefile.am @@ -1,18 +1,18 @@ # # Copyright 2004-2019 the Pacemaker project contributors # # The version control history for this file may have further details. # # This source code is licensed under the GNU General Public License version 2 # or later (GPLv2+) WITHOUT ANY WARRANTY. # MAINTAINERCLEANFILES = Makefile.in headerdir=$(pkgincludedir)/crm/common header_HEADERS = xml.h ipc.h util.h iso8601.h mainloop.h logging.h results.h \ nvpair.h noinst_HEADERS = cib_secrets.h ipcs.h internal.h alerts_internal.h \ iso8601_internal.h remote_internal.h xml_internal.h \ - ipc_internal.h output.h + ipc_internal.h output.h cmdline_internal.h diff --git a/include/crm/common/cmdline_internal.h b/include/crm/common/cmdline_internal.h new file mode 100644 index 0000000000..b27b236204 --- /dev/null +++ b/include/crm/common/cmdline_internal.h @@ -0,0 +1,38 @@ +/* + * Copyright 2019 the Pacemaker project contributors + * + * The version control history for this file may have further details. + * + * This source code is licensed under the GNU Lesser General Public License + * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY. + */ + +#ifndef CMDLINE_INTERNAL__H +#define CMDLINE_INTERNAL__H + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +typedef struct { + char *summary; + + gboolean version; + gboolean quiet; + unsigned int verbosity; + + char *output_ty; + char *output_ty_desc; + char *output_dest; +} pcmk__common_args_t; + +GOptionContext * +pcmk__build_arg_context(pcmk__common_args_t *common_args, const char *fmts); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/lib/common/Makefile.am b/lib/common/Makefile.am index 0b96e05f18..74de16b061 100644 --- a/lib/common/Makefile.am +++ b/lib/common/Makefile.am @@ -1,66 +1,67 @@ # # Copyright 2004-2019 Andrew Beekhof # # This source code is licensed under the GNU General Public License version 2 # or later (GPLv2+) WITHOUT ANY WARRANTY. # include $(top_srcdir)/Makefile.common AM_CPPFLAGS += -I$(top_builddir)/lib/gnu -I$(top_srcdir)/lib/gnu -DPCMK_SCHEMAS_EMERGENCY_XSLT=0 ## libraries lib_LTLIBRARIES = libcrmcommon.la # Disable -Wcast-qual if used, because we do some hacky casting, # and because libxml2 has some signatures that should be const but aren't # for backward compatibility reasons. # s390 needs -fPIC # s390-suse-linux/bin/ld: .libs/ipc.o: relocation R_390_PC32DBL against `__stack_chk_fail@@GLIBC_2.4' can not be used when making a shared object; recompile with -fPIC CFLAGS = $(CFLAGS_COPY:-Wcast-qual=) -fPIC noinst_HEADERS = crmcommon_private.h libcrmcommon_la_LDFLAGS = -version-info 36:0:2 libcrmcommon_la_CFLAGS = $(CFLAGS_HARDENED_LIB) libcrmcommon_la_LDFLAGS += $(LDFLAGS_HARDENED_LIB) libcrmcommon_la_LIBADD = @LIBADD_DL@ $(GNUTLSLIBS) # Use += rather than backlashed continuation lines for parsing by bumplibs.sh libcrmcommon_la_SOURCES = libcrmcommon_la_SOURCES += acl.c libcrmcommon_la_SOURCES += agents.c libcrmcommon_la_SOURCES += alerts.c libcrmcommon_la_SOURCES += attrd_client.c if BUILD_CIBSECRETS libcrmcommon_la_SOURCES += cib_secrets.c endif +libcrmcommon_la_SOURCES += cmdline.c libcrmcommon_la_SOURCES += compat.c libcrmcommon_la_SOURCES += digest.c libcrmcommon_la_SOURCES += io.c libcrmcommon_la_SOURCES += ipc.c libcrmcommon_la_SOURCES += iso8601.c libcrmcommon_la_SOURCES += logging.c libcrmcommon_la_SOURCES += mainloop.c libcrmcommon_la_SOURCES += nvpair.c libcrmcommon_la_SOURCES += operations.c libcrmcommon_la_SOURCES += output.c libcrmcommon_la_SOURCES += output_text.c libcrmcommon_la_SOURCES += output_xml.c libcrmcommon_la_SOURCES += pid.c libcrmcommon_la_SOURCES += procfs.c libcrmcommon_la_SOURCES += remote.c libcrmcommon_la_SOURCES += results.c libcrmcommon_la_SOURCES += schemas.c libcrmcommon_la_SOURCES += strings.c libcrmcommon_la_SOURCES += utils.c libcrmcommon_la_SOURCES += watchdog.c libcrmcommon_la_SOURCES += xml.c libcrmcommon_la_SOURCES += xpath.c libcrmcommon_la_SOURCES += ../gnu/md5.c clean-generic: rm -f *.log *.debug *.xml *~ diff --git a/lib/common/cmdline.c b/lib/common/cmdline.c new file mode 100644 index 0000000000..d68281710d --- /dev/null +++ b/lib/common/cmdline.c @@ -0,0 +1,72 @@ +/* + * Copyright 2019 the Pacemaker project contributors + * + * The version control history for this file may have further details. + * + * This source code is licensed under the GNU Lesser General Public License + * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY. + */ + +#include +#include + +#include +#include + +static gboolean +bump_verbosity(const gchar *option_name, const gchar *optarg, gpointer data, GError **error) { + pcmk__common_args_t *common_args = (pcmk__common_args_t *) data; + common_args->verbosity++; + return TRUE; +} + +static void +free_common_args(gpointer data) { + pcmk__common_args_t *common_args = (pcmk__common_args_t *) data; + + free(common_args->summary); + free(common_args->output_ty); + free(common_args->output_ty_desc); + free(common_args->output_dest); +} + +GOptionContext * +pcmk__build_arg_context(pcmk__common_args_t *common_args, const char *fmts) { + char *desc = crm_strdup_printf("Report bugs to %s\n", PACKAGE_BUGREPORT); + GOptionContext *context; + GOptionGroup *main_group; + + GOptionEntry main_entries[6] = { + { "version", '$', 0, G_OPTION_ARG_NONE, &(common_args->version), + "Display version information and exit.", + NULL }, + { "verbose", 'V', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, bump_verbosity, + "Increase debug output (may be specified multiple times).", + NULL }, + { "quiet", 'q', 0, G_OPTION_ARG_NONE, &(common_args->quiet), + "Be less descriptive in output.", + NULL }, + { "output-as", 0, 0, G_OPTION_ARG_STRING, &(common_args->output_ty), + NULL, + "FORMAT" }, + { "output-to", 0, 0, G_OPTION_ARG_STRING, &(common_args->output_dest), + "Specify the destination for output, \"-\" for stdout or a filename", "DEST" }, + + { NULL } + }; + + common_args->output_ty_desc = crm_strdup_printf("Specify the format for output, one of: %s", fmts); + main_entries[3].description = common_args->output_ty_desc; + + main_group = g_option_group_new(NULL, "Application Options:", NULL, common_args, free_common_args); + g_option_group_add_entries(main_group, main_entries); + + context = g_option_context_new(NULL); + g_option_context_set_summary(context, common_args->summary); + g_option_context_set_description(context, desc); + g_option_context_set_main_group(context, main_group); + + free(desc); + + return context; +}