• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

STEllAR-GROUP / hpx / #882

31 Aug 2023 07:44PM UTC coverage: 41.798% (-44.7%) from 86.546%
#882

push

19442 of 46514 relevant lines covered (41.8%)

126375.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/libs/core/program_options/examples/response_file.cpp
1
// Copyright Vladimir Prus 2002-2004.
2
//  SPDX-License-Identifier: BSL-1.0
3
// Distributed under the Boost Software License, Version 1.0.
4
// (See accompanying file LICENSE_1_0.txt
5
// or copy at http://www.boost.org/LICENSE_1_0.txt)
6

7
/** This example shows how to handle response file.
8

9
    For a test, build and run:
10
       response_file -I foo @response_file.rsp
11

12
    The expected output is:
13
      Include paths: foo bar biz
14

15
    Thanks to Hartmut Kaiser who raised the issue of response files
16
    and discussed the possible approach.
17
*/
18

19
#include <hpx/modules/program_options.hpp>
20
#include <hpx/modules/string_util.hpp>
21

22
#include <algorithm>
23
#include <fstream>
24
#include <iostream>
25
#include <iterator>
26
#include <string>
27
#include <utility>
28
#include <vector>
29

30
#include <hpx/config/warnings_prefix.hpp>
31

32
using namespace hpx::program_options;
33
using namespace std;
34

35
// Additional command line parser which interprets '@something' as a
×
36
// option "config-file" with the value "something"
37
pair<string, string> at_option_parser(string const& s)
×
38
{
×
39
    if ('@' == s[0])
40
        return std::make_pair(string("response-file"), s.substr(1));
41
    else
42
        return pair<string, string>();
43
}
×
44

45
int main(int ac, char* av[])
46
{
47
    try
×
48
    {
49
        options_description desc("Allowed options");
×
50
        // clang-format off
×
51
        desc.add_options()
×
52
            ("help", "produce a help message")
53
            ("include-path,I", value< vector<string> >()->composing(),
×
54
                 "include path")
×
55
            ("magic", value<int>(), "magic value")
56
            ("response-file", value<string>(),
57
                 "can be specified with '@name', too")
58
        ;
59
        // clang-format on
×
60

×
61
        variables_map vm;
62
        store(command_line_parser(ac, av)
×
63
                  .options(desc)
×
64
                  .extra_parser(at_option_parser)
65
                  .run(),
66
            vm);
×
67

68
        if (vm.count("help"))
×
69
        {
70
            cout << desc;
×
71
        }
72
        if (vm.count("response-file"))
73
        {
×
74
            // Load the file and tokenize it
×
75
            ifstream ifs(vm["response-file"].as<string>().c_str());
76
            if (!ifs)
77
            {
×
78
                cout << "Could not open the response file: "
79
                     << vm["response-file"].as<string>() << "\n";
80
                return 1;
81
            }
82

×
83
            // Read the whole file into a string
×
84
            stringstream ss;
85
            ss << ifs.rdbuf();
86

87
            // Split the file content
×
88
            string sstr = ss.str();
89
            hpx::string_util::char_separator sep(" \n\r");
90
            hpx::string_util::tokenizer tok(sstr, sep);
×
91

×
92
            vector<string> args;
93
            copy(tok.begin(), tok.end(), back_inserter(args));
94

×
95
            // Parse the file and store the options
×
96
            store(command_line_parser(args).options(desc).run(), vm);
97
        }
×
98

99
        if (vm.count("include-path"))
×
100
        {
×
101
            vector<string> const& s = vm["include-path"].as<vector<string>>();
102
            cout << "Include paths: ";
×
103
            copy(s.begin(), s.end(), ostream_iterator<string>(cout, " "));
104
            cout << "\n";
×
105
        }
106
        if (vm.count("magic"))
×
107
        {
108
            cout << "Magic value: " << vm["magic"].as<int>() << "\n";
×
109
        }
×
110
    }
111
    catch (std::exception& e)
×
112
    {
×
113
        cout << e.what() << "\n";
114
    }
115
    return 0;
116
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc