Parser de Argumente C

Previzualizare laborator:

Extras din laborator:

This is a class to aid handling of command line arguments in a C++ program. It follows (and enforces) the unsual conventions.

New arguments are added by calling functions of the form of new_[optional_/named_]type.

- type is the type of the value for the argument (int, double, string, vector of strings - - )

- optional means that the use doesn't have to supply it.

- named means that it is identified by following an "-c" or "--long-name" identifier. All named arguments are optional.

Unnamed arguments are expected to appear in order of addition on the command line. Named arguments can be passed in any order (and mixed with the unnamed arguments).

The special argument "--" means that all remaining arguments are treated as unnamed (so you can pass file names that begin with -).

When calling a new_foo function to create an argument the following can/must be passed in

- For all arguments + The place to put the value. + A description of the value of the argument (i.e. it is a filename) + A description of the argument as a whole (i.e. it is the input file).

- For named arguments + A character for the short name. + A string for the long name.

When the program is called if "--help" is passed as an argument the useage information is printed and the program exits.

There is always an implicit "-v" flag for verbose which sets the dsr::verbose variable and a "-V" which sets the VERBOSE variable.

Any extra arguments or arguments with unexpected types are treated as errors and cause the program to abort. Extra arguments can be allowed for by adding a std::vector<std::string> to store them using the "set_string_vector" function. All extra (unnamed) arguments are placed there.

This software is not subject to copyright protection and is in the public domain. Neither Stanford nor the author assume any responsibility whatsoever for its use by other parties, and makes no guarantees, expressed or implied, about its quality, reliability, or any other characteristic.

An example using the class is:

#ifdef HAVE_CONFIG_H

#include <config.h>

#endif

#include "Argument_helper.h"

#include <iostream>

std::string input_filename, output_filename;

int iv, oiv, niv;

int main(int argc, const char* argv[]){

dsr::Argument_helper ah;

ah.new_string("input_filename.type", "The name of the input file",

input_filename);

ah.new_string("output_filename.type", "The name of the output file",

output_filename);

ah.new_int("count", "Some integer", iv);

ah.new_optional_int("opt_count", "Some optional integer", oiv);

ah.new_named_int('i', "integer", "named_int", "Some named integer", niv);

ARGUMENT_HELPER_BASICS(ah);

ah.set_description("A program");

ah.set_author("Daniel Russel, drussel@graphics.stanford.edu");

ah.process(argc, argv);

//test(argv);

ah.write_values(std::cout);

return 0;

}

Download gratuit

Documentul este oferit gratuit,
trebuie doar să te autentifici in contul tău.

Structură de fișiere:
  • Parser de Argumente C.doc
Alte informații:
Tipuri fișiere:
doc
Diacritice:
Nu
Nota:
8/10 (3 voturi)
Nr fișiere:
1 fisier
Pagini (total):
9 pagini
Imagini extrase:
8 imagini
Nr cuvinte:
1 926 cuvinte
Nr caractere:
10 903 caractere
Marime:
18.00KB (arhivat)
Publicat de:
Anonymous A.
Nivel studiu:
Facultate
Tip document:
Laborator
Domeniu:
Engleză
Tag-uri:
process, argv, write
Predat:
la facultate
Materie:
Engleză
Profesorului:
Popescu Marin
Sus!