21 bump_verbosity(
const gchar *option_name,
const gchar *optarg, gpointer
data, GError **error) {
37 args->
summary = strdup(summary);
48 free_common_args(gpointer
data) {
64 GOptionGroup **output_group,
const char *param_string) {
65 GOptionContext *context;
66 GOptionGroup *main_group;
68 GOptionEntry main_entries[3] = {
69 {
"version",
'$', 0, G_OPTION_ARG_NONE, &(common_args->
version),
70 N_(
"Display software version and exit"),
72 {
"verbose",
'V', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, bump_verbosity,
73 N_(
"Increase debug output (may be specified multiple times)"),
79 main_group = g_option_group_new(NULL,
"Application Options:", NULL, common_args, free_common_args);
80 g_option_group_add_entries(main_group, main_entries);
82 context = g_option_context_new(param_string);
83 g_option_context_set_summary(context, common_args->
summary);
84 g_option_context_set_description(context,
86 g_option_context_set_main_group(context, main_group);
89 GOptionEntry output_entries[3] = {
90 {
"output-as", 0, 0, G_OPTION_ARG_STRING, &(common_args->
output_ty),
93 {
"output-to", 0, 0, G_OPTION_ARG_STRING, &(common_args->
output_dest),
94 N_(
"Specify file name for output (or \"-\" for stdout)"),
N_(
"DEST") },
99 if (*output_group == NULL) {
100 *output_group = g_option_group_new(
"output",
N_(
"Output Options:"),
N_(
"Show output help"), NULL, NULL);
105 g_option_group_add_entries(*output_group, output_entries);
106 g_option_context_add_group(context, *output_group);
116 if (context == NULL) {
120 g_option_context_free(context);
126 GOptionGroup *main_group = g_option_context_get_main_group(context);
128 g_option_group_add_entries(main_group, entries);
133 const char *header,
const char *desc,
134 const GOptionEntry entries[])
136 GOptionGroup *group = NULL;
138 group = g_option_group_new(
name, header, desc, NULL, NULL);
139 g_option_group_add_entries(group, entries);
140 g_option_context_add_group(context, group);
146 string_replace(gchar *str,
const gchar *sub,
const gchar *repl)
155 gchar **split = g_strsplit(str, sub, 0);
156 gchar *retval = g_strjoinv(repl, split);
167 if (argv == NULL || argv[0] == NULL) {
171 gs = g_string_sized_new(100);
173 for (
int i = 0; argv[i] != NULL; i++) {
175 g_string_append_c(gs,
' ');
178 if (strchr(argv[i],
' ') == NULL) {
180 g_string_append(gs, argv[i]);
181 }
else if (strchr(argv[i],
'\'') == NULL) {
203 gchar *repl = string_replace(argv[i],
"'",
"\\\'");
209 return g_string_free(gs, FALSE);
214 GPtrArray *arr = NULL;
215 bool saw_dash_dash =
false;
216 bool copy_option =
false;
222 if (g_get_prgname() == NULL && argv && *argv) {
223 gchar *basename = g_path_get_basename(*argv);
225 g_set_prgname(basename);
229 arr = g_ptr_array_new();
231 for (
int i = 0; argv[i] != NULL; i++) {
237 if (saw_dash_dash ==
false && strcmp(argv[i],
"--") == 0) {
238 saw_dash_dash =
true;
241 if (saw_dash_dash ==
true) {
242 g_ptr_array_add(arr, g_strdup(argv[i]));
246 if (copy_option ==
true) {
247 g_ptr_array_add(arr, g_strdup(argv[i]));
256 g_ptr_array_add(arr, g_strdup(argv[i]));
263 if (strcmp(argv[i],
"-INFINITY") == 0) {
264 g_ptr_array_add(arr, g_strdup(argv[i]));
271 if (g_str_has_prefix(argv[i],
"-") && !g_str_has_prefix(argv[i],
"--")) {
273 const char *ch = argv[i]+1;
280 if (*ch !=
'\0' && *ch >=
'1' && *ch <=
'9') {
281 bool is_numeric =
true;
283 while (*ch !=
'\0') {
293 g_ptr_array_add(arr, g_strdup_printf(
"%s", argv[i]));
303 while (*ch !=
'\0') {
309 if (special != NULL && strchr(special, *ch) != NULL) {
313 if (*(ch+1) !=
'\0') {
314 fprintf(stderr,
"Deprecated argument format '-%c%s' used.\n", *ch, ch+1);
315 fprintf(stderr,
"Please use '-%c %s' instead. " 316 "Support will be removed in a future release.\n",
319 g_ptr_array_add(arr, g_strdup_printf(
"-%c", *ch));
320 g_ptr_array_add(arr, g_strdup(ch+1));
328 g_ptr_array_add(arr, g_strdup_printf(
"-%c", *ch));
335 g_ptr_array_add(arr, g_strdup_printf(
"-%c", *ch));
345 g_ptr_array_add(arr, g_strdup(argv[i]));
349 g_ptr_array_add(arr, NULL);
351 return (
char **) g_ptr_array_free(arr, FALSE);
359 gchar **extra_args = NULL;
361 gboolean retval = TRUE;
363 va_start(ap, format);
364 len = vasprintf(&buf, format, ap);
368 if (!g_shell_parse_argv(buf, NULL, &extra_args, error)) {
369 g_strfreev(extra_args);
374 retval = g_option_context_parse_strv(context, &extra_args, error);
376 g_strfreev(extra_args);
_Noreturn crm_exit_t crm_exit(crm_exit_t rc)
gchar ** pcmk__cmdline_preproc(char *const *argv, const char *special)
gchar * pcmk__quote_cmdline(gchar **argv)
GOptionContext * pcmk__build_arg_context(pcmk__common_args_t *common_args, const char *fmts, GOptionGroup **output_group, const char *param_string)
External (OS/environmental) problem.
void pcmk__g_strcat(GString *buffer,...) G_GNUC_NULL_TERMINATED
pcmk__common_args_t * pcmk__new_common_args(const char *summary)
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1
void pcmk__add_arg_group(GOptionContext *context, const char *name, const char *header, const char *desc, const GOptionEntry entries[])
void pcmk__free_arg_context(GOptionContext *context)
gboolean pcmk__force_args(GOptionContext *context, GError **error, const char *format,...)
#define pcmk__assert(expr)
void pcmk__add_main_args(GOptionContext *context, const GOptionEntry entries[])