From 16068321c1db5753de1a5f6c51859e7c843aa501 Mon Sep 17 00:00:00 2001 From: Frank LENORMAND Date: Tue, 7 Mar 2017 18:11:29 +0300 Subject: [PATCH] src: Support the `-help` flag This commit allows a help message to be printed when a `-help` flag is passed to the editor, which will subsequently quit after a summary and a description of all the flags available have been displayed. The GNU convention (passing a single `--help` argument to the program) is also supported, although undocumented. The man page also now documents the `+:` argument, although unrelated to the original changeset. --- doc/kak.1.txt | 8 ++++++-- src/main.cc | 23 ++++++++++++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/doc/kak.1.txt b/doc/kak.1.txt index 4c7710c9..e010048c 100644 --- a/doc/kak.1.txt +++ b/doc/kak.1.txt @@ -7,7 +7,7 @@ kak - a vim inspired, selection oriented code editor SYNOPSIS -------- -*kak* [-q] [-n] [-l] [-ro] [-clear] [-ui ui_type] [-e command] [-f keys] [-p session_id] [-c session_id|[[-d] -s session_id] [+line[:column]] file ... +*kak* [-help] [-q] [-n] [-l] [-ro] [-clear] [-ui ui_type] [-e command] [-f keys] [-p session_id] [-c session_id|[[-d] -s session_id] [+line[:column]|+:] file ... DESCRIPTION ----------- @@ -26,6 +26,9 @@ stays fixed and the cursor one moves around. OPTIONS ------- +-help:: + display a help message and quit + -n:: do not load resource files on startup ('kakrc', 'autoload', 'rc' etc) @@ -63,7 +66,8 @@ OPTIONS enter in 'readonly mode', all the buffers opened will not be written to disk +line[:column]:: - specify a target line and column for the first file + specify a target line and column for the first file; when the plus sign is followed by only a colon, + then the cursor is sent to the last line of the file file:: one or more files to edit diff --git a/src/main.cc b/src/main.cc index 347428f1..d0ccdf58 100644 --- a/src/main.cc +++ b/src/main.cc @@ -801,12 +801,33 @@ int main(int argc, char* argv[]) { "ui", { true, "set the type of user interface to use (ncurses, dummy, or json)" } }, { "l", { false, "list existing sessions" } }, { "clear", { false, "clear dead sessions" } }, - { "ro", { false, "readonly mode" } } } + { "ro", { false, "readonly mode" } }, + { "help", { false, "display a help message and quit" } } } }; + try { + auto show_usage = [&]() + { + write_stdout(format("Usage: {} [options] [file]... [+[:]|+:]\n\n" + "Options:\n" + "{}\n" + "Prefixing a positional argument with a plus (`+`) sign will place the\n" + "cursor at a given set of coordinates, or the end of the buffer if the plus\n" + "sign is followed only by a colon (`:`)\n", + argv[0], generate_switches_doc(param_desc.switches))); + return 0; + }; + + if (contains(ConstArrayView{argv+1, (size_t)argc-1}, StringView{"--help"})) + return show_usage(); + ParametersParser parser(params, param_desc); + const bool show_help_message = (bool)parser.get_switch("help"); + if (show_help_message) + return show_usage(); + const bool list_sessions = (bool)parser.get_switch("l"); const bool clear_sessions = (bool)parser.get_switch("clear"); if (list_sessions or clear_sessions)