21 bump_verbosity(
const gchar *option_name,
const gchar *optarg, gpointer
data, GError **error) {
46 free_common_args(gpointer
data) {
62 GOptionGroup **output_group,
const char *param_string) {
64 GOptionContext *context;
65 GOptionGroup *main_group;
67 GOptionEntry main_entries[3] = {
68 {
"version",
'$', 0, G_OPTION_ARG_NONE, &(common_args->
version),
69 "Display software version and exit",
71 {
"verbose",
'V', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, bump_verbosity,
72 "Increase debug output (may be specified multiple times)",
78 main_group = g_option_group_new(NULL,
"Application Options:", NULL, common_args, free_common_args);
79 g_option_group_add_entries(main_group, main_entries);
81 context = g_option_context_new(param_string);
82 g_option_context_set_summary(context, common_args->
summary);
83 g_option_context_set_description(context, desc);
84 g_option_context_set_main_group(context, main_group);
87 GOptionEntry output_entries[3] = {
88 {
"output-as", 0, 0, G_OPTION_ARG_STRING, &(common_args->
output_ty),
91 {
"output-to", 0, 0, G_OPTION_ARG_STRING, &(common_args->
output_dest),
92 "Specify file name for output (or \"-\" for stdout)",
"DEST" },
97 if (*output_group == NULL) {
98 *output_group = g_option_group_new(
"output",
"Output Options:",
"Show output help", NULL, NULL);
103 g_option_group_add_entries(*output_group, output_entries);
104 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 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);
147 GPtrArray *arr = NULL;
148 bool saw_dash_dash =
false;
149 bool copy_option =
false;
155 if (g_get_prgname() == NULL && argv && *argv) {
156 gchar *basename = g_path_get_basename(*argv);
158 g_set_prgname(basename);
162 arr = g_ptr_array_new();
164 for (
int i = 0; argv[i] != NULL; i++) {
170 if (saw_dash_dash ==
false && strcmp(argv[i],
"--") == 0) {
171 saw_dash_dash =
true;
174 if (saw_dash_dash ==
true) {
175 g_ptr_array_add(arr, g_strdup(argv[i]));
179 if (copy_option ==
true) {
180 g_ptr_array_add(arr, g_strdup(argv[i]));
189 g_ptr_array_add(arr, g_strdup(argv[i]));
196 if (g_str_has_prefix(argv[i],
"-") && !g_str_has_prefix(argv[i],
"--")) {
198 char *ch = argv[i]+1;
205 if (*ch !=
'\0' && *ch >=
'1' && *ch <=
'9') {
206 bool is_numeric =
true;
208 while (*ch !=
'\0') {
218 g_ptr_array_add(arr, g_strdup_printf(
"%s", argv[i]));
228 while (*ch !=
'\0') {
234 if (special != NULL && strchr(special, *ch) != NULL) {
238 if (*(ch+1) !=
'\0') {
239 g_ptr_array_add(arr, g_strdup_printf(
"-%c", *ch));
240 g_ptr_array_add(arr, g_strdup(ch+1));
248 g_ptr_array_add(arr, g_strdup_printf(
"-%c", *ch));
255 g_ptr_array_add(arr, g_strdup_printf(
"-%c", *ch));
265 g_ptr_array_add(arr, g_strdup(argv[i]));
269 g_ptr_array_add(arr, NULL);
271 return (
char **) g_ptr_array_free(arr, FALSE);
279 gchar **extra_args = NULL;
281 gboolean retval = TRUE;
283 va_start(ap, format);
284 len = vasprintf(&buf, format, ap);
288 if (!g_shell_parse_argv(buf, NULL, &extra_args, error)) {
289 g_strfreev(extra_args);
294 retval = g_option_context_parse_strv(context, &extra_args, error);
296 g_strfreev(extra_args);
crm_exit_t crm_errno2exit(int rc)
gchar ** pcmk__cmdline_preproc(char **argv, const char *special)
_Noreturn crm_exit_t crm_exit(crm_exit_t rc)
void pcmk__add_arg_group(GOptionContext *context, const char *name, const char *header, const char *desc, GOptionEntry entries[])
GOptionContext * pcmk__build_arg_context(pcmk__common_args_t *common_args, const char *fmts, GOptionGroup **output_group, const char *param_string)
gboolean summary(GList *resources)
pcmk__common_args_t * pcmk__new_common_args(const char *summary)
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1
void pcmk__free_arg_context(GOptionContext *context)
gboolean pcmk__force_args(GOptionContext *context, GError **error, const char *format,...)
#define PACKAGE_BUGREPORT
void pcmk__add_main_args(GOptionContext *context, GOptionEntry entries[])