diff options
author | Thomas Voss <mail@thomasvoss.com> | 2022-11-13 08:37:24 +0100 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2022-11-13 09:17:41 +0100 |
commit | bd8824c999571179c963fe7a87a49d01c37c9018 (patch) | |
tree | 025ad5a344cd5747a5d2263104309a29c1cc69a0 | |
parent | e9ce2691c913004051c418f7457072df49ca2680 (diff) |
Add support for long options
-rw-r--r-- | center.1 | 12 | ||||
-rw-r--r-- | center.c | 14 | ||||
-rw-r--r-- | completions/_center | 14 |
3 files changed, 25 insertions, 15 deletions
@@ -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. @@ -21,6 +21,7 @@ #include <ctype.h> #include <err.h> #include <errno.h> +#include <getopt.h> #include <limits.h> #include <stdbool.h> #include <stdio.h> @@ -29,6 +30,7 @@ #include <unistd.h> #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 |