casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AlmaArg.h
Go to the documentation of this file.
1 
2 
3 #include <stdcasa/optionparser.h>
4 #include <string>
5 #include <iostream>
6 #include <cstdlib>
7 #include <climits>
8 #include <alma/ASDM/Misc.h>
9 
10 #if !defined(ALMAARG_H)
11 
12 namespace alma {
13 
14  using namespace asdm;
15 
16  // extend the option::Arg struct to provide the necessary argument checking for the alma apps
17  // for use with the optionparser suite
18 
19  struct AlmaArg: public option::Arg
20  {
21  static void printError(const char* msg1, const option::Option& opt, const char* msg2)
22  {
23  std::cerr << msg1 << std::string(opt.name,opt.namelen) << msg2 << std::endl;
24  }
25 
26  // unknown argument
27  static option::ArgStatus Unknown(const option::Option& option, bool msg)
28  {
29  if (msg) printError("Unknown option '", option, "'\n");
30  return option::ARG_ILLEGAL;
31  }
32 
33  // an associated value is required opt=arg, no constraints on type
34  static option::ArgStatus Required(const option::Option& option, bool msg)
35  {
36  if (option.arg != 0)
37  return option::ARG_OK;
38 
39  if (msg) printError("Option '", option, "' requires an argument\n");
40  return option::ARG_ILLEGAL;
41  }
42 
43  // an associated value is required and must be an a long integer
44  static option::ArgStatus Long(const option::Option& option, bool msg)
45  {
46  char* endptr = 0;
47  if (option.arg != 0 && strtol(option.arg, &endptr, 10)){};
48  if (endptr != option.arg && *endptr == 0)
49  return option::ARG_OK;
50 
51  if (msg) printError("Option '", option, "' requires a long integer argument\n");
52  return option::ARG_ILLEGAL;
53  }
54 
55  // an associated value is required and must be an unsigned int
56  static option::ArgStatus uInt(const option::Option& option, bool msg)
57  {
58  char* endptr = 0;
59  unsigned long ulval = 0;
60  // uses strtoul, but must also check that converted value would fit in an unsigned int
61  if (option.arg != 0 && (ulval=strtoul(option.arg, &endptr, 10))){};
62  if (endptr != option.arg && *endptr == 0 && ulval <= UINT_MAX)
63  return option::ARG_OK;
64 
65  if (msg) printError("Option '", option, "' requires an unsigned integer argument\n");
66  return option::ARG_ILLEGAL;
67  }
68 
69  // an associated value is required and must be a valid floating point (double)
70  static option::ArgStatus Float(const option::Option& option, bool msg)
71  {
72  char* endptr = 0;
73  if (option.arg != 0 && strtod(option.arg, &endptr)){};
74  if (endptr != option.arg && *endptr == 0)
75  return option::ARG_OK;
76 
77  if (msg) printError("Option '", option, "' requires a floating point argument\n");
78  return option::ARG_ILLEGAL;
79  }
80 
81  // an associated value is required and must be either "true" or "false"
82  // ignores case
83  static option::ArgStatus Bool(const option::Option& option, bool msg)
84  {
85  if (option.arg != 0) {
86  std::string argVal(option.arg);
87  trim(argVal);
88  argVal = str_tolower(argVal);
89  if (argVal == "true" || argVal == "false")
90  return option::ARG_OK;
91  }
92  if (msg) printError("Option '", option, "' requires a value of 'true' or 'false'\n");
93  return option::ARG_ILLEGAL;
94  }
95  };
96 
97 }
98 
99 #define ALMAARG_H
100 #endif
This is the only file required to use The Lean Mean C++ Option Parser.
! The argument is acceptable for the option.
Definition: optionparser.h:256
static option::ArgStatus Float(const option::Option &option, bool msg)
an associated value is required and must be a valid floating point (double)
Definition: AlmaArg.h:70
static option::ArgStatus Bool(const option::Option &option, bool msg)
an associated value is required and must be either &quot;true&quot; or &quot;false&quot; ignores case ...
Definition: AlmaArg.h:83
static void printError(const char *msg1, const option::Option &opt, const char *msg2)
Definition: AlmaArg.h:21
const char * arg
Pointer to this Option&#39;s argument (if any).
Definition: optionparser.h:489
A parsed option from the command line together with its argument if it has one.
Definition: optionparser.h:442
Functions for checking the validity of option arguments.
Definition: optionparser.h:923
extend the option::Arg struct to provide the necessary argument checking for the alma apps for use wi...
Definition: AlmaArg.h:19
static option::ArgStatus Unknown(const option::Option &option, bool msg)
unknown argument
Definition: AlmaArg.h:27
int namelen
The length of the option name.
Definition: optionparser.h:510
static option::ArgStatus uInt(const option::Option &option, bool msg)
an associated value is required and must be an unsigned int
Definition: AlmaArg.h:56
TableExprNode trim(const TableExprNode &node)
Definition: ExprNode.h:1541
static option::ArgStatus Required(const option::Option &option, bool msg)
an associated value is required opt=arg, no constraints on type
Definition: AlmaArg.h:34
ArgStatus
Possible results when checking if an argument is valid for a certain option.
Definition: optionparser.h:251
static option::ArgStatus Long(const option::Option &option, bool msg)
an associated value is required and must be an a long integer
Definition: AlmaArg.h:44
! The argument is not acceptable and that&#39;s fatal.
Definition: optionparser.h:260
const char * name
The name of the option as used on the command line.
Definition: optionparser.h:481