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.
This commit is contained in:
Frank LENORMAND 2017-03-07 18:11:29 +03:00
parent 79faae8546
commit 16068321c1
2 changed files with 28 additions and 3 deletions

View File

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

View File

@ -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]... [+<line>[:<col>]|+:]\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<char*>{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)