aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/getopt/small/last_getopt_easy_setup.h
blob: f5fabad2278ed8a1b7392fe359be4db8dc4d4ec0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#pragma once

#include "last_getopt_opts.h"

namespace NLastGetopt {
    /** 
     * Wrapper for TOpts class to make the life a bit easier.
     * Usual usage:
     *   TEasySetup opts;
     *   opts('s', "server",  "MR_SERVER", "MapReduce server name in format server:port", true)
     *       ('u', "user",    "MR_USER",   "MapReduce user name", true)
     *       ('o', "output",  "MR_TABLE",  "Name of MR table which will contain results", true)
     *       ('r', "rules",   "FILE",      "Filename for .rules output file")          //!< This parameter is optional and has a required argument
     *       ('v', "version", &PrintSvnVersionAndExit0, "Print version information")   //!< Parameter with handler can't get any argument
     *       ("verbose", "Be verbose")                                                 //!< You may not specify short param name
     *
     *       NLastGetopt::TOptsParseResult r(&opts, argc, argv);
     */
    class TEasySetup: public TOpts { 
    public: 
        TEasySetup(const TStringBuf& optstring = TStringBuf()); 
        TEasySetup& operator()(char shortName, const char* longName, const char* help, bool required = false); 
        TEasySetup& operator()(char shortName, const char* longName, const char* argName, const char* help, bool required = false); 

        template <class TpFunc> 
        TEasySetup& operator()(char shortName, const char* longName, TpFunc handler, const char* help, bool required = false) { 
            AdjustParam(longName, help, nullptr, handler, required).AddShortName(shortName); 
            return *this; 
        } 

        TEasySetup& operator()(const char* longName, const char* help, bool required = false); 
        TEasySetup& operator()(const char* longName, const char* argName, const char* help, bool required = false); 

        template <class TpFunc> 
        TEasySetup& operator()(const char* longName, TpFunc handler, const char* help, bool required = false) { 
            AdjustParam(longName, help, nullptr, handler, required); 
            return *this; 
        } 

    private: 
        TOpt& AdjustParam(const char* longName, const char* help, const char* argName, bool required); 

        template <class TpFunc> 
        TOpt& AdjustParam(const char* longName, const char* help, const char* argName, TpFunc handler, bool required) { 
            TOpt& o = AdjustParam(longName, help, argName, required); 
            o.Handler0(handler); 
            return o; 
        } 
    }; 

}