From e32c02454ae4ec6ac30549d5fa9e80c2b64edb72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B5=D1=80=D0=B5=D0=B2=D0=BA=D0=B8=D0=BD=20=D0=9C?= =?UTF-8?q?=D0=B0=D0=BA=D1=81=D0=B8=D0=BC?= <34583604+MaxVerevkin@users.noreply.github.com> Date: Tue, 23 Jun 2020 05:35:42 +0300 Subject: [PATCH] feat(cli): add -v and --version flags * feat(cli): add -v and --version flags * fix(code): typo * fix(code): clang-format src/application.c * docs(swappy.1.scd): add version flags and long names for 'help' and 'file' --- meson.build | 17 ++++++++++++++++- src/application.c | 24 ++++++++++++++++++++++++ swappy.1.scd | 7 +++++-- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/meson.build b/meson.build index ef4d8f9..dbafb9d 100644 --- a/meson.build +++ b/meson.build @@ -1,7 +1,7 @@ project( 'swappy', 'c', - version: '1.0.0', + version: '1.0.1', license: 'MIT', meson_version: '>=0.48.0', default_options: [ @@ -11,6 +11,21 @@ project( ], ) +version = '"@0@"'.format(meson.project_version()) +git = find_program('git', native: true, required: false) +if git.found() + git_commit = run_command([git, 'rev-parse', '--short', 'HEAD']) + git_branch = run_command([git, 'rev-parse', '--abbrev-ref', 'HEAD']) + if git_commit.returncode() == 0 and git_branch.returncode() == 0 + version = '"@0@-@1@ (" __DATE__ ", branch \'@2@\')"'.format( + meson.project_version(), + git_commit.stdout().strip(), + git_branch.stdout().strip(), + ) + endif +endif +add_project_arguments('-DSWAPPY_VERSION=@0@'.format(version), language: 'c') + add_project_arguments('-Wno-unused-parameter', language: 'c') swappy_inc = include_directories('include') diff --git a/src/application.c b/src/application.c index fe98d58..e24dea9 100644 --- a/src/application.c +++ b/src/application.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include "buffer.h" @@ -728,7 +729,22 @@ static gint command_line_handler(GtkApplication *app, return EXIT_SUCCESS; } +// Print version and quit +gboolean callback_on_flag(const gchar *option_name, const gchar *value, + gpointer data, GError **error) { + if (!strcmp(option_name, "-v") || !strcmp(option_name, "--version")) { + printf("swappy version %s\n", SWAPPY_VERSION); + exit(0); + } + return TRUE; +} + bool application_init(struct swappy_state *state) { + // Callback function for flags + gboolean (*GOptionArgFunc)(const gchar *option_name, const gchar *value, + gpointer data, GError **error); + GOptionArgFunc = &callback_on_flag; + const GOptionEntry cli_options[] = { { .long_name = "file", @@ -745,6 +761,14 @@ bool application_init(struct swappy_state *state) { .description = "Print the final surface to the given file when " "exiting, use - to print to stdout", }, + { + .long_name = "version", + .short_name = 'v', + .flags = G_OPTION_FLAG_NO_ARG, + .arg = G_OPTION_ARG_CALLBACK, + .arg_data = GOptionArgFunc, + .description = "Print version and quit", + }, {NULL}}; state->app = gtk_application_new("me.jtheoof.swappy", diff --git a/swappy.1.scd b/swappy.1.scd index 68c85a0..ed8868b 100644 --- a/swappy.1.scd +++ b/swappy.1.scd @@ -23,10 +23,13 @@ to: *$HOME/Desktop*. # OPTIONS -*-h* +*-h, --help* Show help message and quit. -*-f* +*-v, --version* + Show version and quit. + +*-f, --file* A PNG file to load for editing. If set to *-*, read the file from standard input instead. This is grim