A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
command-line.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 INRIA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19#ifndef COMMAND_LINE_H
20#define COMMAND_LINE_H
21
22#include "callback.h"
23#include "nstime.h"
24#include "type-id.h"
25
26#include <memory> // shared_ptr
27#include <sstream>
28#include <string>
29#include <tuple>
30#include <vector>
31
38namespace ns3
39{
40
232{
233 public:
235 CommandLine();
246 CommandLine(const std::string& filename);
252 CommandLine(const CommandLine& cmd);
259 CommandLine& operator=(const CommandLine& cmd);
261 ~CommandLine();
262
268 void Usage(const std::string& usage);
269
279 template <typename T>
280 void AddValue(const std::string& name, const std::string& help, T& value);
281
302 void AddValue(const std::string& name, const std::string& help, char* value, std::size_t num);
309 typedef bool (*Callback)(const std::string& value);
310
323 void AddValue(const std::string& name,
324 const std::string& help,
326 const std::string& defaultValue = "");
327
334 void AddValue(const std::string& name, const std::string& attributePath);
335
345 template <typename T>
346 void AddNonOption(const std::string& name, const std::string& help, T& value);
347
358 std::string GetExtraNonOption(std::size_t i) const;
359
369 std::size_t GetNExtraNonOptions() const;
370
385 void Parse(int argc, char* argv[]);
386
396 void Parse(std::vector<std::string> args);
397
403 std::string GetName() const;
404
421 void PrintHelp(std::ostream& os) const;
422
428 std::string GetVersion() const;
429
437 void PrintVersion(std::ostream& os) const;
438
439 private:
444 class Item
445 {
446 public:
447 std::string m_name;
448 std::string m_help;
449 virtual ~Item();
456 virtual bool Parse(const std::string& value) const = 0;
460 virtual bool HasDefault() const;
464 virtual std::string GetDefault() const = 0;
465 }; // class Item
466
471 template <typename T>
472 class UserItem : public Item
473 {
474 public:
475 // Inherited
476 bool Parse(const std::string& value) const override;
477 bool HasDefault() const override;
478 std::string GetDefault() const override;
479
481 std::string m_default;
482 }; // class UserItem
483
488 class StringItem : public Item
489 {
490 public:
491 // Inherited
492 bool Parse(const std::string& value) const override;
493 bool HasDefault() const override;
494 std::string GetDefault() const override;
495
501 mutable std::string m_value;
502 }; // class StringItem
503
508 class CharStarItem : public Item
509 {
510 public:
511 // Inherited
512 bool Parse(const std::string& value) const override;
513 bool HasDefault() const override;
514 std::string GetDefault() const override;
515
517 char* m_buffer;
519 std::size_t m_size;
521 std::string m_default;
522 }; // class CharStarItem
523
528 class CallbackItem : public Item
529 {
530 public:
531 // Inherited
532 bool Parse(const std::string& value) const override;
533 bool HasDefault() const override;
534 std::string GetDefault() const override;
535
537 std::string m_default;
538 }; // class CallbackItem
539
549 using HasOptionName = std::tuple<bool, std::string, std::string>;
550
557 HasOptionName GetOptionName(const std::string& param) const;
565 void HandleHardOptions(const std::vector<std::string>& args) const;
566
573 bool HandleOption(const std::string& param) const;
574
581 bool HandleNonOption(const std::string& value);
582
591 bool HandleArgument(const std::string& name, const std::string& value) const;
599 static bool HandleAttribute(const std::string& name, const std::string& value);
600
605 void PrintGlobals(std::ostream& os) const;
613 void PrintAttributes(std::ostream& os, const std::string& type) const;
621 void PrintAttributeList(std::ostream& os, const TypeId tid, std::stringstream& header) const;
628 void PrintGroup(std::ostream& os, const std::string& group) const;
634 void PrintTypeIds(std::ostream& os) const;
640 void PrintGroups(std::ostream& os) const;
646 void Copy(const CommandLine& cmd);
648 void Clear();
654 void PrintDoxygenUsage() const;
655
657 using Items = std::vector<std::shared_ptr<Item>>;
658
663
665 std::size_t m_NNonOptions;
667 std::size_t m_nonOptionCount;
669 std::string m_usage;
671 std::string m_shortName;
672
673}; // class CommandLine
674
682namespace CommandLineHelper
683{
684
694template <typename T>
695bool UserItemParse(const std::string& value, T& dest);
703template <>
704bool UserItemParse<bool>(const std::string& value, bool& dest);
713template <>
714bool UserItemParse<uint8_t>(const std::string& value, uint8_t& dest);
715
725template <typename T>
726std::string GetDefault(const std::string& defaultValue);
727template <>
728std::string GetDefault<bool>(const std::string& defaultValue);
729template <>
730std::string GetDefault<Time>(const std::string& defaultValue);
733} // namespace CommandLineHelper
734
735} // namespace ns3
736
737/********************************************************************
738 * Implementation of the templates declared above.
739 ********************************************************************/
740
741namespace ns3
742{
743
744template <typename T>
745void
746CommandLine::AddValue(const std::string& name, const std::string& help, T& value)
747{
748 auto item = std::make_shared<UserItem<T>>();
749 item->m_name = name;
750 item->m_help = help;
751 item->m_valuePtr = &value;
752
753 std::stringstream ss;
754 ss << value;
755 ss >> item->m_default;
756
757 m_options.push_back(item);
758}
759
760template <typename T>
761void
762CommandLine::AddNonOption(const std::string& name, const std::string& help, T& value)
763{
764 auto item = std::make_shared<UserItem<T>>();
765 item->m_name = name;
766 item->m_help = help;
767 item->m_valuePtr = &value;
768
769 std::stringstream ss;
770 ss << value;
771 ss >> item->m_default;
772 m_nonOptions.push_back(item);
774}
775
776template <typename T>
777bool
779{
780 return !m_default.empty();
781}
782
783template <typename T>
784std::string
786{
787 return CommandLineHelper::GetDefault<T>(m_default);
788}
789
790template <typename T>
791std::string
792CommandLineHelper::GetDefault(const std::string& defaultValue)
793{
794 return defaultValue;
795}
796
797template <typename T>
798bool
799CommandLine::UserItem<T>::Parse(const std::string& value) const
800{
801 return CommandLineHelper::UserItemParse<T>(value, *m_valuePtr);
802}
803
804template <typename T>
805bool
806CommandLineHelper::UserItemParse(const std::string& value, T& dest)
807{
808 std::istringstream iss;
809 iss.str(value);
810 iss >> dest;
811 return !iss.bad() && !iss.fail();
812}
813
833std::ostream& operator<<(std::ostream& os, const CommandLine& cmd);
834
835} // namespace ns3
836
837#endif /* COMMAND_LINE_H */
Declaration of the various callback functions.
Callback template class.
Definition: callback.h:438
An argument Item using a Callback to parse the input.
Definition: command-line.h:529
bool HasDefault() const override
std::string GetDefault() const override
bool Parse(const std::string &value) const override
Parse from a string.
std::string m_default
The default value, as a string, if it exists.
Definition: command-line.h:537
ns3::Callback< bool, const std::string & > m_callback
The Callback.
Definition: command-line.h:536
Extension of Item for char*.
Definition: command-line.h:509
char * m_buffer
The buffer to write in to.
Definition: command-line.h:517
std::string m_default
The default value.
Definition: command-line.h:521
bool Parse(const std::string &value) const override
Parse from a string.
std::string GetDefault() const override
std::size_t m_size
The size of the buffer, including terminating null.
Definition: command-line.h:519
bool HasDefault() const override
The argument abstract base class.
Definition: command-line.h:445
virtual bool Parse(const std::string &value) const =0
Parse from a string.
virtual ~Item()
Destructor.
virtual bool HasDefault() const
virtual std::string GetDefault() const =0
std::string m_name
Argument label: --m_name=...
Definition: command-line.h:447
std::string m_help
Argument help string.
Definition: command-line.h:448
Extension of Item for extra non-options, stored as strings.
Definition: command-line.h:489
bool Parse(const std::string &value) const override
Parse from a string.
std::string GetDefault() const override
std::string m_value
The argument value.
Definition: command-line.h:501
bool HasDefault() const override
An argument Item assigning to POD.
Definition: command-line.h:473
bool HasDefault() const override
Definition: command-line.h:778
std::string m_default
String representation of default value.
Definition: command-line.h:481
std::string GetDefault() const override
Definition: command-line.h:785
T * m_valuePtr
Pointer to the POD location.
Definition: command-line.h:480
bool Parse(const std::string &value) const override
Parse from a string.
Definition: command-line.h:799
Parse command-line arguments.
Definition: command-line.h:232
void PrintAttributeList(std::ostream &os, const TypeId tid, std::stringstream &header) const
Print the Attributes for a single type.
HasOptionName GetOptionName(const std::string &param) const
Strip leading -- or - from options.
std::tuple< bool, std::string, std::string > HasOptionName
Tuple type returned by GetOptionName().
Definition: command-line.h:549
void PrintGroups(std::ostream &os) const
Handler for --PrintGroups: print all TypeId group names.
void PrintTypeIds(std::ostream &os) const
Handler for --PrintTypeIds: print all TypeId names.
std::string GetExtraNonOption(std::size_t i) const
Get extra non-option arguments by index.
std::vector< std::shared_ptr< Item > > Items
Argument list container.
Definition: command-line.h:657
std::size_t m_nonOptionCount
The number of actual non-option arguments seen so far.
Definition: command-line.h:667
std::size_t GetNExtraNonOptions() const
Get the total number of non-option arguments found, including those configured with AddNonOption() an...
void PrintDoxygenUsage() const
Append usage message in Doxygen format to the file indicated by the NS_COMMANDLINE_INTROSPECTION envi...
~CommandLine()
Destructor.
std::string GetName() const
Get the program name.
Items m_options
The list of option arguments.
Definition: command-line.h:660
bool HandleNonOption(const std::string &value)
Handle a non-option.
void Parse(int argc, char *argv[])
Parse the program arguments.
void PrintGroup(std::ostream &os, const std::string &group) const
Handler for --PrintGroup: print all types belonging to a given group.
void Copy(const CommandLine &cmd)
Copy constructor implementation.
std::size_t m_NNonOptions
The expected number of non-option arguments.
Definition: command-line.h:665
void PrintGlobals(std::ostream &os) const
Handler for --PrintGlobals: print all global variables and values.
Items m_nonOptions
The list of non-option arguments.
Definition: command-line.h:662
void PrintVersion(std::ostream &os) const
Print ns-3 version to the desired output stream.
void HandleHardOptions(const std::vector< std::string > &args) const
Handle hard-coded options.
std::string m_shortName
The source file name (without .cc), as would be given to ns3 run
Definition: command-line.h:671
bool HandleOption(const std::string &param) const
Handle an option in the form param=value.
std::string m_usage
The Usage string.
Definition: command-line.h:669
void Clear()
Remove all arguments, Usage(), name.
void PrintAttributes(std::ostream &os, const std::string &type) const
Handler for --PrintAttributes: print the attributes for a given type as well as its parents.
bool HandleArgument(const std::string &name, const std::string &value) const
Match name against the program or general arguments, and dispatch to the appropriate handler.
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:746
static bool HandleAttribute(const std::string &name, const std::string &value)
Callback function to handle attributes.
CommandLine()
Constructor.
void PrintHelp(std::ostream &os) const
Print program usage to the desired output stream.
std::string GetVersion() const
Get the program version.
void AddNonOption(const std::string &name, const std::string &help, T &value)
Add a non-option argument, assigning to POD.
Definition: command-line.h:762
CommandLine & operator=(const CommandLine &cmd)
Assignment.
void Usage(const std::string &usage)
Supply the program usage and documentation.
a unique identifier for an interface.
Definition: type-id.h:59
std::string GetDefault< Time >(const std::string &defaultValue)
Helper to specialize CommandLine::UserItem::GetDefault() on types needing special handling.
bool UserItemParse(const std::string &value, T &dest)
Helpers to specialize CommandLine::UserItem::Parse()
Definition: command-line.h:806
std::string GetDefault< bool >(const std::string &defaultValue)
Helper to specialize CommandLine::UserItem::GetDefault() on types needing special handling.
std::string GetDefault(const std::string &defaultValue)
Helper to specialize CommandLine::UserItem::GetDefault() on types needing special handling.
Definition: command-line.h:792
bool UserItemParse< bool >(const std::string &value, bool &dest)
Specialization of CommandLine::UserItem::Parse() to bool.
bool UserItemParse< uint8_t >(const std::string &value, uint8_t &dest)
Specialization of CommandLine::UserItem::Parse() to uint8_t to distinguish from char.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:129
Declaration of classes ns3::Time and ns3::TimeWithUnit, and the TimeValue implementation classes.
ns3::TypeId declaration; inline and template implementations.