MOOCHO (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OptionsFromStreamPack_CommandLineOptionsFromStreamProcessor.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
5 // Copyright (2003) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
43 
44 // Define this if you want to debug the parser
45 //#define PRINT_COMMAND_LINE_OPTIONS_FROM_STREAM_PROCESSOR_TRACE
46 
47 namespace OptionsFromStreamPack {
48 
50  const std::string &options_file_name_opt_name
51  ,const std::string &options_file_name_opt_doc
52  ,const std::string &options_file_name
53  ,const std::string &extra_options_str_opt_name
54  ,const std::string &extra_options_str_opt_doc
55  ,const std::string &extra_options_str
56  )
57  :options_file_name_opt_name_(options_file_name_opt_name)
58  ,options_file_name_opt_doc_(options_file_name_opt_doc)
59  ,options_file_name_(options_file_name)
60  ,extra_options_str_opt_name_(extra_options_str_opt_name)
61  ,extra_options_str_opt_doc_(extra_options_str_opt_doc)
62  ,extra_options_str_(extra_options_str)
63 {}
64 
66  Teuchos::RCP<OptionsFromStream> const& options
67  )
68 {
69  options_ = options;
70 }
71 
74 {
75  return options_;
76 }
77 
80  )
81 {
82  clp->setOption(options_file_name_opt_name().c_str(),&options_file_name_,options_file_name_opt_doc().c_str());
83  clp->setOption(extra_options_str_opt_name().c_str(),&extra_options_str_,extra_options_str_opt_doc().c_str());
84 }
85 
87 {
88  // Process the file options first
89  if(options_file_name_.length()) {
90  std::ifstream options_in(options_file_name_.c_str());
91  if(options_in) {
92  if(!options_.get())
94  options_->read_options(options_in);
95  }
96  }
97  // Process the extra commandline options
98  const int len = extra_options_str_.length();
99  if(len) {
100  if(!options_.get())
102  const char colon = ':';
103  const std::string::size_type npos = std::string::npos;
104  std::ostringstream ooptsstream;
105  ooptsstream << "\nbegin_options\n\n";
106  std::string::size_type start_i = 0, last_i = 0;
107  while(true) {
108  last_i = extra_options_str_.find(colon,start_i);
109  std::string optgroup = extra_options_str_.substr(start_i,last_i-start_i);
110 #ifdef PRINT_COMMAND_LINE_OPTIONS_FROM_STREAM_PROCESSOR_TRACE
111  std::cout << "\nstart_i = " << start_i;
112  std::cout << "\nlast_i = " << last_i;
113  std::cout << "\noptgroup (before replacement) = \""<<optgroup<<"\"\n";
114 #endif
115  std::replace( optgroup.begin(), optgroup.end(), ',',';' ); // See above!
116 #ifdef PRINT_COMMAND_LINE_OPTIONS_FROM_STREAM_PROCESSOR_TRACE
117  std::cout << "\noptgroup (after replacement) = \""<<optgroup<<"\"\n";
118 #endif
119  ooptsstream << "options_group " << optgroup << "\n";
120  if(last_i == npos) break;
121  start_i = last_i + 1;
122  }
123  ooptsstream << "\nend_options\n";
124  const std::string options_str = ooptsstream.str();
125 #ifdef PRINT_COMMAND_LINE_OPTIONS_FROM_STREAM_PROCESSOR_TRACE
126  std::cout << "options_str:\n" << options_str;
127 #endif
128  std::istringstream ioptsstream(options_str);
129  options_->read_options(ioptsstream);
130  }
131 }
132 
135 {
136  process_options();
137  return get_options();
138 }
139 
140 } // end namespace OptionsFromStreamPack
Extracts options from a text stream and then allows convenient access to them.
AbstractLinAlgPack::size_type size_type
void setup_commandline_processor(Teuchos::CommandLineProcessor *clp)
Setup a comandline processor before it processes commandline options or reads form a file...
Teuchos::RCP< OptionsFromStream > get_options() const
Just return the OptionsFromStream object in its current state.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
void set_options(Teuchos::RCP< OptionsFromStream > const &options)
Set the OptionsFromStream object that will be used to fill options in to.
void process_options()
Read the options file and/or process the commandline options.
Teuchos::RCP< OptionsFromStream > process_and_get_options()
Calls process_options() and returns get_options()
CommandLineOptionsFromStreamProcessor(const std::string &options_file_name_opt_name="ofs-options-file", const std::string &options_file_name_opt_doc="The name of the file containing input options for OptionsFromStream object.", const std::string &options_file_name="", const std::string &extra_options_str_opt_name="ofs-extra-options", const std::string &extra_options_str_opt_doc="Extra options in format \"OptGrp1{name1=val1,...,namen=valn}:OptGr2{name1=val1,...,namen=valn}:...\"", const std::string &extra_options_str="")
Construct with default values for the options file name and extra options.