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'
This commit is contained in:
Веревкин Максим 2020-06-23 05:35:42 +03:00 committed by GitHub
parent af8231e3ad
commit e32c02454a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 3 deletions

View File

@ -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')

View File

@ -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"
@ -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",

View File

@ -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