Commit 8192d064 authored by Marc Betoule's avatar Marc Betoule
Browse files

improve the help message

parent 94cc63b8
...@@ -58,8 +58,9 @@ int main(int argc ,const char** argv) ...@@ -58,8 +58,9 @@ int main(int argc ,const char** argv)
ArgType type[] = {FLAG, INT, INT, FLAG, STRING}; ArgType type[] = {FLAG, INT, INT, FLAG, STRING};
string usage[] = {"Inputs are alm FITS file.", "Number of iteration of anafast.", "Resolution of the output map.", "Pixel based analysis", "Pixel zone definition"}; string usage[] = {"Inputs are alm FITS file.", "Number of iteration of anafast.", "Resolution of the output map.", "Pixel based analysis", "Pixel zone definition"};
char shortopt[] = "ainpz"; char shortopt[] = "ainpz";
Args args = Args(5, longopt, shortopt, type, usage,"Perform needlet ILC.", "outfile l0 J zonefile1 lmax1 ... zonefileJ lmaxJ N infile1 coeff1 ... infileN coeffN \n -p outfile -z zonefile N infile1 coeff1 ... infileN coeffN "); Args args = Args(5, longopt, shortopt, type, usage,"Compute ILC in needlet or pixel space.");
args.add_usage("", "ain", "outfile l0 J zonefile1 lmax1 ... zonefileJ lmaxJ N infile1 coeff1 ... infileN coeffN");
args.add_usage("pz", "" ," outfile N infile1 coeff1 ... infileN coeffN");
// parameter file // parameter file
if ((argc == 2) && (argv[1][0]!='-')) if ((argc == 2) && (argv[1][0]!='-'))
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <cstdlib> #include <cstdlib>
Args::Args(int noptions, string * longoptions, char * shortoptions, ArgType * type, string * usage, string _purpose, string _fullusage){ Args::Args(int noptions, string * longoptions, char * shortoptions, ArgType * type, string * usage, string _purpose){
longOpts = new struct option [noptions+1]; longOpts = new struct option [noptions+1];
shortOpts = ""; shortOpts = "";
optionroles = ""; optionroles = "";
...@@ -36,7 +36,7 @@ Args::Args(int noptions, string * longoptions, char * shortoptions, ArgType * ty ...@@ -36,7 +36,7 @@ Args::Args(int noptions, string * longoptions, char * shortoptions, ArgType * ty
longOpts[i] = o; longOpts[i] = o;
shortOpts += shortoptions[i]; shortOpts += shortoptions[i];
shortOpts += ":"; shortOpts += ":";
optionroles += "\n--"+longoptions[i]+", -"+shortoptions[i]+ " <string>\t: " + usage[i]; optionroles += "\n --"+longoptions[i]+", -"+shortoptions[i]+ " <string>\t: " + usage[i];
} }
break; break;
case FLOAT : case FLOAT :
...@@ -46,7 +46,7 @@ Args::Args(int noptions, string * longoptions, char * shortoptions, ArgType * ty ...@@ -46,7 +46,7 @@ Args::Args(int noptions, string * longoptions, char * shortoptions, ArgType * ty
longOpts[i] = o; longOpts[i] = o;
shortOpts += shortoptions[i]; shortOpts += shortoptions[i];
shortOpts += ":"; shortOpts += ":";
optionroles += "\n--"+longoptions[i]+", -"+shortoptions[i]+ " <float>\t: " + usage[i]; optionroles += "\n --"+longoptions[i]+", -"+shortoptions[i]+ " <float>\t: " + usage[i];
} }
break; break;
case FLAG : case FLAG :
...@@ -55,7 +55,7 @@ Args::Args(int noptions, string * longoptions, char * shortoptions, ArgType * ty ...@@ -55,7 +55,7 @@ Args::Args(int noptions, string * longoptions, char * shortoptions, ArgType * ty
struct option o = {longoptions[i].c_str(), no_argument, NULL, shortoptions[i]}; struct option o = {longoptions[i].c_str(), no_argument, NULL, shortoptions[i]};
longOpts[i] = o; longOpts[i] = o;
shortOpts += shortoptions[i]; shortOpts += shortoptions[i];
optionroles += "\n--"+longoptions[i]+", -"+shortoptions[i]+ "\t: " + usage[i]; optionroles += "\n --"+longoptions[i]+", -"+shortoptions[i]+ "\t: " + usage[i];
} }
break; break;
case INT : case INT :
...@@ -65,39 +65,80 @@ Args::Args(int noptions, string * longoptions, char * shortoptions, ArgType * ty ...@@ -65,39 +65,80 @@ Args::Args(int noptions, string * longoptions, char * shortoptions, ArgType * ty
longOpts[i] = o; longOpts[i] = o;
shortOpts += shortoptions[i]; shortOpts += shortoptions[i];
shortOpts += ":"; shortOpts += ":";
optionroles += "\n--"+longoptions[i]+", -"+shortoptions[i]+ " <int>\t: " + usage[i]; optionroles += "\n --"+longoptions[i]+", -"+shortoptions[i]+ " <int>\t: " + usage[i];
} }
break; break;
} }
purpose = _purpose; purpose = _purpose;
fullusage = _fullusage;
} }
struct option o = {"help", no_argument, NULL, 'h'}; struct option o = {"help", no_argument, NULL, 'h'};
longOpts[noptions] = o; longOpts[noptions] = o;
shortOpts += "h"; shortOpts += "h";
}
void Args::add_usage(string compulsory_option_set, string optionnal_option_set, string positionnal_args){
string synopsis="";
if (flagvalues.size() > 0){ if (flagvalues.size() > 0){
synopsis += " [-"; for (map<string, int>::iterator it = flagvalues.begin(); it != flagvalues.end(); it++){
for (map<string, int>::iterator it = flagvalues.begin(); it != flagvalues.end(); it++) string sn ="";
synopsis += shortnames[it->first]; sn += shortnames[it->first];
synopsis += "] "; if (compulsory_option_set.find(sn)!=string::npos){
synopsis += " -";
synopsis += shortnames[it->first];
synopsis += " ";
}
else if (optionnal_option_set.find(sn)!=string::npos){
synopsis += " [-";
synopsis += shortnames[it->first];
synopsis += "] ";
}
}
} }
for (map<string, int>::iterator it = intvalues.begin(); it != intvalues.end(); it++){ for (map<string, int>::iterator it = intvalues.begin(); it != intvalues.end(); it++){
synopsis += "[-"; string sn = "";
synopsis += shortnames[it->first]; sn += shortnames[it->first];
synopsis += " <int>] "; if (compulsory_option_set.find(sn)!=string::npos){
synopsis += " -";
synopsis += shortnames[it->first];
synopsis += " <int> ";
}
else if (optionnal_option_set.find(sn)!=string::npos){
synopsis += " [-";
synopsis += shortnames[it->first];
synopsis += " <int>] ";
}
} }
for (map<string, double>::iterator it = floatvalues.begin(); it != floatvalues.end(); it++){ for (map<string, double>::iterator it = floatvalues.begin(); it != floatvalues.end(); it++){
synopsis += "[-"; string sn = "";
synopsis += shortnames[it->first]; sn += shortnames[it->first];
synopsis += " <float>] "; if (compulsory_option_set.find(sn)!=string::npos){
synopsis += " -";
synopsis += shortnames[it->first];
synopsis += " <float> ";
}
else if (optionnal_option_set.find(sn)!=string::npos){
synopsis += " [-";
synopsis += shortnames[it->first];
synopsis += " <float>] ";
}
} }
for (map<string, string>::iterator it = stringvalues.begin(); it != stringvalues.end(); it++){ for (map<string, string>::iterator it = stringvalues.begin(); it != stringvalues.end(); it++){
synopsis += "[-"; string sn = "";
synopsis += shortnames[it->first]; sn += shortnames[it->first];
synopsis += " <string>] "; if (compulsory_option_set.find(sn)!=string::npos){
synopsis += " -";
synopsis += shortnames[it->first];
synopsis += " <string> ";
}
else if (optionnal_option_set.find(sn)!=string::npos){
synopsis += " [-";
synopsis += shortnames[it->first];
synopsis += " <string>] ";
}
} }
synopsis += fullusage+"\n"; synopsis += positionnal_args+"\n";
vec_synopsis.push_back(synopsis);
} }
void Args::info(){ void Args::info(){
...@@ -116,8 +157,16 @@ void Args::info(){ ...@@ -116,8 +157,16 @@ void Args::info(){
} }
void Args::printusage(){ void Args::printusage(){
cerr << synopsis << endl cerr << "Synopsis:\n"
<< purpose << endl << "---------\n";
for (vector<string>::iterator it = vec_synopsis.begin(); it != vec_synopsis.end(); it++){
cerr << " " << name << *it << endl;
}
cerr << "Purpose:\n"
<< "--------\n";
cerr<< purpose << endl<<endl;
cerr << "Options:\n"
<< "--------"
<< optionroles << endl; << optionroles << endl;
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
...@@ -156,9 +205,9 @@ void Args::processarg(int val){ ...@@ -156,9 +205,9 @@ void Args::processarg(int val){
void Args::processargs(int argc, char **argv){ void Args::processargs(int argc, char **argv){
int opt = 0; int opt = 0;
int longIndex = 0; int longIndex = 0;
string name = argv[0]; name = argv[0];
synopsis = name.substr(name.find_last_of('/')+1) + synopsis; name = name.substr(name.find_last_of('/')+1);
synopsis = "Synopsis:\n" + synopsis;
opterr = 0; opterr = 0;
opt = getopt_long( argc, argv, shortOpts.c_str(), longOpts, &longIndex ); opt = getopt_long( argc, argv, shortOpts.c_str(), longOpts, &longIndex );
while( opt != -1 ) { while( opt != -1 ) {
...@@ -183,7 +232,12 @@ bool Args::operator [](string key){ ...@@ -183,7 +232,12 @@ bool Args::operator [](string key){
} }
string Args::operator[](int i){ string Args::operator[](int i){
return posarg[i]; if (i < nposarg)
return posarg[i];
else{
cerr << "Missing positionnal argument: " << i << endl;
printusage();
}
} }
void Args::def (int val,string key){ void Args::def (int val,string key){
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include <getopt.h> #include <getopt.h>
#include <string> #include <string>
#include <map> #include <map>
#include <vector>
using namespace std; using namespace std;
...@@ -28,8 +29,10 @@ class Args ...@@ -28,8 +29,10 @@ class Args
{ {
public: public:
// constructor // constructor
Args(int noptions, string * longoptions, char * shortoptions, ArgType * type, string * usage, string purpose="", string fullusage=""); Args(int noptions, string * longoptions, char * shortoptions, ArgType * type, string * usage, string purpose="");
void add_usage(string compulsory_option_set, string optionnal_option_set, string positionnal_args);
// various // various
void info(); void info();
void printusage(); void printusage();
...@@ -71,9 +74,10 @@ private: ...@@ -71,9 +74,10 @@ private:
string fullusage; string fullusage;
string shortOpts; string shortOpts;
string optionroles; string optionroles;
string synopsis; vector<string> vec_synopsis;
struct option * longOpts; struct option * longOpts;
string name;
// options storage // options storage
map<string,int> intvalues; map<string,int> intvalues;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment