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 <SYS/SYS_ParseNumber.h>
19 #include <SYS/SYS_TypeDecorate.h>
20 #include <SYS/SYS_Types.h>
21 #include <stdlib.h>
22 
23 #define UT_MAX_OPTIONS 4 // A maximum of three args following
24  // an option
25 class UT_WorkBuffer;
26 class UT_String;
27 class UT_StringRef;
28 class UT_StringHolder;
29 
31 {
32 public:
33  /// Default constructor
34  UT_Args();
35  /// Convenience constructor. This is equivalent to: @code
36  /// UT_Args args;
37  /// args.initialize(argc, argv);
38  /// args.stripOptions(options);
39  /// @endcode
40  UT_Args(int argc, const char *const argv[], const char *options);
41  /// Destructor
42  ~UT_Args() = default;
43 
44  // UT_Args is not copyable and also non-trivially relocatable because it
45  // has self-references from my_opt into pointers owned by myArgv
46  UT_Args(const UT_Args &) = delete;
47  UT_Args &operator=(const UT_Args &) = delete;
48 
49  int argc() const { return my_argc; }
50  const char *argv(unsigned i) const { return myArgv(i); }
51  const char *const *argv() const { return myArgv.getArgv(); }
52  const char *operator[](unsigned i) const
53  {
54  return (i < my_argc) ? argv(i) : 0;
55  }
56  const char *operator()(unsigned i) const
57  {
58  UT_ASSERT_P(i < my_argc);
59  return argv(i);
60  }
61 
62  int found(int opt) const
63  {
64  UT_ASSERT_P(opt >= 0 && opt < 128);
65  return my_found[opt];
66  }
67  const char *argp(int opt, int which=0) const
68  {
69  UT_ASSERT_P(opt >= 0 && opt < 128);
70  UT_ASSERT_P(which >= 0 && which < UT_MAX_OPTIONS);
71  return my_opt[opt][which];
72  }
73  const char *argp2(int opt) const
74  {
75  return my_opt[opt][1];
76  }
77  const char *argp3(int opt) const
78  {
79  UT_ASSERT_P(opt >= 0 && opt < 128);
80  return my_opt[opt][2];
81  }
82 
83  fpreal fargp(int opt, int which=0) const
84  {
85  UT_ASSERT_P(opt >= 0 && opt < 128);
86  UT_ASSERT_P(which >= 0 && which < UT_MAX_OPTIONS);
87  return SYSatof(my_opt[opt][which]);
88  }
89  fpreal fargp2(int opt) const { return fargp(opt, 1); }
90  fpreal fargp3(int opt) const { return fargp(opt, 2); }
91 
92  int iargp(int opt, int which=0) const
93  {
94  UT_ASSERT_P(opt >= 0 && opt < 128);
95  UT_ASSERT_P(which >= 0 && which < UT_MAX_OPTIONS);
96  return SYSatoi(my_opt[opt][which]);
97  }
98  int iargp2(int opt) const { return (int)fargp(opt, 1); }
99  int iargp3(int opt) const { return (int)fargp(opt, 2); }
100 
101  void initialize(int argc, const char *const argv[]);
102  /// Initialize with an array of UT_StringRef objects. This method
103  /// extracts the c_str() from the UT_StringRef, so the UT_StringRef objects
104  /// need to remain in scope while the UT_Args is in existence.
105  void initialize(int argc, const UT_StringRef *argv);
106  void initialize(int argc, const UT_StringHolder *argv);
107  void appendArg(const char *arg);
108  void stripOptions(const char *options);
109 
110  /// This method will make a command string out of all the arguments (and
111  /// options). Arguments will be quoted according to 'csh' syntax.
112  int fillCommandLine(UT_WorkBuffer &buf,
113  int first_arg=0,
114  int include_options=1) const;
115 
116  /// This is a version of fillCommandLine that takes a UT_String for the
117  /// result.
118  void makeCommandLine(UT_String &str, int first_arg=1) const;
119 
120  /// Set an argument to a specific value
121  void setArgument(int idx, const char *text);
122 
123  /// Dump options
124  void display();
125 
126 private:
127  int my_argc; // Arg count (after options)
128  UT_WorkArgs myArgv;
129  char my_found[128]; // Options found (one for each char)
130  const char *my_opt[128][UT_MAX_OPTIONS];
131 };
132 
134 
135 #endif
int iargp(int opt, int which=0) const
Definition: UT_Args.h:92
fpreal fargp3(int opt) const
Definition: UT_Args.h:90
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2540
int iargp3(int opt) const
Definition: UT_Args.h:99
const char * operator[](unsigned i) const
Definition: UT_Args.h:52
const char * argv(unsigned i) const
Definition: UT_Args.h:50
int found(int opt) const
Definition: UT_Args.h:62
#define UT_API
Definition: UT_API.h:14
const char *const * argv() const
Definition: UT_Args.h:51
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
Definition: core.h:1736
SYS_DECLARE_IS_NOT_TR(UT_Args)
int iargp2(int opt) const
Definition: UT_Args.h:98
const char * argp2(int opt) const
Definition: UT_Args.h:73
fpreal fargp(int opt, int which=0) const
Definition: UT_Args.h:83
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:155
int argc() const
Definition: UT_Args.h:49
fpreal fargp2(int opt) const
Definition: UT_Args.h:89
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:56
const char * argp3(int opt) const
Definition: UT_Args.h:77
fpreal64 fpreal
Definition: SYS_Types.h:277
LeafData & operator=(const LeafData &)=delete
#define UT_MAX_OPTIONS
Definition: UT_Args.h:23
const char * argp(int opt, int which=0) const
Definition: UT_Args.h:67