• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright Sascha Ochsenknecht 2009.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt
4 // or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 #include <boost/program_options.hpp>
7 using namespace boost::program_options;
8 
9 #include <string>
10 #include <iostream>
11 #include <fstream>
12 using namespace std;
13 
14 #include "minitest.hpp"
15 
16 
required_throw_test()17 void required_throw_test()
18 {
19    options_description          opts;
20    opts.add_options()
21      ("cfgfile,c", value<string>()->required(), "the configfile")
22      ("fritz,f",   value<string>()->required(), "the output file")
23    ;
24 
25    variables_map vm;
26    bool thrown = false;
27    {
28       // This test must throw exception
29       string cmdline = "prg -f file.txt";
30       vector< string > tokens =  split_unix(cmdline);
31       thrown = false;
32       try {
33          store(command_line_parser(tokens).options(opts).run(), vm);
34          notify(vm);
35       }
36       catch (required_option& e) {
37          BOOST_CHECK_EQUAL(e.what(), string("the option '--cfgfile' is required but missing"));
38          thrown = true;
39       }
40       BOOST_CHECK(thrown);
41    }
42 
43    {
44       // This test mustn't throw exception
45       string cmdline = "prg -c config.txt";
46       vector< string > tokens =  split_unix(cmdline);
47       thrown = false;
48       try {
49          store(command_line_parser(tokens).options(opts).run(), vm);
50          notify(vm);
51       }
52       catch (required_option& e) {
53          thrown = true;
54       }
55       BOOST_CHECK(!thrown);
56    }
57 }
58 
59 
60 
simple_required_test(const char * config_file)61 void simple_required_test(const char* config_file)
62 {
63    options_description          opts;
64    opts.add_options()
65      ("cfgfile,c", value<string>()->required(), "the configfile")
66      ("fritz,f",   value<string>()->required(), "the output file")
67    ;
68 
69    variables_map vm;
70    bool thrown = false;
71    {
72       // This test must throw exception
73       string cmdline = "prg -f file.txt";
74       vector< string > tokens =  split_unix(cmdline);
75       thrown = false;
76       try {
77          // options coming from different sources
78          store(command_line_parser(tokens).options(opts).run(), vm);
79          store(parse_config_file<char>(config_file, opts), vm);
80          notify(vm);
81       }
82       catch (required_option& e) {
83          thrown = true;
84       }
85       BOOST_CHECK(!thrown);
86    }
87 }
88 
multiname_required_test()89 void multiname_required_test()
90 {
91    options_description          opts;
92    opts.add_options()
93      ("foo,bar", value<string>()->required(), "the foo")
94    ;
95 
96    variables_map vm;
97    bool thrown = false;
98    {
99       // This test must throw exception
100       string cmdline = "prg --bar file.txt";
101       vector< string > tokens =  split_unix(cmdline);
102       thrown = false;
103       try {
104          // options coming from different sources
105          store(command_line_parser(tokens).options(opts).run(), vm);
106          notify(vm);
107       }
108       catch (required_option& e) {
109          thrown = true;
110       }
111       BOOST_CHECK(!thrown);
112    }
113 }
114 
115 
116 
main(int,char * av[])117 int main(int /*argc*/, char* av[])
118 {
119    required_throw_test();
120    simple_required_test(av[1]);
121    multiname_required_test();
122 
123    return 0;
124 }
125 
126