-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserCommand.h
175 lines (159 loc) · 6.33 KB
/
UserCommand.h
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* UserCommand.h
*
* Created on: Oct 7, 2016
* Author: paul
*/
#ifndef SRC_USERCOMMAND_H_
#define SRC_USERCOMMAND_H_
#include <iostream>
#include <string>
#include <vector>
#include "tools/Token.h"
/**
* Used to retain command arguments and the proper input and output streams for
* a command call.
* <p>
* Note that, with the exception of @link UserCommand::getWholeCommand @endlink,
* all of the functions that retrieve options or arguments from the user's
* command input iterate over the internal vector used to store it, and thus
* may slow down your program if they are called too often.
*
* @since 0.5
*/
class UserCommand {
public:
/**
* Constructs a new UserCommand object with the given command, input, and
* output. This also invokes the command parser, parsing the user's command
* into its components. (This does not parse out the command's options).
*
* @param inputCommand the command the user input
* @param in the stream to use as stdin for the command
* @param out the stream to use as out for the command
*/
UserCommand (const std::string &inputCommand, std::istream &in,
std::ostream &out);
/**
* Constructs a new UserCommand object with the given command, input, and
* output. This also invokes the command parser, parsing the user's command
* into its components. (This does not parse out the command's options).
*
* @param inputCommand the command the user input as a Token
* @param in the stream to use as stdin for the command
* @param out the stream to use as stdout for the command
*/
UserCommand (const Token &inputCommand, std::istream &in, std::ostream &out);
virtual ~UserCommand();
//COMMAND INFORMATION RETRIEVAL ********************************************
/**
* Returns the entire command (including the command name), parsed into
* arguments as a vector.
*
* @return the entire command, including the command name, as a vector
*/
const std::vector<std::string>& getWholeCommand () const;
/**
* Returns all arguments passed by the user that are not options/switches
* (arguments identified with a `-` or `--` at the beginning).
*
* @return all non-option arguments
*/
std::vector<std::string> getArguments () const;
/**
* Returns all of the options/flags that the user specified (arguments with
* a leading `-` or `--`).
* <p>
* Note that iash option syntax allows users to chain single-letter options
* together (like `-abcde`), and will report all of the characters present
* after a single minus as individual options (so, in this example, `a`,
* `b`, `c`, `d`, `e` will all be reported as options). If you want iash to
* report whole words as options, have users enter them with a double-minus
* prefix (so `--abcde` will be reported as `abcde`).
*
* @return all options/flags
*/
std::vector<std::string> getOptions () const;
/**
* Returns whether the given single-letter option was specified by the
* user.
* <p>
* Note that iash option syntax allows users to chain single-letter options
* together (like `-abcde`), and will report all of the characters present
* after a single minus as individual options (so, in this example, `a`,
* `b`, `c`, `d`, `e` will all be reported as options). If you want iash to
* report whole words as options, have users enter them with a double-minus
* prefix (so `--abcde` will be reported as `abcde`).
*
* @param opt the single-letter option to check for
* @return whether the option was passed by the user
*/
bool hasOption (char opt) const;
/**
* Returns whether the given whole-word option was specified by the
* user.
* <p>
* Note that iash option syntax allows users to chain single-letter options
* together (like `-abcde`), and will report all of the characters present
* after a single minus as individual options (so, in this example, `a`,
* `b`, `c`, `d`, `e` will all be reported as options). If you want iash to
* report whole words as options, have users enter them with a double-minus
* prefix (so `--abcde` will be reported as `abcde`).
*
* @param opt the whole-word option to check for
* @return whether the option was passed by the user
*/
bool hasOption (const std::string &opt) const;
/**
* Finds the argument following the specified single-letter option or flag.
* If the specified option does not have an argument that follows it, or the
* option is embedded in a chained single-letter argument and is not at the
* end (for example, `c` in `-abcde`), then this will return an empty
* string.
* <p>
* Note that this does not guarantee that the returned argument is actually
* specifying a parameter for the given option. In order to insure this,
* Command::validate should be overridden to check that the command
* structure is valid.
*
* @param opt the single-letter option to find a contextual argument for
* @return the argument following the option, or a blank string if one
* could not be found
*/
std::string getContextualArgument (char opt) const;
/**
* Finds the argument following the specified multi-character option or
* flag. If the specified option dooes not have an argument that follows it,
* then this will return an empty string.
* <p>
* Note that this does not guarantee that the returned argument is actually
* specifying a parameter for the given option. In order to insure this,
* Command::validate should be overridden to check that the command
* structure is valid.
*
* @param opt a multi-character option to find a contextual argument for
* @return the argument following the option, or a blank string if one
* could not be found
*/
std::string getContextualArgument (std::string opt) const;
/**
* Gets a reference to the input stream to be used as stdin for this command
* invocation.
*
* @return a reference to the input stream for this command
*/
std::istream& getStdin () const;
/**
* Gets a reference to the output stream to be used as stdout for this
* command invocation.
*
* @return a reference to the output stream for this command
*/
std::ostream& getStdout () const;
private:
std::istream &m_stdin;
std::ostream &m_stdout;
std::vector<std::string> m_commandParts;
std::string m_raw;
};
#endif /* SRC_USERCOMMAND_H_ */