File: commandlineparser.cpp

package info (click to toggle)
codelite 6.1.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 48,992 kB
  • ctags: 43,502
  • sloc: cpp: 334,263; ansic: 18,441; xml: 4,713; yacc: 2,653; lex: 2,449; python: 1,188; sh: 385; makefile: 40
file content (111 lines) | stat: -rw-r--r-- 3,239 bytes parent folder | download | duplicates (3)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "commandlineparser.h"
#include <wx/debug.h>
#include <algorithm>

CommandLineParser::CommandLineParser(int argc, char **argv)
    : m_argc(argc)
    , m_argv(argv)
    , m_curIndex(1)
    , m_used(false)
{
}

CommandLineParser::~CommandLineParser()
{
}

wxString CommandLineParser::GetArg(const wxString& shortOpt, const wxString& longOption) const
{
    if ( m_argsMap.count(shortOpt) ) {
        return m_argsMap.find(shortOpt)->second;;
    }
    if ( m_argsMap.count(longOption) ) {
        return m_argsMap.find(longOption)->second;;
    }
    return "";
}

void CommandLineParser::DoParse()
{
    wxASSERT_MSG(!m_used, "CommandLineParser can be used only once. Please constuct another instance and use it instead");

    if ( m_argc <= 1 )
        return;

    // when we see the "--" we consider the rest as the command
    while ( HasMore() ) {
        wxString optionName = NextToken();
        if ( optionName == "--cmd" ) {
            // Stop parsing, from this point on, everything is the command to execute
            while ( HasMore() ) {
                wxString cmdToken = NextToken();
                if ( cmdToken.Contains(" ") ) {
                    cmdToken.Prepend("\"").Append("\"");
                }
                m_command << cmdToken << " ";
            }
            break;

        } else {
            CommandLineParser::OptionInfo oi = GetOptionInfo(optionName);
            if ( oi.IsOk() ) {
                if ( oi.GetFlags() & CommandLineParser::kOptionWithValue ) {
                    wxString value = NextToken();
                    m_argsMap.insert( std::make_pair(StripDash(optionName), value) );

                } else {
                    m_argsMap.insert( std::make_pair(StripDash(optionName), "") );
                }
            }
        }
    }
}

bool CommandLineParser::HasMore()
{
    return ( m_curIndex < m_argc );
}

wxString CommandLineParser::NextToken()
{
    wxASSERT( HasMore() );
    return m_argv[m_curIndex++];
}

wxString CommandLineParser::PeekNextToken()
{
    wxASSERT( HasMore() );
    return m_argv[m_curIndex];
}

bool CommandLineParser::HasOption(const wxString& shortOpt, const wxString& longOption) const
{
    return m_argsMap.count(shortOpt) || m_argsMap.count(longOption);
}

void CommandLineParser::AddOption(const wxString& shortOpt, const wxString& longOpt, size_t flags)
{
    OptionInfo oi(longOpt, shortOpt);
    oi.SetFlags( flags );
    m_optionsInfo.push_back( oi );
}

CommandLineParser::OptionInfo CommandLineParser::GetOptionInfo(const wxString& name) const
{
    CommandLineParser::OptionInfo::List_t::const_iterator iter = std::find_if(m_optionsInfo.begin(),
                                                                              m_optionsInfo.end(),
                                                                              CommandLineParser::OptionInfo(name));
    if ( iter == m_optionsInfo.end() ) {
        return CommandLineParser::OptionInfo();
    }
    return *iter;
}

wxString CommandLineParser::StripDash(const wxString& opt) const
{
    wxString noPrefix (opt);
    noPrefix.StartsWith("--", &noPrefix);
    noPrefix.StartsWith("-",  &noPrefix);
    noPrefix.StartsWith("/",  &noPrefix);
    return noPrefix;
}