Project

General

Profile

Statistics
| Branch: | Revision:

root / src / envir / args.h @ 636cbdd5

History | View | Annotate | Download (3.32 KB)

1
//==========================================================================
2
//  ARGS.H - part of
3
//                     OMNeT++/OMNEST
4
//             Discrete System Simulation in C++
5
//
6
//  Command line argument handling
7
//
8
//  Author: Andras Varga
9
//
10
//==========================================================================
11

    
12
/*--------------------------------------------------------------*
13
  Copyright (C) 1992-2008 Andras Varga
14
  Copyright (C) 2006-2008 OpenSim Ltd.
15

16
  This file is distributed WITHOUT ANY WARRANTY. See the file
17
  `license' for details on this and other legal matters.
18
*--------------------------------------------------------------*/
19

    
20
#ifndef __ARGS_H
21
#define __ARGS_H
22

    
23
#include <string>
24
#include <vector>
25
#include <map>
26
#include "envirdefs.h"
27

    
28
NAMESPACE_BEGIN
29

    
30
/**
31
 * Somewhat similar to getopt(), this class stores and parses command-line
32
 * arguments.
33
 */
34
class ENVIR_API ArgList
35
{
36
  private:
37
    int argc;
38
    char **argv;
39
    std::string spec;
40

    
41
    std::map<char,std::vector<std::string> > shortOpts;
42
    std::map<std::string,std::string> longOpts;
43
    std::vector<std::string> params;
44

    
45
  private:
46
    // looks up spec for the given option
47
    bool isValidOption(char c) const;
48

    
49
    // looks up spec for the given option, and returns true if it has an arg
50
    bool hasArg(char c) const;
51

    
52
    // looks up spec for the given option, and returns true if it has an optional arg
53
    bool hasOptionalArg(char c) const;
54

    
55
    // if char c == 0, returns kth argument not kth option
56
    bool getOpt(char c, int k, const char *&value, bool validate) const;
57

    
58
    // fills in shortOpts, longOpts and params from argc+argv
59
    void parse();
60

    
61
  public:
62
    /**
63
     * Ctor
64
     */
65
    ArgList();
66

    
67
    /**
68
     * Takes argc, argv, and a getopt()-like specification
69
     * of single-letter options. The spec argument is a string that
70
     * specifies the option characters that are valid for this program.
71
     * An option character in this string can be followed by a colon (`:')
72
     * to indicate that it takes a required argument. If the option
73
     * character is followed by a question mark ('?'), it means that
74
     * the argument is optional, that is, it should not be an error to specify
75
     * the option as the last argument (ie followed by nothing).
76
     * '?' is probably ONLY useful with -h.
77
     *
78
     * Throws an exception if there are unrecognized options
79
     */
80
    void parse(int argc, char *argv[], const char *spec);
81

    
82
    /**
83
     * Returns true if the given option is present on the command line.
84
     */
85
    bool optionGiven(char c) const;
86

    
87
    /**
88
     * Returns the value of the given option. If there are more than one
89
     * occurrences, k specifies which one to return. Returns NULL if not
90
     * found.
91
     */
92
    const char *optionValue(char c, int k=0) const;
93

    
94
    /**
95
     * Returns long options (those that begin with '--'), as key-value pairs;
96
     * keys do not contain the leading '--'.
97
     */
98
    std::map<std::string,std::string> getLongOptions() const;
99

    
100
    /**
101
     * Returns the kth non-option argument. Returns NULL if k is out of range.
102
     */
103
    const char *argument(int k) const;
104

    
105
    /**
106
     * Returns the original argc.
107
     */
108
    int getArgCount() const  {return argc;}
109

    
110
    /**
111
     * Returns the original argv. It should not be modified.
112
     */
113
    char **getArgVector() const  {return argv;}
114
};
115

    
116
NAMESPACE_END
117

    
118
#endif
119

    
120