Nice way to do help screen output?

  • Thread starter Richard Tierney
  • Start date
R

Richard Tierney

To create help output (the response to "myprog --help", for example) I
currently create a big .h file, which includes a single string, such
as:

static char *help_text = "\
myprog: my program\n\
loads and\n\
loads of\n\
painfully manually\n\
formatted and\n\
justified text\n"

There's got to be a better way to do this - any ideas? Ideally, I
would like to write the help as a plain text file. Of course, I don't
want to distribute this file with the program, so it needs some
preprocessing. I'm on Linux.

Cheers

Richard
 
J

Joona I Palaste

Richard Tierney <[email protected]> scribbled the following
To create help output (the response to "myprog --help", for example) I
currently create a big .h file, which includes a single string, such
as:
static char *help_text = "\
myprog: my program\n\
loads and\n\
loads of\n\
painfully manually\n\
formatted and\n\
justified text\n"
There's got to be a better way to do this - any ideas? Ideally, I
would like to write the help as a plain text file. Of course, I don't
want to distribute this file with the program, so it needs some
preprocessing. I'm on Linux.

You're going to have to type the text in manually anyway, so what does
it matter whether it's going in your C source or a text file? If you
can use an external text file in your program, you can use the normal
C stream IO functions to display its contents, which will give you the
benefit of not having to type the \n\ bits after every line.
But since you said you don't want to use an external text file,
writing the text in your source code is really the only option. Or you
could just write a mass of whitespace there and write the text directly
into the compiled object file using a hex editor. This gives you the
rather minuscule benefit of being able to write only one character (the
newline) instead of three for \n\.
 
K

Karl Heinz Buchegger

Richard said:
To create help output (the response to "myprog --help", for example) I
currently create a big .h file, which includes a single string, such
as:

static char *help_text = "\
myprog: my program\n\
loads and\n\
loads of\n\
painfully manually\n\
formatted and\n\
justified text\n"

There's got to be a better way to do this - any ideas? Ideally, I
would like to write the help as a plain text file.

What is hindering you?

Writing tools is something a programmer is always aware of.
In your case a tool would be simple. Write a program which generates
the header file from a text file:

open help text file for input
open header file for output

output "static char *help_text = \"\\" to header file

as long as a line could be read from the help text file
output that line to the header file
output "\n\\" to the header file

output "\";" to the header file

close header file
close text file

That's your tool.
You edit your plain vanilla help text file. When you are finished
you let the tool generate the header file and use that in the
compilation as usual.
 
M

Michael Bruschkewitz

How much time need you to format the text in your editor?
How much time will it need to create a tool?
How much time you will need to search this tool?

The normal help-screen message should fit into one screen page.
Write the text.
Insert " at the start of each line.
Append \n" at the end of each line.
Use the search-and-replace feature of your favourite editor for this.

char const helpmsg[] =
"this\n"
" is\n"
" the\n"
" easy\n"
" way"
;

std::cout << helpmsg << std::endl;

Done.

-mb
 
M

Malcolm

Richard Tierney said:
To create help output (the response to "myprog --help", for example) I
currently create a big .h file

There's got to be a better way to do this - any ideas? Ideally, I
would like to write the help as a plain text file.
There's no one easy answer. Many C compilers will allow you to embed data
such as strings, images, sound files and so on into the program. However
this means that the listing is no longer portable.
Another way is to write a function that wraps and left-justifies your
string. This is probably the best solution, so long as you have a monospaced
font. With a variable pitched font, the function is still possible to write,
but much more difficult, much less generally useful, and probably more
trouble than it is worth.

The third approach is to write your own utility program to convert a text
file into C source (C data, not executable code). The problem with this is
that the utility program becomes part of you developement environment, may
have to be documented and so on, and the program becomes much more difficult
to maintain.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top