Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
133 lines (110 loc) · 4.17 KB

File metadata and controls

133 lines (110 loc) · 4.17 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* \copyright Copyright 2013 Google Inc. All Rights Reserved.
* \license @{
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @}
*/
//
// Command Processor class provides a basic shell interpreter for writing apps.
// It might be useful for writing experimental code that makes API calls.
// I use it in some samples.
#ifndef GOOGLEAPIS_SAMPLES_COMMAND_PROCESSOR_H_
#define GOOGLEAPIS_SAMPLES_COMMAND_PROCESSOR_H_
#include <map>
using std::map;
#include <memory>
#include <utility>
using std::make_pair;
using std::pair;
#include <vector>
using std::vector;
#include "googleapis/base/callback.h"
#include "googleapis/base/macros.h"
#include "googleapis/client/transport/http_response.h"
#include "googleapis/strings/stringpiece.h"
namespace googleapis {
namespace sample {
using client::HttpResponse;
class CommandProcessor {
public:
CommandProcessor();
virtual ~CommandProcessor();
// Controller for the application takes input commands, runs them, and
// prints the output/errors to the console.
virtual void RunShell();
void set_prompt(const string& prompt) { prompt_ = prompt; }
const string& prompt() const { return prompt_; }
// If true then CheckAndLogResponse will also log the http_response.body()
// for messages that are http_response.ok()
void set_log_success_bodies(bool on) { log_success_bodies_ = on; }
bool log_success_bodies() const { return log_success_bodies_; }
// Split args into list separating by spaces unless they are escaped
// or within quotes.
// Returns true if args were ok. False if they ended prematurely.
// Even if false is returned, the list will contain the best interpretation
// of the args.
static bool SplitArgs(const StringPiece& phrase, vector<string>* list);
protected:
typedef Callback2<const string&, const vector<string>&> CommandRunner;
struct CommandEntry {
CommandEntry(
const StringPiece& usage_args,
const StringPiece& description,
CommandRunner* callback)
: args(usage_args), help(description), runner(callback) {
callback->CheckIsRepeatable();
}
static bool CompareEntry(
const pair<const StringPiece, const CommandEntry*>& a,
const pair<const StringPiece, const CommandEntry*>& b) {
return a.first < b.first;
}
const StringPiece args;
const StringPiece help;
std::unique_ptr<CommandRunner> runner;
private:
DISALLOW_COPY_AND_ASSIGN(CommandEntry);
};
// Sets up command processor with available commands.
// Derived classes should override this and call AddCommand with their
// various commands.
//
// The base method adds the builtin commands (calls AddBulitinCommands)
virtual void InitCommands();
void AddCommand(StringPiece name, CommandEntry* details);
// For displaying errors, etc.
bool CheckAndLogResponse(HttpResponse* response);
// Handler for "quit" command.
virtual void QuitHandler(const string&, const vector<string>&);
// Handler for "help" command.
virtual void HelpHandler(const string&, const vector<string>&);
// Handler for "quiet" and "verbose" commands.
virtual void VerboseHandler(int level, const string&, const vector<string>&);
// Adds builtin 'help' and 'quit' commands.
// Called by InitCommands but offered separately so you dont need to
// propagate InitCommands when overriding it.
void AddBuiltinCommands();
private:
// maps command name to entry map for exeuting it.
typedef map<StringPiece, CommandEntry*> CommandEntryMap;
CommandEntryMap commands_;
string prompt_;
bool log_success_bodies_;
bool done_;
DISALLOW_COPY_AND_ASSIGN(CommandProcessor);
};
} // namespace sample
} // namespace googleapis
#endif // GOOGLEAPIS_SAMPLES_COMMAND_PROCESSOR_H_
Morty Proxy This is a proxified and sanitized view of the page, visit original site.