2011-06-27 63 views
3

我正在編寫一個命令行程序在C中,我想實現一個--help選項來顯示常用的東西,如可用的選項,他們做什麼,和用法的例子。正確的格式爲 - 幫助輸出

是否有適當的方法來格式化幫助中顯示的文本?或者我只是盡我所能讓它看起來不錯?

我看了一下SourceForge上的一些隨機程序,看看他們是如何做到的,而且大多數人只是使用一堆printf()來輸出預格式化(儘可能的間距和縮進)文本。

groff還是troff這裏適合?我在Google上看到了這些應用程序,這些應用程序似乎是某種排版程序,但我對它們並不熟悉。

回答

5

一般來說,越來越多的人擔心該程序的運行方式與顯示幫助的程度相關。盡你所能(努力總是表示讚賞)與printf並繼續與它和你的生活。你有更大的魚來炒。

+0

聽起來不錯。非常感謝!我只是想確保在'stdlib'中隱藏了'make_nice_looking_help()'函數沒有祕密。 ;) – Michael

+0

'libpopt'中有這樣的功能。如果你[修補了一些錯誤](http://homepage.ntlworld.com./jonathan.deboynepollard/Softwares/libpopt.html),它甚至會用文字包裝'--help'文本。 – JdeBP

3

不要爲那種格式出汗。另一方面,你自己會不時地使用它。包括程序的目的,相關選項和一些示例等相關細節。明確界定它們的空白行應該是好的。

在不相干的筆記上,如果您在c中執行命令行選項,請使用gnu getopts c庫。它使得它更容易,更容易。作爲獎勵,你不會被解析命令行的細節所困擾。

2

使用grofftroff是矯枉過正。格式化的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" 
; 

是的,它需要很長時間才能進入長選項。

+0

Gotcha。非常感謝這個例子! – Michael