2013-04-07 15 views
5

Sto utilizzando Boost.program_options per analizzare le righe di comando per l'implementazione dei programmi di utilità POSIX. Come semplice esempio, prendi cmp.Come visualizzare la descrizione dell'operando della riga di comando in --help output

Ora mi piacerebbe avere un argomento aggiuntivo --help che mostra una descrizione di tutti gli argomenti, che è importante in questo caso. Ho:

po::options_description options("Options"); 
options.add_options()("help", "Show this help output.") 
        (",l", "(Lowercase ell.) Write the byte number (decimal) and the differing bytes (octal) for each difference.") 
        (",s", "Write nothing for differing files; return exit status only.") 

po::positional_options_description operands; 
operands.add("file1", 1);//, "A pathname of the first file to be compared. If file1 is '-', the standard input shall be used.") 
operands.add("file2", 1);//, "A pathname of the second file to be compared. If file2 is '-', the standard input shall be used."); 

po::variables_map vm; 
po::store(po::command_line_parser(argc, argv).options(options).positional(operands).run(), vm); 
po::notify(vm); 

if(vm.count("help")) 
{ 
    std::cout << "cmp: compare two files\nUsage: cmp [ -l | -s ] file1 file2\n" << options; 
    return 0; 
} 

che è in grado di mostrare le descrizione file1 e file2 opzioni. Posso ovviamente aggiungerli a options, ma questo aggiungerebbe almeno due argomenti indesiderati [-]-file{1,2}, che davvero non desidero. Voglio solo che questa uscita per --help (senza hardcoding ovviamente):

cmp: compare two files 
Usage: cmp [ -l | -s ] file1 file2 
Options: 
    --help    Show this help output. 
    -l     (Lowercase ell.) Write the byte number (decimal) and the differing bytes (octal) for each difference. 
    -s     Write nothing for differing files; return exit status only. 
Operands: 
    file1     A pathname of the first file to be compared. If file1 is '-', the standard input shall be used. 
    file2     A pathname of the second file to be compared. If file2 is '-', the standard input shall be used. 

Esiste un modo per raggiungere questo obiettivo, senza l'hacking intorno alla biblioteca? Penso che si tratti di cose piuttosto semplici, ma non riesco a trovarlo in nessuno degli tutorials.

UPDATE a beneficio di tutti, ho presentato una feature request per questo, in un certo senso si spera retrocompatibile.

risposta

1

Non è l'ideale, ma per quanto riguarda la creazione di un set "fittizio" di opzioni di programma, lasciare che il form formattato boost :: program_options formi il testo di aiuto per te, quindi rimuovere i trattini con una sostituzione rapida?

Quindi inviare il testo della guida in aggiunta al testo della guida options.

Qualcosa di simile a questo:

po::options_description dummy_options("Operands"); 
dummy_options.add_options() 
    ("file1", po::value<std::string>(), "A pathname of the first file to be compared. If file1 is '-', the standard input shall be used.") 
    ("file2", po::value<std::string>(), "A pathname of the second file to be compared. If file2 is '-', the standard input shall be used.") 
    ; 

std::stringstream s; 
s << dummy_options; 
std::string dummy_help_text = s.str(); 
boost::replace_all(dummy_help_text, "--", ""); 
boost::replace_all(dummy_help_text, "arg", "  "); 

std::cout << dummy_help_text << std::endl; 

L'output è simile al seguente:

Operands: 
    file1     A pathname of the first file to be compared. If file1 
         is '-', the standard input shall be used. 
    file2     A pathname of the second file to be compared. If file2 
         is '-', the standard input shall be used. 

Non è l'ideale, perché, tra le altre cose, la spaziatura tra le colonne non corrisponderà l'uscita aiuto dalla vostra altra uscita options. Ma per qualcosa di veloce e sporco, che funziona fondamentalmente, potrebbe andare bene.

È un pensiero, comunque.

(Non vedo nulla nell'API Boost.Program_Options che ti permetta di fare questo anche nel modo giusto. https://stackoverflow.com/a/3621947/368896 dà un suggerimento che questo genere di cose non è supportato, ma che ora ha 3 anni .)

+0

Cattive notizie, sfortunatamente ': (' – rubenvb