From bd8824c999571179c963fe7a87a49d01c37c9018 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sun, 13 Nov 2022 08:37:24 +0100 Subject: Add support for long options --- center.1 | 12 ++++++------ center.c | 14 ++++++++++++-- completions/_center | 14 +++++++------- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/center.1 b/center.1 index b81fd6a..8333ad9 100644 --- a/center.1 +++ b/center.1 @@ -12,7 +12,7 @@ .\" LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR .\" OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR .\" PERFORMANCE OF THIS SOFTWARE. -.Dd $Mdocdate: October 8 2022 $ +.Dd $Mdocdate: November 13 2022 $ .Dt CENTER 1 .Os .Sh NAME @@ -51,15 +51,15 @@ flag. .Pp The options are as follows: .Bl -tag -width Ds -.It Fl e +.It Fl e Ns , Fl Fl ignore\-ansi Do not take ANSI color escape sequences into account when centering input. This will cause input containing ANSI color escape sequences to fail to be visually centered. -.It Fl l +.It Fl l Ns , Fl Fl longest Center the file as a whole instead of centering each line individually. In otherwords each line is centered as if it has the length of the longest line. This is useful for tasks like centering source code. -.It Fl r +.It Fl r Ns , Fl Fl spaces Display tabs using spaces. By default .Nm @@ -71,12 +71,12 @@ This flag ensures that every tab character will display as 8 columns or the amount of columns specified by the .Fl t flag. -.It Fl t Ar width +.It Fl t Ns , Fl Fl tabsize Ns = Ns Ar width Treat tab characters as if they have a width of .Ar width . If this option is not specified, tabs are assumed to have a tab width of 8 columns. -.It Fl w Ar width +.It Fl w Ns , Fl Fl width Ns = Ns Ar width Center align the input as if the output device has a width of .Ar width . This option is required when the output device is not a terminal. diff --git a/center.c b/center.c index f4f2f00..cbc4d54 100644 --- a/center.c +++ b/center.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -29,6 +30,7 @@ #include #define ESC 033 +#define OPTSTR "elrt:w:" #define PROG_ARGS "[-elr] [-t width] [-w width] [file ...]\n" #define die(...) err(EXIT_FAILURE, __VA_ARGS__) @@ -74,8 +76,16 @@ main(int argc, char **argv) { int opt; void (*centerfunc)(FILE *) = center; - - while ((opt = getopt(argc, argv, ":elrt:w:")) != -1) { + static struct option longopts[] = { + {"ignore-ansi", no_argument, NULL, 'e'}, + {"longest", no_argument, NULL, 'l'}, + {"spaces", no_argument, NULL, 'r'}, + {"tabsize", required_argument, NULL, 't'}, + {"width", required_argument, NULL, 'w'}, + {NULL, 0, NULL, 0 } + }; + + while ((opt = getopt_long(argc, argv, OPTSTR, longopts, NULL)) != -1) { switch (opt) { case 'e': lenfunc = utf8len; diff --git a/completions/_center b/completions/_center index 504028e..16238d9 100644 --- a/completions/_center +++ b/completions/_center @@ -1,11 +1,11 @@ #compdef center -_arguments -sS \ - '-e[do not consider ANSI color escape sequences]' \ - '-l[center files, not files]' \ - '-r[display tabs using spaces]' \ - '-t+[specify width of tab characters]:tab width (in columns)' \ - '-w+[specify output width]:output width (in columns)' \ - '*: :_files' +_arguments -s -S \ + '(-e --ignore-ansi)'{-e,--ignore-ansi}'[do not consider ANSI color escape sequences]' \ + '(-l --longest)'{-l,--longest}'[center files, not files]' \ + '(-r --spaces)'{-r,--spaces}'[display tabs using spaces]' \ + '(-t --tabsize)'{-t+,--tabsize=}'[specify width of tab characters]:integer' \ + '(-w --width)'{-w+,--width=}'[specify output width]:integer' \ + '*:file:_files' # vi: ft=zsh -- cgit v1.2.3