From 383a942bb3fe05be887a6622ca5ed86e9bba0662 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sat, 18 Oct 2025 17:12:20 -0400 Subject: Added automatic manpages. --- src/compile/cli.c | 35 +++++++++++++++++++++++++++++++++++ src/compile/cli.h | 1 + 2 files changed, 36 insertions(+) (limited to 'src/compile') diff --git a/src/compile/cli.c b/src/compile/cli.c index 820f0e2e..3337e250 100644 --- a/src/compile/cli.c +++ b/src/compile/cli.c @@ -1,5 +1,6 @@ // This file defines how to compile CLI argument parsing +#include "../stdlib/cli.h" #include "../environment.h" #include "../stdlib/datatypes.h" #include "../stdlib/optionals.h" @@ -131,3 +132,37 @@ Text_t compile_cli_arg_call(env_t *env, Text_t fn_name, type_t *fn_type, const c code = Texts(code, ");\n"); return code; } + +public +Text_t compile_manpage(Text_t program, OptionalText_t synopsis, OptionalText_t description, arg_t *args) { + Text_t date = Text(""); // TODO: use date + Text_t man = Texts(".\\\" Automatically generated by Tomo\n" + ".TH \"", + Text$upper(program, Text("C")), "\" \"1\" \"", date, + "\" \"\" \"\"\n" + ".SH NAME\n", + program, " \\- ", synopsis.tag == TEXT_NONE ? Text("a Tomo program") : synopsis, "\n"); + + if (description.tag != TEXT_NONE) { + man = Texts(man, ".SH DESCRIPTION\n", description, "\n"); + } + + man = Texts(man, ".SH OPTIONS\n"); + for (arg_t *arg = args; arg; arg = arg->next) { + man = Texts(man, "\n.TP\n\\f[B]\\-\\-", Text$from_str(arg->name), "\\f[R]"); + if (arg->alias) man = Texts(man, ", \\f[B]\\-", Text$from_str(arg->alias), "\\f[R]"); + if (arg->type->tag == BoolType) { + man = Texts(man, "\n.TP\n\\f[B]\\-\\-no\\-", Text$from_str(arg->name)); + } else if (is_numeric_type(arg->type)) { + man = Texts(man, " \\f[I]N\\f[R]"); + } else if (arg->type->tag == ListType) { + man = Texts(man, " \\f[I]value1\\f[R] \\f[I]value2...\\f[R]"); + } else if (arg->type->tag == TableType) { + man = Texts(man, " \\f[I]key1:value1\\f[R] \\f[I]key2:value2...\\f[R]"); + } else { + man = Texts(man, " \\f[I]value\\f[R]"); + } + } + + return man; +} diff --git a/src/compile/cli.h b/src/compile/cli.h index 13c3dee5..fa60eccf 100644 --- a/src/compile/cli.h +++ b/src/compile/cli.h @@ -7,3 +7,4 @@ #include "../types.h" Text_t compile_cli_arg_call(env_t *env, Text_t fn_name, type_t *fn_type, const char *version); +Text_t compile_manpage(Text_t program, OptionalText_t synopsis, OptionalText_t description, arg_t *args); -- cgit v1.2.3