[PATCH weston v3 1/2] editor: Use parse_options() from shared for command line options

Bryce Harrington bryce at osg.samsung.com
Mon Nov 28 23:42:55 UTC 2016


On Sun, Nov 27, 2016 at 02:54:54PM -0800, Yong Bakos wrote:
> On Nov 21, 2016, at 1:26 PM, Bryce Harrington <bryce at osg.samsung.com> wrote:
> > 
> > Also add a basic --help option
> > 
> > Signed-off-by: Bryce Harrington <bryce at osg.samsung.com>
> 
> This patch extracts some option flags out of main, uses parse_options instead
> of strcmp, and adds some behavior around the help option and error behavior
> around an excess arg count. It reflects the same option-parsing idiom found
> elsewhere in the weston codebase.
> 
> While the `char *text_buffer` declaration in main belongs in 2/2 of this patch,
> this one is:

I've made that change and pushed the two patches with your review, thanks!

To ssh://git.freedesktop.org/git/wayland/weston
   21fac60..411ffab  master -> master

Bryce
 
> Reviewed-by: Yong Bakos <ybakos at humanoriented.com>
> 
> yong
> 
> 
> > ---
> > clients/editor.c | 80 ++++++++++++++++++++++++++++++++++++++++----------------
> > 1 file changed, 58 insertions(+), 22 deletions(-)
> > 
> > diff --git a/clients/editor.c b/clients/editor.c
> > index 30bf555..b8fc63a 100644
> > --- a/clients/editor.c
> > +++ b/clients/editor.c
> > @@ -37,6 +37,7 @@
> > 
> > #include <pango/pangocairo.h>
> > 
> > +#include "shared/config-parser.h"
> > #include "shared/helpers.h"
> > #include "shared/xalloc.h"
> > #include "window.h"
> > @@ -1489,28 +1490,63 @@ global_handler(struct display *display, uint32_t name,
> > 	}
> > }
> > 
> > +/** Display help for command line options, and exit */
> > +static uint32_t opt_help = 0;
> > +
> > +/** Require a distinct click to show the input panel (virtual keyboard) */
> > +static uint32_t opt_click_to_show = 0;
> > +
> > +/** Set a specific (RFC-3066) language.  Used for the virtual keyboard, etc. */
> > +static const char *opt_preferred_language = NULL;
> > +
> > +/**
> > + * \brief command line options for editor
> > + */
> > +static const struct weston_option editor_options[] = {
> > +	{ WESTON_OPTION_BOOLEAN, "help", 'h', &opt_help },
> > +	{ WESTON_OPTION_BOOLEAN, "click-to-show", 'C', &opt_click_to_show },
> > +	{ WESTON_OPTION_STRING, "preferred-language", 'L', &opt_preferred_language },
> > +};
> > +
> > +static void
> > +usage(const char *program_name, int exit_code)
> > +{
> > +	unsigned k;
> > +
> > +	fprintf(stderr, "Usage: %s [OPTIONS]\n\n", program_name);
> > +	for (k = 0; k < ARRAY_LENGTH(editor_options); k++) {
> > +		const struct weston_option *p = &editor_options[k];
> > +		if (p->name) {
> > +			fprintf(stderr, "  --%s", p->name);
> > +			if (p->type != WESTON_OPTION_BOOLEAN)
> > +				fprintf(stderr, "=VALUE");
> > +			fprintf(stderr, "\n");
> > +		}
> > +		if (p->short_name) {
> > +			fprintf(stderr, "  -%c", p->short_name);
> > +			if (p->type != WESTON_OPTION_BOOLEAN)
> > +				fprintf(stderr, "VALUE");
> > +			fprintf(stderr, "\n");
> > +		}
> > +	}
> > +	exit(exit_code);
> > +}
> > +
> > int
> > main(int argc, char *argv[])
> > {
> > 	struct editor editor;
> > -	int i;
> > -	uint32_t click_to_show = 0;
> > -	const char *preferred_language = NULL;
> > -
> > -	for (i = 1; i < argc; i++) {
> > -		if (strcmp("--click-to-show", argv[i]) == 0)
> > -			click_to_show = 1;
> > -		else if (strcmp("--preferred-language", argv[i]) == 0 &&
> > -			 i + 1 < argc) {
> > -			preferred_language = argv[i + 1];
> > -			i++;
> > -		} else {
> > -			printf("Usage: %s [OPTIONS]\n"
> > -			       "  --click-to-show\n"
> > -			       "  --preferred-language LANGUAGE\n",
> > -			       argv[0]);
> > -			return 1;
> > -		}
> > +	char *text_buffer = NULL;
> > +
> > +	parse_options(editor_options, ARRAY_LENGTH(editor_options),
> > +		      &argc, argv);
> > +	if (opt_help)
> > +		usage(argv[0], EXIT_SUCCESS);
> > +
> > +	if (argc > 1) {
> > +		usage(argv[0], EXIT_FAILURE);
> > +		/* FIXME: Use remaining arguments as a path/filename to load */
> > +		return 0;
> > 	}
> > 
> > 	memset(&editor, 0, sizeof editor);
> > @@ -1537,12 +1573,12 @@ main(int argc, char *argv[])
> > 	editor.widget = window_frame_create(editor.window, &editor);
> > 
> > 	editor.entry = text_entry_create(&editor, "Entry");
> > -	editor.entry->click_to_show = click_to_show;
> > -	if (preferred_language)
> > -		editor.entry->preferred_language = strdup(preferred_language);
> > +	editor.entry->click_to_show = opt_click_to_show;
> > +	if (opt_preferred_language)
> > +		editor.entry->preferred_language = strdup(opt_preferred_language);
> > 	editor.editor = text_entry_create(&editor, "Numeric");
> > 	editor.editor->content_purpose = ZWP_TEXT_INPUT_V1_CONTENT_PURPOSE_NUMBER;
> > -	editor.editor->click_to_show = click_to_show;
> > +	editor.editor->click_to_show = opt_click_to_show;
> > 	editor.selection = NULL;
> > 	editor.selected_text = NULL;
> > 
> > -- 
> > 1.9.1
> > 
> > _______________________________________________
> > wayland-devel mailing list
> > wayland-devel at lists.freedesktop.org
> > https://lists.freedesktop.org/mailman/listinfo/wayland-devel


More information about the wayland-devel mailing list