我正在用C编写一个命令行程序,我想实现一个--help
选项来显示常用的东西,比如可用的选项和它们的用途以及用法示例。
是否有正确的方法来格式化帮助中显示的文本?或者我是否尽力让它看起来不错?
我查看了SourceForge上的一些随机程序,看看他们是如何做到的,并且大多数只使用了一堆printf()
来输出预先格式化的(就间距和缩进而言)文本。
此处groff
或troff
是否合适?我在谷歌搜索中遇到过这些应用程序,它们似乎是某种排版程序,但我对它们并不熟悉。
答案 0 :(得分:5)
一般来说,更多的人担心程序的工作方式与帮助显示的程度相同。使用printf尽力而为(努力永远受到赞赏)并继续使用它和您的生活。你有更大的鱼可以炒。
答案 1 :(得分:3)
不要那么多格式化。另一方面,你自己会不时地使用它。包括诸如程序的目的,相关选项和一些示例等相关细节。清楚地描述它们的空白行应该是好的。
在不相关的说明中,如果您在c中执行命令行选项,请使用gnu getopts c库。它使它变得更容易。作为奖励,您不会被解析命令行的细节所困扰。
答案 2 :(得分:2)
使用groff
或troff
是过度的。正确布局printf()
格式就足够了。如果系统地布置帮助消息,则可以轻松管理。我在任何程序中使用的最复杂的选项包括:
static const char optlist[] = "a:cd:e:f:ghi:o:p:st:u:vxyz:A:BCD:E:F:G:HIJL:M:N:O:PQ:RS:TUVX:YZ:";
static const char usestr[] =
"[-cghsvxyY] [-d dbase] [-f file] [-e 'SQL stmt'] ['SQL stmt' ...]\n"
"Other options: [-CJPRU][-BHITV][-D delim][-E escape][-Q quote][-F format]\n"
" [-A date][-i file][-o file][-L qlimit][-N number][-M FIFO]\n"
" [-Z debug][-G eor][-t table][-p password][-u username]\n"
" [-a ibase][-X record=rectag,recset=settag,header=hdrtag]\n"
" [-O orderby][-S skip][-z number]\n"
"NB: -h gives more help!";
static const char fullhelp[] =
"\nOption summary:\n"
" -a ibase - input base (default 0; set to 10 for fields with leading zeroes)\n"
" -c - continue after errors\n"
" -d dbase - select database\n"
" -e 'SQL stmt' - execute SQL command\n"
" -f file - input file for SQLCMD\n"
" -g - debugging mode (single-step commands)\n"
" -h - print this message\n"
" -i file - input file for SQLRELOAD\n"
" -o file - output file for SQLUNLOAD\n"
" -p password - password for connection (beware security implications!)\n"
" -s - silent mode\n"
" -t table - name of table (for SQLRELOAD or SQLUNLOAD)\n"
" -u username - username for connection (beware security implications!)\n"
" -v - verbose mode\n"
" -x - trace mode\n"
" -y - enable history\n"
" -z number - set debugging level for syntax and lexical analyzers\n"
" -A date - set date format (eg dd-mm-yyyy for $DBDATE=dmy4-)\n"
" -B - operate in benchmark mode (implies -x and times SQL)\n"
" -C - operate as SQLCMD\n"
" -D delim - set field delimiter (default $DBDELIMITER or pipe)\n"
" -E escape - set escape character (default $DBESCAPE or backslash)\n"
" -F format - set output format (default SELECT; alternatives include:\n"
" UNLOAD, FIXED, FIXSEP, FIXDEL, CSV, XML, HTML)\n"
" -G eor - set EOR (end of record) character string (default $DBNEWLINE or newline)\n"
" -H - include column headings in output\n"
" -I - interactive mode (after executing command line)\n"
" -J - simulate isql/dbaccess; accept [database|-] [script|-]\n"
" -L qlimit - limit on number of rows returned by query\n"
" -M FIFO - monitor FIFO for input commands\n"
" -N number - size of transaction under RELOAD\n"
" (default 1024; 0 means no sub-transactions)\n"
" -O col1,... - order by these columns (SQLUNLOAD)\n"
" -P - operate as SQLUPLOAD (not implemented yet)\n"
" -Q quote - set quote character (default $DBQUOTE or double quote)\n"
" -R - operate as SQLRELOAD\n"
" -S skip - initial rows to skip (SQLRELOAD)\n"
" -T - include column types in output\n"
" -U - operate as SQLUNLOAD\n"
" -V - print version and exit\n"
" -X xmloptions - configure the XML output\n"
" recset='recsettag',record='recordtag',header='headertag'\n"
" -Y - do not enable history, even in interactive mode\n"
" -Z debug - set debugging level (SQLCMD must be compiled with debugging enabled)\n"
;
是的,它不久就需要长期选择。