ArgumentParser.h 3.3 KB
Newer Older
fabianw's avatar
fabianw committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 *  ArgumentParser.h
 *  Cubism
 *
 *	This argument parser assumes that all arguments are optional ie, each of the argument names is preceded by a '-'
 *		all arguments are however NOT optional to avoid a mess with default values and returned values when not found!
 *
 *	More converter could be required:
 *		add as needed
 *			TypeName as{TypeName}() in Value
 *
 *  Created by Christian Conti on 6/7/10.
 *  Copyright 2010 ETH Zurich. All rights reserved.
 *
 */

#pragma once
18
#include <iosfwd>  // Forward declaration of <iostream>
fabianw's avatar
fabianw committed
19 20 21
#include <map>
#include <string>

kicici's avatar
kicici committed
22 23 24
#include "Common.h"

CUBISM_NAMESPACE_BEGIN
fabianw's avatar
fabianw committed
25 26 27 28

class Value
{
private:
fabianw's avatar
fabianw committed
29
    std::string content;
fabianw's avatar
fabianw committed
30 31

public:
32 33 34
    Value() = default;
    Value(const std::string &content_) : content(content_) {}
    Value(const Value& c) = default;
fabianw's avatar
fabianw committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48

    Value& operator=(const Value& rhs)
    {
        if (this != &rhs)
            content = rhs.content;
        return *this;
    }
    Value& operator+=(const Value& rhs)
    {
        content += " " + rhs.content;
        return *this;
    }
    Value operator+(const Value& rhs) { return Value(content + " " + rhs.content); }

49 50 51 52 53
    double asDouble(double def = 0);
    int asInt(int def = 0);
    bool asBool(bool def = false);
    std::string asString(const std::string &def = std::string());
    friend std::ostream& operator<<(std::ostream& lhs, const Value& rhs);
fabianw's avatar
fabianw committed
54 55 56 57 58 59
};


class CommandlineParser
{
private:
fabianw's avatar
fabianw committed
60 61 62
    const int iArgC;
    char** vArgV;
    bool bStrictMode, bVerbose;
fabianw's avatar
fabianw committed
63 64

protected:
fabianw's avatar
fabianw committed
65
    std::map<std::string,Value> mapArguments;
fabianw's avatar
fabianw committed
66 67

public:
68
    CommandlineParser(int argc, char ** argv);
fabianw's avatar
fabianw committed
69

70 71
    Value& operator()(std::string key);
    bool check(std::string key) const;
fabianw's avatar
fabianw committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

    int getargc() const { return iArgC; }
    char** getargv() const { return vArgV; }

    void set_strict_mode()
    {
        bStrictMode = true;
    }

    void unset_strict_mode()
    {
        bStrictMode = false;
    }

    void mute()
    {
        bVerbose = false;
    }

    void loud()
    {
        bVerbose = true;
    }

96 97
    void save_options(const std::string &path = ".");
    void print_args();
fabianw's avatar
fabianw committed
98 99 100
};


101
class ArgumentParser : public CommandlineParser
fabianw's avatar
fabianw committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
{
    typedef std::map<std::string, Value> ArgMap;
    typedef std::map<std::string, Value*> pArgMap;
    typedef std::map<std::string, ArgMap* > FileMap;

    const char commentStart;

    // keep a reference from option origin
    ArgMap  from_commandline;
    FileMap from_files;
    pArgMap from_code;

    // for runtime interaction (we keep the original map)
    ArgMap mapRuntime;

    // helper
118 119
    void _ignoreComments(std::istream& stream, char commentChar);
    void _parseFile(std::ifstream& stream, ArgMap& container);
fabianw's avatar
fabianw committed
120 121

public:
122
    ArgumentParser(const int _argc, char ** _argv, const char cstart='#'):
fabianw's avatar
fabianw committed
123 124 125 126 127 128 129 130 131 132 133
        CommandlineParser(_argc, _argv), commentStart(cstart)
    {
        from_commandline = mapArguments;
    }

    virtual ~ArgumentParser()
    {
        for (FileMap::iterator it = from_files.begin(); it != from_files.end(); it++)
            delete it->second;
    }

134 135
    void readFile(const std::string &filepath);
    Value& operator()(std::string key);
fabianw's avatar
fabianw committed
136

137
    inline bool exist(const std::string &key) const { return check(key); }
fabianw's avatar
fabianw committed
138

139 140
    void write_runtime_environment() const;
    void read_runtime_environment();
fabianw's avatar
fabianw committed
141

142 143
    Value& parseRuntime(std::string key);
    void print_args(void);
fabianw's avatar
fabianw committed
144
};
kicici's avatar
kicici committed
145 146

CUBISM_NAMESPACE_END