OpenASIP 2.2
Loading...
Searching...
No Matches
CmdLineOptionParser.cc
Go to the documentation of this file.
1/*
2 Copyright (c) 2002-2009 Tampere University.
3
4 This file is part of TTA-Based Codesign Environment (TCE).
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23 */
24/**
25 * @file CmdLineOptionParser.cc
26 *
27 * Definitions of CmdLineOptionParser classes.
28 *
29 * @author Jussi Nyk�nen 2003 (nykanen-no.spam-cs.tut.fi)
30 * @author Pekka Jääskeläinen 2005 (pjaaskel-no.spam-cs.tut.fi)
31 * @author Henry Linjamäki 2017 (henry.linjamaki-no.spam-tut.fi)
32 * @note reviewed 3 December 2003 by jn, kl, ao
33 * @note rating: red
34 */
35
36#include <cstdio>
37#include <iostream>
38#include <string>
39#include <sstream>
40
41#include "Application.hh"
43#include "Exception.hh"
44#include "OptionValue.hh"
45
46using std::cerr;
47using std::endl;
48using std::string;
49using std::istringstream;
50
51//////////////////////////////////////////////////////////////////////////////
52// CmdLineOptionParser
53//////////////////////////////////////////////////////////////////////////////
54
55/**
56 * Constructor.
57 *
58 * @param name The name of the option.
59 * @param desc The description of the option.
60 * @param alias The short name of the option.
61 */
63 std::string name,
64 std::string desc,
65 std::string alias,
66 bool hidden) :
67 longName_(name), shortName_(alias), desc_(desc), hidden_(hidden),
68 defined_(false) {
69}
70
71/**
72 * Destructor.
73 */
76
77/**
78 * This implementation should never be called.
79 *
80 * @return Nothing.
81 * @exception WrongSubclass Called for a wrong subclass.
82 */
83int
85 throw WrongSubclass(__FILE__, __LINE__, __func__);
86 return 0;
87}
88
89unsigned
91 throw WrongSubclass(__FILE__, __LINE__, __func__);
92 return 0;
93}
94
95/**
96 * This implementation should be never called.
97 *
98 * @return Nothing.
99 * @exception WrongSubclass Called for a wrong subclass.
100 */
101std::string
103 throw WrongSubclass(__FILE__, __LINE__, __func__);
104 return "";
105}
106
107/**
108 * This implementation should never be called.
109 *
110 * @return Nothing.
111 * @exception WrongSubclass Called for a wrong subclass.
112 */
113double
115 throw WrongSubclass(__FILE__, __LINE__, __func__);
116 return 0;
117}
118
119/**
120 * This implementation should never be called.
121 *
122 * @return Nothing.
123 * @exception WrongSubclass Called for a wrong subclass.
124 */
125bool
127 throw WrongSubclass(__FILE__, __LINE__, __func__);
128 return false;
129}
130
131/**
132 * This implementation should never be called.
133 *
134 * @return Nothing.
135 * @exception WrongSubclass Called for a wrong subclass.
136 */
137bool
139 throw WrongSubclass(__FILE__, __LINE__, __func__);
140 return false;
141}
142
143/**
144 * This implementation should never be called.
145 *
146 * @return Nothing.
147 * @exception WrongSubclass Called for a wrong subclass.
148 */
149int
151 throw WrongSubclass(__FILE__, __LINE__, __func__);
152 return 0;
153}
154
155/**
156 * Sets the CmdLineOptionParser to be defined in the parsed command line.
157 */
158void
162
163/**
164 * Returns true if the option was defined in the command line.
165 *
166 * @return True if the option was defined in the command line.
167 */
168bool
172
173//////////////////////////////////////////////////////////////////////////////
174// IntegerCmdLineOptionParser
175//////////////////////////////////////////////////////////////////////////////
176
177/**
178 * Constructor.
179 *
180 * @param name The name of the option.
181 * @param desc The description of the option.
182 * @param alias The short name of the option.
183 */
185 std::string name,
186 std::string desc,
187 std::string alias) : CmdLineOptionParser(name, desc, alias), value_(0) {
188}
189
190/**
191 * Destructor.
192 */
195
196/**
197 * Copies the option value into a OptionValue object.
198 *
199 * Client is responsible of deallocating the memeory reserved for the
200 * returned object.
201 *
202 * @return A copy of the option value.
203 */
209
210/**
211 * Parses the integer option value.
212 *
213 * @param arguments The arguments of option.
214 * @param prefix The prefix of option.
215 * @return True when parsing is ready.
216 * @exception IllegalCommandLine If the arguments of option are erronous.
217 */
218bool
220 std::string arguments, std::string prefix) {
221 if (prefix != "") {
222 string msg = "Illegal prefix for integer option";
223 string method = "IntegerCmdLineOptionParser::parseValue()";
224 throw IllegalCommandLine(__FILE__, __LINE__, method, msg);
225 }
226
227 if (arguments == "") {
228 string msg = "Missing value for integer option.";
229 string method = "IntegerCmdLineOptionParser::parseValue()";
230 throw IllegalCommandLine(__FILE__, __LINE__, method, msg);
231 }
232
233 istringstream istream(arguments);
234 if((!(istream >> value_)) || istream.peek() != EOF) {
235 string msg = "Format of " + longName() + " option value was wrong: "
236 + arguments;
237 string method = "IntegerCmdLineOptionParser::parseValue()";
238 throw IllegalCommandLine(__FILE__, __LINE__, method, msg);
239 }
240
241 setDefined();
242 return true;
243}
244
245/**
246 * Returns the value of integer option.
247 *
248 * @param index This must be zero for integer option.
249 * @return The value of integer option.
250 * @exception WrongSubclass Is never thrown by this function.
251 */
252int
254 assert(index == 0);
255 return value_;
256}
257
258//////////////////////////////////////////////////////////////////////////////
259// IntegerCmdLineOptionParser
260//////////////////////////////////////////////////////////////////////////////
261
262/**
263 * Constructor.
264 *
265 * @param name The name of the option.
266 * @param desc The description of the option.
267 * @param alias The short name of the option.
268 */
270 std::string name,
271 std::string desc,
272 std::string alias) :
273 CmdLineOptionParser(name, desc, alias), value_(0) {
274}
275
276/**
277 * Destructor.
278 */
281
287
288/**
289 * Parses the unsigned integer option value.
290 *
291 * @param arguments The arguments of option.
292 * @param prefix The prefix of option.
293 * @return True when parsing is ready.
294 * @exception IllegalCommandLine If the arguments of option are erronous.
295 */
296bool
298 std::string arguments, std::string prefix) {
299 if (prefix != "") {
300 string msg = "Illegal prefix for unsigned integer option";
301 string method = "UnsignedIntegerCmdLineOptionParser::parseValue()";
302 throw IllegalCommandLine(__FILE__, __LINE__, method, msg);
303 }
304
305 if (arguments == "") {
306 string msg = "Missing value for unsigned integer option.";
307 string method = "UnsignedIntegerCmdLineOptionParser::parseValue()";
308 throw IllegalCommandLine(__FILE__, __LINE__, method, msg);
309 }
310
311 istringstream istream(arguments);
312 if((!(istream >> value_)) || istream.peek() != EOF) {
313 string msg = "Format of " + longName() + " option value was wrong: "
314 + arguments;
315 string method = "unsignedIntegerCmdLineOptionParser::parseValue()";
316 throw IllegalCommandLine(__FILE__, __LINE__, method, msg);
317 }
318
319 setDefined();
320 return true;
321}
322
323/**
324 * Returns the value of the unsigned integer option.
325 *
326 * @param index This must be zero for usigned integer option.
327 * @return The value of usigned integer option.
328 * @exception WrongSubclass Is never thrown by this function.
329 */
330unsigned
332 assert(index == 0);
333 return value_;
334}
335
336//////////////////////////////////////////////////////////////////////////////
337// StringCmdLineOptionParser
338//////////////////////////////////////////////////////////////////////////////
339
340/**
341 * Constructor.
342 *
343 * @param name The name of the option.
344 * @param desc The description of the option.
345 * @param alias The short name of the option.
346 */
348 std::string name,
349 std::string desc,
350 std::string alias) : CmdLineOptionParser(name, desc, alias), value_("") {
351}
352
353/**
354 * Destructor.
355 */
358
359/**
360 * Copies the option value into a OptionValue object.
361 *
362 * Client is responsible of deallocating the memery reserved for the
363 * returned object.
364 *
365 * @return A copy of the option value.
366 */
372
373/**
374 * Parses the value of option.
375 *
376 * @param arguments The arguments of option.
377 * @param prefix The prefix of option.
378 * @return True when parsing is ready.
379 * @exception IllegalCommandLine Not thrown.
380 */
381bool
383 std::string arguments, std::string prefix) {
384 if (prefix != "") {
385 string msg = "Illegal prefix for string option";
386 string method = "StringCmdLineOptionParser::parseValue()";
387 throw IllegalCommandLine(__FILE__, __LINE__, method, msg);
388 }
389
390 if (arguments == "") {
391 string msg = "Missing value for string option.";
392 string method = "StringCmdLineOptionParser::parseValue()";
393 throw IllegalCommandLine(__FILE__, __LINE__, method, msg);
394 }
395
396 value_ = arguments;
397
398 setDefined();
399 return true;
400}
401
402string
404 assert(index == 0);
405 return value_;
406}
407
408//////////////////////////////////////////////////////////////////////////////
409// OptionalStringCmdLineOptionParser
410//////////////////////////////////////////////////////////////////////////////
411
412/**
413 * Constructor.
414 *
415 * @param name The name of the option.
416 * @param desc The description of the option.
417 * @param alias The short name of the option.
418 */
420 std::string name, std::string desc, std::string alias)
421 : CmdLineOptionParser(name, desc, alias), value_(""), flag_(false) {
422}
423
424/**
425 * Destructor.
426 */
429
430/**
431 * Copies the option value into a OptionValue object.
432 *
433 * Client is responsible of deallocating the memeory reserved for the
434 * returned object.
435 *
436 * @return A copy of the option value.
437 */
443
444/**
445 * Parses the value of option.
446 *
447 * @param arguments The arguments of option.
448 * @param prefix The prefix of option.
449 * @return True when parsing is ready.
450 * @exception IllegalCommandLine Not thrown.
451 */
452bool
454 std::string arguments, std::string prefix) {
455 if (prefix != "") {
456 string msg = "Illegal prefix for string option";
457 string method = "StringCmdLineOptionParser::parseValue()";
458 throw IllegalCommandLine(__FILE__, __LINE__, method, msg);
459 }
460
461 value_ = arguments;
462 flag_ = true;
463
464 setDefined();
465 return true;
466}
467
468string
470 assert(index == 0);
471 return value_;
472}
473
474/**
475 * Returns true if the flag is set.
476 *
477 * @return The boolean flag value.
478 * @exception WrongSubclass Is never thrown by this function.
479 */
480bool
484
485/**
486 * Returns true if the flag is not set.
487 *
488 * @return Inverse of the boolean flag.
489 * @exception WrongSubclass Is never thrown by this function.
490 */
491bool
495
496//////////////////////////////////////////////////////////////////////////////
497// RealCmdLineOptionParser
498//////////////////////////////////////////////////////////////////////////////
499
500/**
501 * Constructor.
502 *
503 * @param name The name of the option.
504 * @param desc The description of the option.
505 * @param alias The short name of the option.
506 */
508 std::string name,
509 std::string desc,
510 std::string alias) :
511 CmdLineOptionParser(name, desc, alias), value_(0) {
512}
513
514/**
515 * Destructor.
516 */
519
520/**
521 * Copies the option value into a OptionValue object.
522 *
523 * Client is responsible of deallocating the memeory reserved for the
524 * returned object.
525 *
526 * @return A copy of the option value.
527 */
533
534/**
535 * Parses the value of option.
536 *
537 * @param arguments The arguments of option.
538 * @param prefix The prefix of option.
539 * @return True when parsing is ready.
540 * @exception IllegalCommandLine If the argument is erronous.
541 */
542bool
543RealCmdLineOptionParser::parseValue(std::string arguments, std::string prefix) {
544 if (prefix != "") {
545 string msg = "Illegal prefix for real option";
546 string method = "RealCmdLineOptionParser::parseValue()";
547 throw IllegalCommandLine(__FILE__, __LINE__, method, msg);
548 }
549
550 // string must not contain anything else than numbers and a dot.
551 if (arguments == "") {
552 string msg = "Missing value for real option.";
553 string method = "RealCmdLineOptionParser::parseValue()";
554 throw IllegalCommandLine(__FILE__, __LINE__, method, msg);
555 }
556
557 istringstream istream(arguments);
558 if((!(istream >> value_)) || istream.peek() != EOF) {
559 string msg = "Format of " + longName() + " option value was wrong: "
560 + arguments;
561 string method = "RealCmdLineOptionParser::parseValue()";
562 throw IllegalCommandLine(__FILE__, __LINE__, method, msg);
563 }
564
565 setDefined();
566 return true;
567}
568
569/**
570 * Returns the real value.
571 *
572 * @return The real value.
573 * @exception WrongSubclass Is never thrown by this function.
574 */
575double
577 return value_;
578}
579
580//////////////////////////////////////////////////////////////////////////////
581// BoolCmdLineOptionParser
582//////////////////////////////////////////////////////////////////////////////
583
584/**
585 * Constructor.
586 *
587 * @param name The name of the option.
588 * @param desc The description of the option.
589 * @param alias The short name of the option.
590 */
592 std::string name,
593 std::string desc,
594 std::string alias,
595 bool hidden) :
596 CmdLineOptionParser(name, desc, alias, hidden), value_(false) {
597}
598
599/**
600 * Destructor.
601 */
604
605/**
606 * Copies the option value into a OptionValue object.
607 *
608 * Client is responsible of deallocating the memeory reserved for the
609 * returned object.
610 *
611 * @return A copy of the option value.
612 */
618
619/**
620 * Parses the value of option.
621 *
622 * @param arguments The arguments for option.
623 * @param prefix The prefix of option.
624 * @return True if parsing is ready, otherwise false.
625 * @exception IllegalCommandLine If the prefix or arguments are invalid.
626 */
627bool
628BoolCmdLineOptionParser::parseValue(std::string arguments, std::string prefix) {
629 if (prefix == "no-") {
630 value_ = false;
631 } else if (prefix == "") {
632 value_ = true;
633 } else {
634 string msg = "Illegal prefix for boolean option " + longName() +
635 ": " + prefix;
636 string method = "BoolCmdLineOptionParser::parseValue()";
637 throw IllegalCommandLine(__FILE__, __LINE__, method, msg);
638 }
639
640 if (arguments != "") {
641 return false;
642 } else {
643 setDefined();
644 return true;
645 }
646}
647
648/**
649 * Returns true if the flag is set.
650 *
651 * @return The boolean flag value.
652 * @exception WrongSubclass Is never thrown by this function.
653 */
654bool
656 return value_;
657}
658
659/**
660 * Returns true if the flag is not set.
661 *
662 * @return Inverse of the boolean flag.
663 * @exception WrongSubclass Is never thrown by this function.
664 */
665bool
667 return !value_;
668}
669
670//////////////////////////////////////////////////////////////////////////////
671// IntegerListCmdLineOptionParser
672//////////////////////////////////////////////////////////////////////////////
673
674/**
675 * Constructor.
676 *
677 * @param name The name of the option.
678 * @param desc The description of the option.
679 * @param alias The short name of the option.
680 */
682 std::string name,
683 std::string desc,
684 std::string alias) : CmdLineOptionParser(name, desc, alias) {
685}
686
687/**
688 * Destructor.
689 */
693
694/**
695 * Copies the option value into a OptionValue object.
696 *
697 * Client is responsible of deallocating the memeory reserved for the
698 * returned object.
699 *
700 * @return A copy of the option value.
701 */
707
708/**
709 * Parses the value of integer set.
710 *
711 * Value should be integers separated by commas.
712 *
713 * @param arguments The arguments of option.
714 * @param prefix The prefix of option.
715 * @return True when parsing is ready.
716 * @exception IllegalCommandLine If argument is invalid.
717 */
718bool
720 std::string arguments, std::string prefix) {
721 if (prefix != "") {
722 string msg = "Illegal prefix for integer list option";
723 string method = "IntegerListCmdLineOptionParser::parseValue()";
724 throw IllegalCommandLine(__FILE__, __LINE__, method, msg);
725 }
726
727 if (arguments == "") {
728 string msg = "Missing value for option " + longName();
729 string method = "IntegerSetCmdLineOptionParser::parseValue()";
730 throw IllegalCommandLine(__FILE__, __LINE__, method, msg);
731 }
732
733 int number;
734 char comma;
735 istringstream istream(arguments);
736 // there must be at least one number
737 if (!(istream >> number)) {
738 string msg = "Format of " + longName() + "option value was wrong";
739 string method = "IntegerSetCmdLineOptionParser::parseValue()";
740 throw IllegalCommandLine(__FILE__, __LINE__, method, msg);
741 }
742 values_.push_back(number);
743
744 while (istream.peek() != EOF) {
745
746 // now there must be comma
747 if (!(istream >> comma) || comma != ',') {
748 string msg = "Format of " + longName() + "option value was wrong";
749 string method = "IntegerSetCmdLineOptionParser::parseValue()";
750 throw IllegalCommandLine(__FILE__, __LINE__, method, msg);
751 }
752
753 if (!(istream >> number)) {
754 string msg = "Format of " + longName() + "option value was wrong";
755 string method = "IntegerSetCmdLineOptionParser::parseValue()";
756 throw IllegalCommandLine(__FILE__, __LINE__, method, msg);
757 }
758 values_.push_back(number);
759 }
760 setDefined();
761 return true;
762}
763
764/**
765 * Returns a particular value of integer list option.
766 *
767 * @param index The index of list element that is wanted.
768 * @return The value of a particular element in list.
769 * @exception WrongSubclass Is never thrown by this function.
770 */
771int
773 assert(index > 0 && static_cast<unsigned>(index) <= values_.size());
774 return values_[index - 1];
775}
776
777/**
778 * Returns the number of list elements.
779 *
780 * @return The number of list elements.
781 * @exception WrongSubclass Is never thrown by this function.
782 */
783int
785 return values_.size();
786}
787
788//////////////////////////////////////////////////////////////////////////////
789// StringListCmdLineOptionParser
790//////////////////////////////////////////////////////////////////////////////
791
792/**
793 * Constructor.
794 *
795 * @param name The name of the option.
796 * @param desc The description of the option.
797 * @param alias The short name of the option.
798 */
800 std::string name,
801 std::string desc,
802 std::string alias) : CmdLineOptionParser(name, desc, alias) {
803}
804
805/**
806 * Destructor.
807 */
811
812/**
813 * Copies the option value into a OptionValue object.
814 *
815 * Client is responsible of deallocating the memeory reserved for the
816 * returned object.
817 *
818 * @return A copy of the option value.
819 */
825
826/**
827 * Parses the value of string set.
828 *
829 * Value should be strings separated by commas.
830 *
831 * @param arguments The arguments of option.
832 * @param prefix The prefix of option.
833 * @return True when parsing is ready.
834 * @exception IllegalCommandLine If argument is invalid.
835 */
836bool
838 std::string arguments, std::string prefix) {
839 if (prefix != "") {
840 string msg = "Illegal prefix for string list option";
841 throw IllegalCommandLine(__FILE__, __LINE__, __func__, msg);
842 }
843
844 if (arguments == "") {
845 string msg = "Missing value for option " + longName();
846 throw IllegalCommandLine(__FILE__, __LINE__, __func__, msg);
847 }
848
849 string comma = ",";
850 string::size_type commaPosition = 0;
851 string::size_type lastPosition = 0;
852 while (commaPosition < arguments.size()) {
853 commaPosition = arguments.find(comma, lastPosition);
854 if (commaPosition == string::npos) {
855 values_.push_back(
856 arguments.substr(lastPosition, arguments.size()));
857 break;
858 } else {
859 values_.push_back(
860 arguments.substr(lastPosition, commaPosition-lastPosition));
861 lastPosition = commaPosition + 1;
862 }
863 }
864
865 setDefined();
866 return true;
867}
868
869/**
870 * Returns a particular value of string list option.
871 *
872 * @param index The index of list element that is wanted.
873 * @return The value of a particular element in list.
874 * @exception WrongSubclass Is never thrown by this function.
875 */
876std::string
878 assert(index > 0 && static_cast<unsigned>(index) <= values_.size());
879 return values_[index - 1];
880}
881
882/**
883 * Returns the number of list elements.
884 *
885 * @return The number of list elements.
886 * @exception WrongSubclass Is never thrown by this function.
887 */
888int
890 return values_.size();
891}
#define __func__
#define assert(condition)
find Finds info of the inner loops in the false
virtual bool isFlagOn() const
bool value_
The value of option.
virtual OptionValue * copy() const
virtual bool parseValue(std::string arguments, std::string prefix)
virtual bool isFlagOff() const
BoolCmdLineOptionParser(std::string name, std::string desc, std::string alias="", bool hidden=false)
CmdLineOptionParser(std::string name, std::string desc, std::string alias, bool hidden=false)
bool defined_
Is the value of this option set in the parsed command line?
virtual int integer(int index=0) const
virtual int listSize() const
virtual bool isFlagOff() const
virtual bool isFlagOn() const
std::string longName() const
virtual double real() const
virtual std::string String(int index=0) const
virtual unsigned unsignedInteger(int index=0) const
IntegerCmdLineOptionParser(std::string name, std::string desc, std::string alias="")
virtual bool parseValue(std::string arguments, std::string prefix)
virtual int integer(int index=0) const
virtual OptionValue * copy() const
int value_
The value of option.
IntegerListCmdLineOptionParser(std::string name, std::string desc, std::string alias="")
std::vector< int > values_
The values in integer list.
virtual OptionValue * copy() const
virtual int integer(int index=0) const
virtual bool parseValue(std::string arguments, std::string prefix)
std::string value_
The value of the option.
virtual bool parseValue(std::string arguments, std::string prefix)
OptionalStringCmdLineOptionParser(std::string name, std::string desc, std::string alias="")
virtual OptionValue * copy() const
virtual std::string String(int index=0) const
virtual OptionValue * copy() const
virtual double real() const
double value_
The value of the option.
RealCmdLineOptionParser(std::string name, std::string desc, std::string alias="")
virtual bool parseValue(std::string arguments, std::string prefix)
virtual std::string String(int index=0) const
virtual OptionValue * copy() const
std::string value_
The value of the option.
virtual bool parseValue(std::string arguments, std::string prefix)
StringCmdLineOptionParser(std::string name, std::string desc, std::string alias="")
virtual OptionValue * copy() const
StringListCmdLineOptionParser(std::string name, std::string desc, std::string alias="")
std::vector< std::string > values_
The values in string list.
virtual bool parseValue(std::string arguments, std::string prefix)
virtual std::string String(int index=0) const
UnsignedIntegerCmdLineOptionParser(std::string name, std::string desc, std::string alias="")
unsigned value_
The value of option.
virtual OptionValue * copy() const
virtual bool parseValue(std::string arguments, std::string prefix)
virtual unsigned unsignedInteger(int index=0) const