DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
utlCommandLineParser.h
Go to the documentation of this file.
1 
18 #ifndef __utlCommandLineParser_h
19 #define __utlCommandLineParser_h
20 
21 #include <cstdio>
22 #include <cstdlib>
23 #include <string>
24 #include <cstring>
25 #include "utlCoreMacro.h"
26 
27 #if UTL_OS==1
28 #include <unistd.h>
29 #endif
30 
43 #define utl_usage(usage) utl::utlOption((char*)NULL,(unsigned int)argc,(char**)argv,(char*)NULL,(char*)usage)
44 
49 #define utl_help(str) utl::utlOption((char*)NULL,(unsigned int)argc,(char**)argv,str,(char*)NULL)
50 
58 #define utl_option(name,defaut,usage) utl::utlOption((char*)name,(unsigned int)argc,(char**)argv,defaut,(char*)usage)
59 
65 #define utl_showdoc(argu) if (utl_option(argu,(const char *)NULL,0)) return 0;
66 
67 namespace utl
68 {
69 
70 
71 inline const char*
72 utlOption(const char *const name,const unsigned int argc,char **argv,const char *const defaut, const char *const usage=NULL)
73 {
74  static bool first=true, visu=false;
75  const char *res = NULL;
76  if (first)
77  {
78  first=false;
79  visu = utlOption("-h",argc,argv,(const char*)NULL)!=NULL;
80  visu |= utlOption("-help",argc,argv,(char*)NULL)!=NULL;
81  visu |= utlOption("--help",argc,argv,(char*)NULL)!=NULL;
82  if (argc==1)
83  visu=true;
84  }
85  if (!name && visu)
86  {
87  if (usage)
88  {
89  std::string argv0Str(argv[0]);
90  unsigned found = argv0Str.find_last_of("/\\");
91  std::fprintf(stderr,"\n %s",utl::GetColoredString(argv0Str.substr(0,found), COLOR_RED).c_str());
92  std::fprintf(stderr," : %s",usage);
93  std::fprintf(stderr," (%s, %s)\n\n",__DATE__,__TIME__);
94  }
95  if (defaut)
96  std::fprintf(stderr,"%s\n",defaut);
97  }
98  if (name)
99  {
100  if (argc>0)
101  {
102  unsigned int k=0;
103  while (k<argc && strcmp(argv[k],name))
104  k++;
105  res=(k++==argc?defaut:(k==argc?argv[--k]:argv[k]));
106  }
107  else
108  res = defaut;
109  if (visu && usage)
110  std::fprintf(stderr," %-8s = %-12s : %s\n", utl::GetColoredString(std::string(name),COLOR_BOLD).c_str(),res?res:"NULL",
111  utl::GetColoredString(std::string(usage),COLOR_PURPLE).c_str());
112  }
113  return res;
114 }
115 
116 inline bool
117 utlOption(const char *const name,const unsigned int argc,char **argv, const bool defaut,const char *const usage=NULL)
118 {
119  const char *s = utlOption(name,argc,argv,(const char*)NULL);
120  const bool res = s?(strcmp(s,"false") && strcmp(s,"FALSE") && strcmp(s,"off") && strcmp(s,"OFF") && strcmp(s,"0")):defaut;
121  utlOption(name,0,NULL,res?"true":"false",usage);
122  return res;
123 }
124 
125 inline int
126 utlOption(const char *const name,const unsigned int argc,char **argv, const int defaut,const char *const usage=NULL)
127 {
128  const char *s = utlOption(name,argc,argv,(const char*)NULL);
129  const int res = s?atoi(s):defaut;
130  char tmp[256];
131  std::sprintf(tmp,"%d",res);
132  utlOption(name,0,NULL,tmp,usage);
133  return res;
134 }
135 
136 inline char
137 utlOption(const char *const name,const unsigned int argc,char **argv, const char defaut,const char *const usage=NULL)
138 {
139  const char *s = utlOption(name,argc,argv,(const char*)NULL);
140  const char res = s?s[0]:defaut;
141  char tmp[8];
142  tmp[0] = res;
143  tmp[1] ='\0';
144  utlOption(name,0,NULL,tmp,usage);
145  return res;
146 }
147 
148 inline double
149 utlOption(const char *const name,const unsigned int argc,char **argv, const double defaut,const char *const usage=NULL)
150 {
151  const char *s = utlOption(name,argc,argv,(const char*)NULL);
152  const double res = s?atof(s):defaut;
153  char tmp[256];
154  std::sprintf(tmp,"%g",res);
155  utlOption(name,0,NULL,tmp,usage);
156  return res;
157 }
158 
159 }
162 #endif
163 
164 
Definition: utl.h:90
std::string GetColoredString(const std::string &str, const int color)
Definition: utlCoreMacro.h:114
const char * utlOption(const char *const name, const unsigned int argc, char **argv, const char *const defaut, const char *const usage=NULL)
macros for utlCore