HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_Args.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: Command Library (C++)
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __UT_Args_H__
13 #define __UT_Args_H__
14 
15 #include "UT_API.h"
16 #include "UT_WorkArgs.h"
17 #include "UT_Assert.h"
18 #include <stdlib.h>
19 #include <SYS/SYS_Types.h>
20 #include <SYS/SYS_ParseNumber.h>
21 
22 #define UT_MAX_OPTIONS 4 // A maximum of three args following
23  // an option
24 class UT_WorkBuffer;
25 class UT_String;
26 class UT_StringRef;
27 class UT_StringHolder;
28 
30 {
31 public:
32  /// Default constructor
33  UT_Args();
34  /// Convenience constructor. This is equivalent to: @code
35  /// UT_Args args;
36  /// args.initialize(argc, argv);
37  /// args.stripOptions(options);
38  /// @endcode
39  UT_Args(int argc, const char *const argv[], const char *options);
40  /// Destructor
41  ~UT_Args() = default;
42 
43  // UT_Args is not copyable and also non-trivially relocatable because it
44  // has self-references from my_opt into pointers owned by myArgv
45  UT_Args(const UT_Args &) = delete;
46  UT_Args &operator=(const UT_Args &) = delete;
47 
48  int argc() const { return my_argc; }
49  const char *argv(unsigned i) const { return myArgv(i); }
50  const char *const *argv() const { return myArgv.getArgv(); }
51  const char *operator[](unsigned i) const
52  {
53  return (i < my_argc) ? argv(i) : 0;
54  }
55  const char *operator()(unsigned i) const
56  {
57  UT_ASSERT_P(i < my_argc);
58  return argv(i);
59  }
60 
61  int found(int opt) const
62  {
63  UT_ASSERT_P(opt >= 0 && opt < 128);
64  return my_found[opt];
65  }
66  const char *argp(int opt, int which=0) const
67  {
68  UT_ASSERT_P(opt >= 0 && opt < 128);
69  UT_ASSERT_P(which >= 0 && which < UT_MAX_OPTIONS);
70  return my_opt[opt][which];
71  }
72  const char *argp2(int opt) const
73  {
74  return my_opt[opt][1];
75  }
76  const char *argp3(int opt) const
77  {
78  UT_ASSERT_P(opt >= 0 && opt < 128);
79  return my_opt[opt][2];
80  }
81 
82  fpreal fargp(int opt, int which=0) const
83  {
84  UT_ASSERT_P(opt >= 0 && opt < 128);
85  UT_ASSERT_P(which >= 0 && which < UT_MAX_OPTIONS);
86  return SYSatof(my_opt[opt][which]);
87  }
88  fpreal fargp2(int opt) const { return fargp(opt, 1); }
89  fpreal fargp3(int opt) const { return fargp(opt, 2); }
90 
91  int iargp(int opt, int which=0) const
92  {
93  UT_ASSERT_P(opt >= 0 && opt < 128);
94  UT_ASSERT_P(which >= 0 && which < UT_MAX_OPTIONS);
95  return SYSatoi(my_opt[opt][which]);
96  }
97  int iargp2(int opt) const { return (int)fargp(opt, 1); }
98  int iargp3(int opt) const { return (int)fargp(opt, 2); }
99 
100  void initialize(int argc, const char *const argv[]);
101  /// Initialize with an array of UT_StringRef objects. This method
102  /// extracts the c_str() from the UT_StringRef, so the UT_StringRef objects
103  /// need to remain in scope while the UT_Args is in existence.
104  void initialize(int argc, const UT_StringRef *argv);
105  void initialize(int argc, const UT_StringHolder *argv);
106  void appendArg(const char *arg);
107  void stripOptions(const char *options);
108 
109  /// This method will make a command string out of all the arguments (and
110  /// options). Arguments will be quoted according to 'csh' syntax.
111  int fillCommandLine(UT_WorkBuffer &buf,
112  int first_arg=0,
113  int include_options=1) const;
114 
115  /// This is a version of fillCommandLine that takes a UT_String for the
116  /// result.
117  void makeCommandLine(UT_String &str, int first_arg=1) const;
118 
119  /// Set an argument to a specific value
120  void setArgument(int idx, const char *text);
121 
122  /// Dump options
123  void display();
124 
125 private:
126  int my_argc; // Arg count (after options)
127  UT_WorkArgs myArgv;
128  char my_found[128]; // Options found (one for each char)
129  const char *my_opt[128][UT_MAX_OPTIONS];
130 };
131 #endif
int iargp(int opt, int which=0) const
Definition: UT_Args.h:91
fpreal fargp3(int opt) const
Definition: UT_Args.h:89
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2540
int iargp3(int opt) const
Definition: UT_Args.h:98
const char * operator[](unsigned i) const
Definition: UT_Args.h:51
const char * argv(unsigned i) const
Definition: UT_Args.h:49
int found(int opt) const
Definition: UT_Args.h:61
#define UT_API
Definition: UT_API.h:14
const char *const * argv() const
Definition: UT_Args.h:50
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
Definition: core.h:1736
int iargp2(int opt) const
Definition: UT_Args.h:97
const char * argp2(int opt) const
Definition: UT_Args.h:72
fpreal fargp(int opt, int which=0) const
Definition: UT_Args.h:82
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:155
int argc() const
Definition: UT_Args.h:48
fpreal fargp2(int opt) const
Definition: UT_Args.h:88
OPENVDB_API void initialize()
Global registration of native Grid, Transform, Metadata and Point attribute types. Also initializes blosc (if enabled).
Definition: logging.h:294
const char * operator()(unsigned i) const
Definition: UT_Args.h:55
const char * argp3(int opt) const
Definition: UT_Args.h:76
fpreal64 fpreal
Definition: SYS_Types.h:277
#define UT_MAX_OPTIONS
Definition: UT_Args.h:22
const char * argp(int opt, int which=0) const
Definition: UT_Args.h:66