Developer documentation
Version 3.0.3-105-gd3941f44
cmdline_option.h
Go to the documentation of this file.
1/* Copyright (c) 2008-2022 the MRtrix3 contributors.
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 *
7 * Covered Software is provided under this License on an "as is"
8 * basis, without warranty of any kind, either expressed, implied, or
9 * statutory, including, without limitation, warranties that the
10 * Covered Software is free of defects, merchantable, fit for a
11 * particular purpose or non-infringing.
12 * See the Mozilla Public License v. 2.0 for more details.
13 *
14 * For more details, see http://www.mrtrix.org/.
15 */
16
17#ifndef __cmdline_option_h__
18#define __cmdline_option_h__
19
20#include <cassert>
21#include <limits>
22#include <string>
23
24#ifdef None
25# undef None
26#endif
27
28#include "mrtrix.h"
29#include "types.h"
30
31namespace MR
32{
33 namespace App
34 {
35
44 enum ArgType {
45 Undefined,
46 Text,
47 Boolean,
48 Integer,
49 Float,
50 ArgFileIn,
51 ArgFileOut,
52 ArgDirectoryIn,
53 ArgDirectoryOut,
54 Choice,
55 ImageIn,
56 ImageOut,
57 IntSeq,
58 FloatSeq,
59 TracksIn,
60 TracksOut,
61 Various
62 };
63
64 using ArgFlags = int;
65 constexpr ArgFlags None = 0;
66 constexpr ArgFlags Optional = 0x1;
67 constexpr ArgFlags AllowMultiple = 0x2;
69
70
71
72
74 // @{
75
77
106 public:
108
111 Argument (const char* name = nullptr, std::string description = std::string()) :
112 id (name), desc (description), type (Undefined), flags (None)
113 {
114 memset (&limits, 0x00, sizeof (limits));
115 }
116
118 const char* id;
120 std::string desc;
122 ArgType type;
124 ArgFlags flags;
125
127 union {
128 const char* const* choices;
129 struct { NOMEMALIGN
130 int64_t min, max;
131 } i;
132 struct { NOMEMALIGN
134 } f;
136
137
138 operator bool () const {
139 return id;
140 }
141
143
154 flags |= Optional;
155 return *this;
156 }
157
159
161 flags |= AllowMultiple;
162 return *this;
163 }
164
167 assert (type == Undefined);
168 type = Text;
169 return *this;
170 }
171
174 assert (type == Undefined);
175 type = ImageIn;
176 return *this;
177 }
178
181 assert (type == Undefined);
182 type = ImageOut;
183 return *this;
184 }
185
187
188 Argument& type_integer (const int64_t min = std::numeric_limits<int64_t>::min(), const int64_t max = std::numeric_limits<int64_t>::max()) {
189 assert (type == Undefined);
190 type = Integer;
191 limits.i.min = min;
192 limits.i.max = max;
193 return *this;
194 }
195
197
199 assert (type == Undefined);
200 type = Boolean;
201 return *this;
202 }
203
205
206 Argument& type_float (const default_type min = -std::numeric_limits<default_type>::infinity(),
207 const default_type max = std::numeric_limits<default_type>::infinity()) {
208 assert (type == Undefined);
209 type = Float;
210 limits.f.min = min;
211 limits.f.max = max;
212 return *this;
213 }
214
216
226 Argument& type_choice (const char* const* choices) {
227 assert (type == Undefined);
228 type = Choice;
229 limits.choices = choices;
230 return *this;
231 }
232
235 assert (type == Undefined);
236 type = ArgFileIn;
237 return *this;
238 }
239
242 assert (type == Undefined);
243 type = ArgFileOut;
244 return *this;
245 }
246
249 assert (type == Undefined);
250 type = ArgDirectoryIn;
251 return *this;
252 }
253
256 assert (type == Undefined);
257 type = ArgDirectoryOut;
258 return *this;
259 }
260
263 assert (type == Undefined);
264 type = IntSeq;
265 return *this;
266 }
267
270 assert (type == Undefined);
271 type = FloatSeq;
272 return *this;
273 }
274
277 assert (type == Undefined);
278 type = TracksIn;
279 return *this;
280 }
281
284 assert (type == Undefined);
285 type = TracksOut;
286 return *this;
287 }
288
291 assert (type == Undefined);
292 type = Various;
293 return *this;
294 }
295
296
297 std::string syntax (int format) const;
298 std::string usage () const;
299 };
300
301
302
303
304
305
307
343 class Option : public vector<Argument> { NOMEMALIGN
344 public:
345 Option () : id (nullptr), flags (Optional) { }
346
347 Option (const char* name, const std::string& description) :
348 id (name), desc (description), flags (Optional) { }
349
351 push_back (arg);
352 return *this;
353 }
354 operator bool () const {
355 return id;
356 }
357
359 const char* id;
361 std::string desc;
363 ArgFlags flags;
364
366
379 flags &= ~Optional;
380 return (*this);
381 }
382
384
386 flags |= AllowMultiple;
387 return *this;
388 }
389
390 bool is (const std::string& name) const {
391 return name == id;
392 }
393
394 std::string syntax (int format) const;
395 std::string usage () const;
396 };
397
398
399
400
401
402
404
418 class OptionGroup : public vector<Option> { NOMEMALIGN
419 public:
420 OptionGroup (const char* group_name = "OPTIONS") : name (group_name) { }
421 const char* name;
422
424 push_back (option);
425 return *this;
426 }
427
429 assert (!empty());
430 back() + argument;
431 return *this;
432 }
433
435 if (empty())
436 push_back (Option());
437 return vector<Option>::back();
438 }
439
440 std::string header (int format) const;
441 std::string contents (int format) const;
442 static std::string footer (int format);
443 };
444
445
446
447 }
448
450}
451
452#endif
A class to specify a command-line argument.
std::string desc
the argument description
ArgType type
the argument type
Argument & type_file_out()
specifies that the argument should be an output file
std::string usage() const
default_type max
Argument & type_bool()
specifies that the argument should be a boolean
Argument & type_image_in()
specifies that the argument should be an input image
Argument & allow_multiple()
specifies that multiple such arguments can be specified
Argument & type_float(const default_type min=-std::numeric_limits< default_type >::infinity(), const default_type max=std::numeric_limits< default_type >::infinity())
specifies that the argument should be a floating-point value
Argument & type_directory_in()
specifies that the argument should be an input directory
Argument & type_file_in()
specifies that the argument should be an input file
Argument & type_image_out()
specifies that the argument should be an output image
Argument & type_text()
specifies that the argument should be a text string
Argument & type_directory_out()
specifies that the argument should be an output directory
Argument & type_integer(const int64_t min=std::numeric_limits< int64_t >::min(), const int64_t max=std::numeric_limits< int64_t >::max())
specifies that the argument should be an integer
Argument & type_sequence_float()
specifies that the argument should be a sequence of comma-separated floating-point values.
union MR::App::Argument::@0 limits
a structure to store the various parameters of the Argument
Argument & type_sequence_int()
specifies that the argument should be a sequence of comma-separated integer values
struct MR::App::Argument::@0::@1 i
struct MR::App::Argument::@0::@2 f
ArgFlags flags
the argument flags (AllowMultiple & Optional)
Argument & optional()
specifies that the argument is optional
Argument & type_choice(const char *const *choices)
specifies that the argument should be selected from a predefined list
Argument & type_various()
specifies that the argument could be one of various types
Argument & type_tracks_out()
specifies that the argument should be an output tracks file
Argument(const char *name=nullptr, std::string description=std::string())
constructor
std::string syntax(int format) const
const char * id
the argument name
const char *const * choices
Argument & type_tracks_in()
specifies that the argument should be an input tracks file
a class to hold a named list of Option's
OptionGroup & operator+(const Option &option)
static std::string footer(int format)
OptionGroup(const char *group_name="OPTIONS")
std::string header(int format) const
std::string contents(int format) const
A class to specify a command-line option.
std::string usage() const
Option & allow_multiple()
specifies that multiple such options can be specified
std::string syntax(int format) const
Option & required()
specifies that the option is required
bool is(const std::string &name) const
Option(const char *name, const std::string &description)
ArgFlags flags
option flags (AllowMultiple and/or Optional)
const char * id
the option name
std::string desc
the option description
Option & operator+(const Argument &arg)
#define NOMEMALIGN
Definition: memory.h:22
vector< ParsedArgument > argument
the list of arguments parsed from the command-line
vector< ParsedOption > option
the list of options parsed from the command-line
Definition: base.h:24
double default_type
the default type used throughout MRtrix
Definition: types.h:228
const std::string name
Definition: thread.h:108