1 // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
3 /******************************************************************************
5 * file: ZshCompletionOutput.h
7 * Copyright (c) 2006, Oliver Kiddle
10 * See the file COPYING in the top directory of this distribution for
13 * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
14 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19 * DEALINGS IN THE SOFTWARE.
21 *****************************************************************************/
23 #ifndef TCLAP_ZSHCOMPLETIONOUTPUT_H
24 #define TCLAP_ZSHCOMPLETIONOUTPUT_H
32 #include <tclap/CmdLineInterface.h>
33 #include <tclap/CmdLineOutput.h>
34 #include <tclap/XorHandler.h>
35 #include <tclap/Arg.h>
40 * A class that generates a Zsh completion function as output from the usage()
41 * method for the given CmdLine and its Args.
43 class ZshCompletionOutput : public CmdLineOutput
48 ZshCompletionOutput();
51 * Prints the usage to stdout. Can be overridden to
52 * produce alternative behavior.
53 * \param c - The CmdLine object the output is generated for.
55 virtual void usage(CmdLineInterface& c);
58 * Prints the version to stdout. Can be overridden
59 * to produce alternative behavior.
60 * \param c - The CmdLine object the output is generated for.
62 virtual void version(CmdLineInterface& c);
65 * Prints (to stderr) an error message, short usage
66 * Can be overridden to produce alternative behavior.
67 * \param c - The CmdLine object the output is generated for.
68 * \param e - The ArgException that caused the failure.
70 virtual void failure(CmdLineInterface& c,
75 void basename( std::string& s );
76 void quoteSpecialChars( std::string& s );
78 std::string getMutexList( CmdLineInterface& _cmd, Arg* a );
79 void printOption( Arg* it, std::string mutex );
80 void printArg( Arg* it );
82 std::map<std::string, std::string> common;
86 ZshCompletionOutput::ZshCompletionOutput()
87 : common(std::map<std::string, std::string>()),
90 common["host"] = "_hosts";
91 common["hostname"] = "_hosts";
92 common["file"] = "_files";
93 common["filename"] = "_files";
94 common["user"] = "_users";
95 common["username"] = "_users";
96 common["directory"] = "_directories";
97 common["path"] = "_directories";
98 common["url"] = "_urls";
101 inline void ZshCompletionOutput::version(CmdLineInterface& _cmd)
103 std::cout << _cmd.getVersion() << std::endl;
106 inline void ZshCompletionOutput::usage(CmdLineInterface& _cmd )
108 std::list<Arg*> argList = _cmd.getArgList();
109 std::string progName = _cmd.getProgramName();
110 std::string xversion = _cmd.getVersion();
111 theDelimiter = _cmd.getDelimiter();
114 std::cout << "#compdef " << progName << std::endl << std::endl <<
115 "# " << progName << " version " << _cmd.getVersion() << std::endl << std::endl <<
118 for (ArgListIterator it = argList.begin(); it != argList.end(); it++)
120 if ( (*it)->shortID().at(0) == '<' )
122 else if ( (*it)->getFlag() != "-" )
123 printOption((*it), getMutexList(_cmd, *it));
126 std::cout << std::endl;
129 inline void ZshCompletionOutput::failure( CmdLineInterface& _cmd,
132 static_cast<void>(_cmd); // unused
133 std::cout << e.what() << std::endl;
136 inline void ZshCompletionOutput::quoteSpecialChars( std::string& s )
138 size_t idx = s.find_last_of(':');
139 while ( idx != std::string::npos )
141 s.insert(idx, 1, '\\');
142 idx = s.find_last_of(':', idx);
144 idx = s.find_last_of('\'');
145 while ( idx != std::string::npos )
147 s.insert(idx, "'\\'");
149 idx = std::string::npos;
151 idx = s.find_last_of('\'', --idx);
155 inline void ZshCompletionOutput::basename( std::string& s )
157 size_t p = s.find_last_of('/');
158 if ( p != std::string::npos )
164 inline void ZshCompletionOutput::printArg(Arg* a)
166 static int count = 1;
168 std::cout << " \\" << std::endl << " '";
169 if ( a->acceptsMultipleValues() )
172 std::cout << count++;
174 if ( !a->isRequired() )
177 std::cout << a->getName() << ':';
178 std::map<std::string, std::string>::iterator compArg = common.find(a->getName());
179 if ( compArg != common.end() )
181 std::cout << compArg->second;
185 std::cout << "_guard \"^-*\" " << a->getName();
190 inline void ZshCompletionOutput::printOption(Arg* a, std::string mutex)
192 std::string flag = a->flagStartChar() + a->getFlag();
193 std::string name = a->nameStartString() + a->getName();
194 std::string desc = a->getDescription();
196 // remove full stop and capitalization from description as
197 // this is the convention for zsh function
198 if (!desc.compare(0, 12, "(required) "))
202 if (!desc.compare(0, 15, "(OR required) "))
206 size_t len = desc.length();
207 if (len && desc.at(--len) == '.')
213 desc.replace(0, 1, 1, tolower(desc.at(0)));
216 std::cout << " \\" << std::endl << " '" << mutex;
218 if ( a->getFlag().empty() )
224 std::cout << "'{" << flag << ',' << name << "}'";
226 if ( theDelimiter == '=' && a->isValueRequired() )
228 quoteSpecialChars(desc);
229 std::cout << '[' << desc << ']';
231 if ( a->isValueRequired() )
233 std::string arg = a->shortID();
234 // Example arg: "[-A <integer>] ... "
235 size_t pos = arg.rfind(" ... ");
236 if (pos != std::string::npos) {
240 arg.erase(0, arg.find_last_of(theDelimiter) + 1);
241 if ( arg.at(arg.length()-1) == ']' )
242 arg.erase(arg.length()-1);
243 if ( arg.at(arg.length()-1) == ']' )
245 arg.erase(arg.length()-1);
247 if ( arg.at(0) == '<' )
249 arg.erase(arg.length()-1);
252 size_t p = arg.find('|');
253 if ( p != std::string::npos )
257 arg.replace(p, 1, 1, ' ');
259 while ( (p = arg.find_first_of('|', p)) != std::string::npos );
260 quoteSpecialChars(arg);
261 std::cout << ": :(" << arg << ')';
265 std::cout << ':' << arg;
266 std::map<std::string, std::string>::iterator compArg = common.find(arg);
267 if ( compArg != common.end() )
269 std::cout << ':' << compArg->second;
277 inline std::string ZshCompletionOutput::getMutexList( CmdLineInterface& _cmd, Arg* a)
279 XorHandler xorHandler = _cmd.getXorHandler();
280 std::vector< std::vector<Arg*> > xorList = xorHandler.getXorList();
282 if (a->getName() == "help" || a->getName() == "version")
287 std::ostringstream list;
288 if ( a->acceptsMultipleValues() )
293 for ( int i = 0; static_cast<unsigned int>(i) < xorList.size(); i++ )
295 for ( ArgVectorIterator it = xorList[i].begin();
296 it != xorList[i].end();
301 for ( ArgVectorIterator iu = xorList[i].begin();
302 iu != xorList[i].end();
305 bool notCur = (*iu) != a;
306 bool hasFlag = !(*iu)->getFlag().empty();
307 if ( iu != xorList[i].begin() && (notCur || hasFlag) )
310 list << (*iu)->flagStartChar() << (*iu)->getFlag() << ' ';
311 if ( notCur || hasFlag )
312 list << (*iu)->nameStartString() << (*iu)->getName();
319 // wasn't found in xor list
320 if (!a->getFlag().empty()) {
321 list << "(" << a->flagStartChar() << a->getFlag() << ' ' <<
322 a->nameStartString() << a->getName() << ')';