Skip to content

Commit

Permalink
feat(cli): add -v and --version flags
Browse files Browse the repository at this point in the history
* 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'
  • Loading branch information
MaxVerevkin committed Jun 23, 2020
1 parent af8231e commit e32c024
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
17 changes: 16 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
project(
'swappy',
'c',
version: '1.0.0',
version: '1.0.1',
license: 'MIT',
meson_version: '>=0.48.0',
default_options: [
Expand All @@ -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')
Expand Down
24 changes: 24 additions & 0 deletions src/application.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <gdk/gdk.h>
#include <glib-2.0/glib.h>
#include <gtk/gtk.h>
#include <stdio.h>
#include <time.h>

#include "buffer.h"
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down
7 changes: 5 additions & 2 deletions swappy.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ to: *$HOME/Desktop*.

# OPTIONS

*-h*
*-h, --help*
Show help message and quit.

*-f* <file>
*-v, --version*
Show version and quit.

*-f, --file* <file>
A PNG file to load for editing.

If set to *-*, read the file from standard input instead. This is grim
Expand Down

0 comments on commit e32c024

Please sign in to comment.