Skip to content

Commit

Permalink
Generate --help output from man page
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaIvanovV committed Jan 15, 2022
1 parent aa87f01 commit 0a89f52
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,14 @@ ${BUILDDIR}/%.o: %.c
@mkdir -p ${@D}
$(CC) -c -o $@ $(CFLAGS) -MD $<

-include ${DEP}
${GENDIR}/options-msg.h: ${MAN} ./gen-help.sh
@mkdir -p ${@D}
./gen-help.sh $< > $@

${BUILDDIR}/args.o: CFLAGS += -I${GENDIR}
${BUILDDIR}/args.o: ${GENDIR}/options-msg.h

${TBOBJ}:
$(MAKE) -C ${TBDIR} termbox.o

-include ${DEP}
1 change: 1 addition & 0 deletions config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ BIN := ictree
SRCDIR := src
INCDIR := include
DOCDIR := doc
GENDIR := gen
BUILDDIR := build

# Termbox2 lib
Expand Down
12 changes: 12 additions & 0 deletions gen-help.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh

text="$(man -P cat -l "$1")" || exit 1

echo '#define OPTIONS_MSG \'
echo '"\\n" \'
echo '"Options:\\n" \'
echo "$text" |
sed -n -e '/^OPTIONS/,$p' |
sed '1d; /^[A-Z]/q' |
sed '$d; s/^/"/; s/$/\\n" \\/' |
sed '$s/ \\$//'
9 changes: 9 additions & 0 deletions gen/options-msg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#define OPTIONS_MSG \
"\n" \
"Options:\n" \
" --help, -h\n" \
" Print a help message and exit.\n" \
"\n" \
" --version, -v\n" \
" Display version information and exit.\n" \
"\n"
9 changes: 3 additions & 6 deletions src/args.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,14 @@

#include "args.h"
#include "error.h"
#include "options-msg.h"
#include "version.h"

#define VERSION_MSG \
"ictree v" VERSION "\n" \
"Copyright 2022 Nikita Ivanov"

#define HELP_MSG \
"Usage: ictree [OPTION]... [FILE]\n" \
"\n" \
"--version, -v: print version and exit\n" \
"--help, -h: print this message and exit"
#define HELP_MSG "Usage: ictree [OPTION]... [FILE]\n" OPTIONS_MSG

static struct option long_opts[] = {
{ "version", no_argument, 0, 'v' },
Expand Down Expand Up @@ -60,7 +57,7 @@ enum ArgAction process_args(int argc, char **argv)
puts(VERSION_MSG);
return ArgActionExit;
case 'h':
puts(HELP_MSG);
printf("%s", HELP_MSG);
return ArgActionExit;
default:
set_errorf("invalid option: %s", argv[opt_i + 1]);
Expand Down

0 comments on commit 0a89f52

Please sign in to comment.