commandline.i.hh 1.27 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#include "radixcommand/commandline.hh"
#include "radixbug/bug.hh"
#include <sstream>
#include <iomanip>
#include <exception>
#include <typeinfo>
namespace radix
{
template<typename return_type>
return_type CommandLine::get(const std::string& name) const
{
    return_type result;
    const auto& it = mData.find(name);
    if(it == mData.end())
    {
        std::stringstream ss;
        ss << "No such option: " << name;
        throw std::logic_error(ss.str());
20
21
    }
    else
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    {
        std::string value = std::get<0>(it->second);
        if(value.empty())
        {
            std::stringstream ss;
            ss << "Illegal attempt to convert flag '" << name << "' to a value.";
            throw std::logic_error(ss.str());
        }
        std::istringstream ss(value);
        ss >> result;
    }
    return result;
}

template<typename return_type>
return_type CommandLine::get(const std::string& name, return_type defaultValue) const
{
    return_type result = defaultValue;
    if(existsNotEmpty(name))
    {
        result = get<return_type>(name);
    }
    return result;
}
46
47
48
49
50
51
52
53
54

template<typename return_type>
return_type CommandLine::arg(size_t i) const
{
    return_type result;
    std::stringstream ss(mArgs[i]);
    ss >> result;
    return result;
}
55
} // namespace radix