Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/program_options/example/real.cpp @ 12

Last change on this file since 12 was 12, checked in by landauf, 17 years ago

added boost

File size: 3.5 KB
Line 
1// Copyright Vladimir Prus 2002-2004.
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
7#include <boost/program_options.hpp>
8using namespace boost::program_options;
9
10#include <iostream>
11using namespace std;
12
13/* Auxiliary functions for checking input for validity. */
14
15/* Function used to check that 'opt1' and 'opt2' are not specified
16   at the same time. */
17void conflicting_options(const variables_map& vm, 
18                         const char* opt1, const char* opt2)
19{
20    if (vm.count(opt1) && !vm[opt1].defaulted() 
21        && vm.count(opt2) && !vm[opt2].defaulted())
22        throw logic_error(string("Conflicting options '") 
23                          + opt1 + "' and '" + opt2 + "'.");
24}
25
26/* Function used to check that of 'for_what' is specified, then
27   'required_option' is specified too. */
28void option_dependency(const variables_map& vm,
29                        const char* for_what, const char* required_option)
30{
31    if (vm.count(for_what) && !vm[for_what].defaulted())
32        if (vm.count(required_option) == 0 || vm[required_option].defaulted())
33            throw logic_error(string("Option '") + for_what
34                              + "' requires option '" + required_option + "'.");
35}
36
37int main(int argc, char* argv[])
38{
39    try {
40        string ofile;
41        string macrofile, libmakfile;
42        bool t_given = false;
43        bool b_given = false;
44        string mainpackage;
45        string depends = "deps_file";
46        string sources = "src_file";
47        string root = ".";
48
49        options_description desc("Allowed options");
50        desc.add_options()
51        // First parameter describes option name/short name
52        // The second is parameter to option
53        // The third is description
54        ("help,h", "print usage message")
55        ("output,o", value(&ofile), "pathname for output")
56        ("macrofile,m", value(&macrofile), "full pathname of macro.h")
57        ("two,t", bool_switch(&t_given), "preprocess both header and body")
58        ("body,b", bool_switch(&b_given), "preprocess body in the header context")
59        ("libmakfile,l", value(&libmakfile), 
60             "write include makefile for library")
61        ("mainpackage,p", value(&mainpackage), 
62             "output dependency information")
63        ("depends,d", value(&depends), 
64         "write dependencies to <pathname>")
65        ("sources,s", value(&sources), "write source package list to <pathname>")
66        ("root,r", value(&root), "treat <dirname> as project root directory")
67        ;
68   
69        variables_map vm;
70        store(parse_command_line(argc, argv, desc), vm);
71
72        if (vm.count("help")) { 
73            cout << desc << "\n";
74            return 0;
75        }
76
77        conflicting_options(vm, "output", "two");
78        conflicting_options(vm, "output", "body");
79        conflicting_options(vm, "output", "mainpackage");
80        conflicting_options(vm, "two", "mainpackage");
81        conflicting_options(vm, "body", "mainpackage");
82
83        conflicting_options(vm, "two", "body");
84        conflicting_options(vm, "libmakfile", "mainpackage");
85        conflicting_options(vm, "libmakfile", "mainpackage");
86
87        option_dependency(vm, "depends", "mainpackage");
88        option_dependency(vm, "sources", "mainpackage");
89        option_dependency(vm, "root", "mainpackage");
90
91        cout << "two = " << vm["two"].as<bool>() << "\n";
92    }
93    catch(exception& e) {
94        cerr << e.what() << "\n";
95    }
96}
Note: See TracBrowser for help on using the repository browser.