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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8#ifndef COMMAND_LINE_H
9#define COMMAND_LINE_H
10
11#include "callback.h"
12#include "nstime.h"
13#include "type-id.h"
14
15#include <memory> // shared_ptr
16#include <sstream>
17#include <string>
18#include <tuple>
19#include <vector>
20
21/**
22 * @file
23 * @ingroup commandline
24 * ns3::CommandLine declaration.
25 */
26
27namespace ns3
28{
29
30/**
31 * @ingroup core
32 * @defgroup commandline Command Line Parsing
33 *
34 * A uniform way to specify program documentation,
35 * allowed command line arguments and help strings,
36 * and set any attribute or global value, all from
37 * the command line directly.
38 *
39 * The main entry point is CommandLine
40 */
41
42/**
43 * @ingroup commandline
44 * @brief Parse command-line arguments
45 *
46 * Instances of this class can be used to parse command-line
47 * arguments. Programs can register a general usage message with
48 * CommandLine::Usage, and arguments with CommandLine::AddValue.
49 * Argument variable types with input streamers (`operator>>`)
50 * can be set directly; more complex argument parsing
51 * can be accomplished by providing a Callback.
52 *
53 * CommandLine also provides handlers for these standard arguments:
54 * @verbatim
55 --PrintGlobals: Print the list of globals.
56 --PrintGroups: Print the list of groups.
57 --PrintGroup=[group]: Print all TypeIds of group.
58 --PrintTypeIds: Print all TypeIds.
59 --PrintAttributes=[typeid]: Print all attributes of typeid.
60 --PrintVersion: Print the ns-3 version.
61 --PrintHelp: Print this help message. \endverbatim
62 *
63 * The more common \c \--version is a synonym for \c \--PrintVersion.
64 *
65 * The more common \c \--help is a synonym for \c \--PrintHelp; an example
66 * is given below.
67 *
68 * CommandLine can also handle non-option arguments
69 * (often called simply "positional" parameters: arguments which don't begin
70 * with "-" or "--"). These can be parsed directly in to variables,
71 * by registering arguments with AddNonOption in the order expected.
72 * Additional non-option arguments encountered will be captured as strings.
73 *
74 * Finally, CommandLine processes Attribute and GlobalValue arguments.
75 * Default values for specific attributes can be set using a shorthand
76 * argument name.
77 *
78 * In use, arguments are given in the form
79 * @verbatim
80 --arg=value --toggle first-non-option\endverbatim
81 * Most arguments expect a value, as in the first form, \c \--arg=value.
82 * Toggles, corresponding to boolean arguments, can be given in any of
83 * the forms
84 * @verbatim
85 --toggle1 --toggle2=1 --toggle3=t --toggle4=true \endverbatim
86 * The first form changes the state of toggle1 from its default;
87 * all the rest set the corresponding boolean variable to true.
88 * \c 0, \c f and \c false are accepted to set the variable to false.
89 * Option arguments can appear in any order on the command line,
90 * even intermixed with non-option arguments.
91 * The order of non-option arguments is preserved.
92 *
93 * Option arguments can be repeated on the command line; the last value given
94 * will be the final value used. For example,
95 * @verbatim
96 --arg=one --toggle=f --arg=another --toggle \endverbatim
97 * The variable set by \c \--arg will end up with the value \c "another";
98 * the boolean set by \c \--toggle will end up as \c true.
99 *
100 * Because arguments can be repeated it can be hard to decipher what
101 * value each variable ended up with, especially when using boolean toggles.
102 * Suggested best practice is for scripts to report the values of all items
103 * settable through CommandLine, as done by the example below.
104 *
105 *
106 * CommandLine can set the initial value of every attribute in the system
107 * with the \c \--TypeIdName::AttributeName=value syntax, for example
108 * @verbatim
109 --Application::StartTime=3s \endverbatim
110 * In some cases you may want to highlight the use of a particular
111 * attribute for a simulation script. For example, you might want
112 * to make it easy to set the \c Application::StartTime using
113 * the argument \c \--start, and have its help string show as part
114 * of the help message. This can be done using the
115 * @link AddValue(const std::string&, const std::string&) AddValue (name, attributePath) \endlink
116 * method.
117 *
118 * CommandLine can also set the value of every GlobalValue
119 * in the system with the \c \--GlobalValueName=value syntax, for example
120 * @verbatim
121 --SchedulerType=HeapScheduler \endverbatim
122 *
123 * A simple example of CommandLine is in `src/core/example/``command-line-example.cc`
124 * See that file for an example of handling non-option arguments.
125 *
126 * The heart of that example is this code:
127 *
128 * @code
129 * int intArg = 1;
130 * bool boolArg = false;
131 * std::string strArg = "strArg default";
132 *
133 * CommandLine cmd (__FILE__);
134 * cmd.Usage ("CommandLine example program.\n"
135 * "\n"
136 * "This little program demonstrates how to use CommandLine.");
137 * cmd.AddValue ("intArg", "an int argument", intArg);
138 * cmd.AddValue ("boolArg", "a bool argument", boolArg);
139 * cmd.AddValue ("strArg", "a string argument", strArg);
140 * cmd.AddValue ("anti", "ns3::RandomVariableStream::Antithetic");
141 * cmd.AddValue ("cbArg", "a string via callback", MakeCallback (SetCbArg));
142 * cmd.Parse (argc, argv);
143 * @endcode
144 * after which it prints the values of each variable. (The \c SetCbArg function
145 * is not shown here; see `src/core/example/``command-line-example.cc`)
146 *
147 * Here is the output from a few runs of that program:
148 *
149 * @verbatim
150 $ ./ns3 run "command-line-example"
151 intArg: 1
152 boolArg: false
153 strArg: "strArg default"
154 cbArg: "cbArg default"
155
156 $ ./ns3 run "command-line-example --intArg=2 --boolArg --strArg=Hello --cbArg=World"
157 intArg: 2
158 boolArg: true
159 strArg: "Hello"
160 cbArg: "World"
161
162 $ ./ns3 run "command-line-example --help"
163 ns3-dev-command-line-example-debug [Program Arguments] [General Arguments]
164
165 CommandLine example program.
166
167 This little program demonstrates how to use CommandLine.
168
169 Program Arguments:
170 --intArg: an int argument [1]
171 --boolArg: a bool argument [false]
172 --strArg: a string argument [strArg default]
173 --anti: Set this RNG stream to generate antithetic values
174 (ns3::RandomVariableStream::Antithetic) [false]
175 --cbArg: a string via callback
176
177 General Arguments:
178 --PrintGlobals: Print the list of globals.
179 --PrintGroups: Print the list of groups.
180 --PrintGroup=[group]: Print all TypeIds of group.
181 --PrintTypeIds: Print all TypeIds.
182 --PrintAttributes=[typeid]: Print all attributes of typeid.
183 --PrintVersion: Print the ns-3 version.
184 --PrintHelp: Print this help message. \endverbatim
185 *
186 * Having parsed the arguments, some programs will need to perform
187 * some additional validation of the received values. A common issue at this
188 * point is to discover that the supplied arguments are incomplete or
189 * incompatible. Suggested best practice is to supply an error message
190 * and the complete usage message. For example,
191 *
192 * @code
193 * int value1;
194 * int value2;
195 *
196 * CommandLine cmd (__FILE__);
197 * cmd.Usage ("...");
198 * cmd.AddValue ("value1", "first value", value1);
199 * cmd.AddValue ("value2", "second value", value1);
200 *
201 * cmd.Parse (argc, argv);
202 *
203 * if (value1 * value2 < 0)
204 * {
205 * std::cerr << "value1 and value2 must have the same sign!" << std::endl;
206 * std::cerr << cmd;
207 * exit (-1);
208 * }
209 * @endcode
210 *
211 * Finally, note that for examples which will be run by \c test.py
212 * the preferred declaration of a CommandLine instance is
213 *
214 * @code
215 * CommandLine cmd (__FILE__);
216 * @endcode
217 * This will ensure that the program usage and arguments can be added to
218 * the Doxygen documentation automatically.
219 */
221{
222 public:
223 /** Constructor */
224 CommandLine();
225 /**
226 * Construct and register the source file name.
227 * This would typically be called by
228 * CommandLine cmd (__FILE__);
229 *
230 * This form is required to generate Doxygen documentation of the
231 * arguments and options.
232 *
233 * @param [in] filename The source file name.
234 */
235 CommandLine(const std::string& filename);
236 /**
237 * Copy constructor
238 *
239 * @param [in] cmd The CommandLine to copy from
240 */
241 CommandLine(const CommandLine& cmd);
242 /**
243 * Assignment
244 *
245 * @param [in] cmd The CommandLine to assign from
246 * @return The CommandLine
247 */
248 CommandLine& operator=(const CommandLine& cmd);
249 /** Destructor */
250 ~CommandLine();
251
252 /**
253 * Supply the program usage and documentation.
254 *
255 * @param [in] usage Program usage message to write with \c \--help.
256 */
257 void Usage(const std::string& usage);
258
259 /**
260 * Add a program argument, assigning to POD
261 *
262 * @param [in] name The name of the program-supplied argument
263 * @param [in] help The help text used by \c \--PrintHelp
264 * @param [out] value A reference to the variable where the
265 * value parsed will be stored (if no value
266 * is parsed, this variable is not modified).
267 */
268 template <typename T>
269 void AddValue(const std::string& name, const std::string& help, T& value);
270
271 /**
272 * Retrieve the \c char* in \c char* and add it as a program argument
273 *
274 * This variant it used to receive C string pointers
275 * from the python bindings.
276 *
277 * The C string result stored in \c value will be null-terminated,
278 * and have a maximum length of \c (num - 1).
279 * The result will be truncated to fit, if necessary.
280 *
281 * @param [in] name The name of the program-supplied argument
282 * @param [in] help The help text used by \c \--PrintHelp
283 * @param [out] value A pointer pointing to the beginning
284 * of a null-terminated C string buffer provided by
285 * the caller. The parsed value will be stored in
286 * that buffer (if no value is parsed, this
287 * variable is not modified).
288 * @param num The size of the buffer pointed to by \c value,
289 * including any terminating null.
290 */
291 void AddValue(const std::string& name, const std::string& help, char* value, std::size_t num);
292 /**
293 * Callback function signature for
294 * AddValue(const std::string&,const std::string&,Callback<bool,const std::string>).
295 *
296 * @param [in] value The argument value.
297 */
298 typedef bool (*Callback)(const std::string& value);
299
300 /**
301 * Add a program argument, using a Callback to parse the value
302 *
303 * @param [in] name The name of the program-supplied argument
304 * @param [in] help The help text used by \c \--help
305 * @param [in] callback A Callback function that will be invoked to parse and
306 * store the value.
307 * @param [in] defaultValue Optional default value for argument.
308 *
309 * The callback should have the signature
310 * CommandLine::Callback
311 */
312 void AddValue(const std::string& name,
313 const std::string& help,
315 const std::string& defaultValue = "");
316
317 /**
318 * Add a program argument as a shorthand for an Attribute.
319 *
320 * @param [in] name The name of the program-supplied argument.
321 * @param [out] attributePath The fully-qualified name of the Attribute
322 */
323 void AddValue(const std::string& name, const std::string& attributePath);
324
325 /**
326 * Add a non-option argument, assigning to POD
327 *
328 * @param [in] name The name of the program-supplied argument
329 * @param [in] help The help text used by \c \--PrintHelp
330 * @param [out] value A reference to the variable where the
331 * value parsed will be stored (if no value
332 * is parsed, this variable is not modified).
333 */
334 template <typename T>
335 void AddNonOption(const std::string& name, const std::string& help, T& value);
336
337 /**
338 * Get extra non-option arguments by index.
339 * This allows CommandLine to accept more non-option arguments than
340 * have been configured explicitly with AddNonOption().
341 *
342 * This is only valid after calling Parse().
343 *
344 * @param [in] i The index of the non-option argument to return.
345 * @return The i'th non-option argument, as a string.
346 */
347 std::string GetExtraNonOption(std::size_t i) const;
348
349 /**
350 * Get the total number of non-option arguments found,
351 * including those configured with AddNonOption() and extra non-option
352 * arguments.
353 *
354 * This is only valid after calling Parse().
355 *
356 * @returns the number of non-option arguments found.
357 */
358 std::size_t GetNExtraNonOptions() const;
359
360 /**
361 * Parse the program arguments
362 *
363 * @param [in] argc The 'argc' variable: number of arguments (including the
364 * main program name as first element).
365 * @param [in] argv The 'argv' variable: a null-terminated array of strings,
366 * each of which identifies a command-line argument.
367 *
368 * Obviously, this method will parse the input command-line arguments and
369 * will attempt to handle them all.
370 *
371 * As a side effect, this method saves the program basename, which
372 * can be retrieved by GetName().
373 */
374 void Parse(int argc, char* argv[]);
375
376 /**
377 * Parse the program arguments.
378 *
379 * This version may be convenient when synthesizing arguments
380 * programmatically. Other than the type of argument this behaves
381 * identically to Parse(int, char *)
382 *
383 * @param [in] args The vector of arguments.
384 */
385 void Parse(std::vector<std::string> args);
386
387 /**
388 * Get the program name
389 *
390 * @return The program name. Only valid after calling Parse()
391 */
392 std::string GetName() const;
393
394 /**
395 * @brief Print program usage to the desired output stream
396 *
397 * Handler for \c \--PrintHelp and \c \--help: print Usage(), argument names, and help strings
398 *
399 * Alternatively, an overloaded operator << can be used:
400 * @code
401 * CommandLine cmd (__FILE__);
402 * cmd.Parse (argc, argv);
403 * ...
404 *
405 * std::cerr << cmd;
406 * @endcode
407 *
408 * @param [in,out] os The output stream to print on.
409 */
410 void PrintHelp(std::ostream& os) const;
411
412 /**
413 * Get the program version.
414 *
415 * @return The program version
416 */
417 std::string GetVersion() const;
418
419 /**
420 * Print ns-3 version to the desired output stream
421 *
422 * Handler for \c \--PrintVersion and \c \--version.
423 *
424 * @param [in,out] os The output stream to print on.
425 */
426 void PrintVersion(std::ostream& os) const;
427
428 private:
429 /**
430 * @ingroup commandline
431 * @brief The argument abstract base class
432 */
433 class Item
434 {
435 public:
436 std::string m_name; /**< Argument label: \c \--m_name=... */
437 std::string m_help; /**< Argument help string */
438 virtual ~Item(); /**< Destructor */
439 /**
440 * Parse from a string.
441 *
442 * @param [in] value The string representation
443 * @return \c true if parsing the value succeeded
444 */
445 virtual bool Parse(const std::string& value) const = 0;
446 /**
447 * @return \c true if this item has a default value.
448 */
449 virtual bool HasDefault() const;
450 /**
451 * @return The default value
452 */
453 virtual std::string GetDefault() const = 0;
454 }; // class Item
455
456 /**
457 * @ingroup commandline
458 * @brief An argument Item assigning to POD
459 */
460 template <typename T>
461 class UserItem : public Item
462 {
463 public:
464 // Inherited
465 bool Parse(const std::string& value) const override;
466 bool HasDefault() const override;
467 std::string GetDefault() const override;
468
469 T* m_valuePtr; /**< Pointer to the POD location */
470 std::string m_default; /**< String representation of default value */
471 }; // class UserItem
472
473 /**
474 * @ingroup commandline
475 * @brief Extension of Item for extra non-options, stored as strings.
476 */
477 class StringItem : public Item
478 {
479 public:
480 // Inherited
481 bool Parse(const std::string& value) const override;
482 bool HasDefault() const override;
483 std::string GetDefault() const override;
484
485 /**
486 * The argument value.
487 * @internal This has to be \c mutable because the Parse()
488 * function is \c const in the base class Item.
489 */
490 mutable std::string m_value;
491 }; // class StringItem
492
493 /**
494 * @ingroup commandline
495 * @brief Extension of Item for \c char*.
496 */
497 class CharStarItem : public Item
498 {
499 public:
500 // Inherited
501 bool Parse(const std::string& value) const override;
502 bool HasDefault() const override;
503 std::string GetDefault() const override;
504
505 /** The buffer to write in to. */
506 char* m_buffer;
507 /** The size of the buffer, including terminating null. */
508 std::size_t m_size;
509 /** The default value. */
510 std::string m_default;
511 }; // class CharStarItem
512
513 /**
514 * @ingroup commandline
515 * @brief An argument Item using a Callback to parse the input
516 */
517 class CallbackItem : public Item
518 {
519 public:
520 // Inherited
521 bool Parse(const std::string& value) const override;
522 bool HasDefault() const override;
523 std::string GetDefault() const override;
524
526 std::string m_default; /**< The default value, as a string, if it exists. */
527 }; // class CallbackItem
528
529 /**
530 * Tuple type returned by GetOptionName().
531 *
532 * | Field | Meaning
533 * |----------|--------------------------------------------
534 * | `get<0>` | Is this an option (beginning with `-`)?
535 * | `get<1>` | The option name (after any `-`, before `=`)
536 * | `get<2>` | The value (after any `=`)
537 */
538 using HasOptionName = std::tuple<bool, std::string, std::string>;
539
540 /**
541 * Strip leading `--` or `-` from options.
542 *
543 * @param [in] param Option name to search
544 * @returns \c false if none found, indicating this is a non-option.
545 */
546 HasOptionName GetOptionName(const std::string& param) const;
547 /**
548 * Handle hard-coded options.
549 *
550 * \note: if any hard-coded options are found this function exits.
551 *
552 * @param [in] args Vector of hard-coded options to handle.
553 */
554 void HandleHardOptions(const std::vector<std::string>& args) const;
555
556 /**
557 * Handle an option in the form \c param=value.
558 *
559 * @param [in] param The option string.
560 * @returns \c true if this was really an option.
561 */
562 bool HandleOption(const std::string& param) const;
563
564 /**
565 * Handle a non-option
566 *
567 * @param [in] value The command line non-option value.
568 * @return \c true if \c value could be parsed correctly.
569 */
570 bool HandleNonOption(const std::string& value);
571
572 /**
573 * Match name against the program or general arguments,
574 * and dispatch to the appropriate handler.
575 *
576 * @param [in] name The argument name
577 * @param [in] value The command line value
578 * @returns \c true if the argument was handled successfully
579 */
580 bool HandleArgument(const std::string& name, const std::string& value) const;
581 /**
582 * Callback function to handle attributes.
583 *
584 * @param [in] name The full name of the Attribute.
585 * @param [in] value The value to assign to \pname{name}.
586 * @return \c true if the value was set successfully, false otherwise.
587 */
588 static bool HandleAttribute(const std::string& name, const std::string& value);
589
590 /**
591 * Handler for \c \--PrintGlobals: print all global variables and values
592 * @param [in,out] os The output stream to print on.
593 */
594 void PrintGlobals(std::ostream& os) const;
595 /**
596 * Handler for \c \--PrintAttributes: print the attributes for a given type
597 * as well as its parents.
598 *
599 * @param [in,out] os the output stream.
600 * @param [in] type The type name whose Attributes should be displayed,
601 */
602 void PrintAttributes(std::ostream& os, const std::string& type) const;
603 /**
604 * Print the Attributes for a single type.
605 *
606 * @param [in,out] os the output stream.
607 * @param [in] tid The TypeId whose Attributes should be displayed,
608 * @param [in] header A header line to print if \c tid has Attributes
609 */
610 void PrintAttributeList(std::ostream& os, const TypeId tid, std::stringstream& header) const;
611 /**
612 * Handler for \c \--PrintGroup: print all types belonging to a given group.
613 *
614 * @param [in,out] os The output stream.
615 * @param [in] group The name of the TypeId group to display
616 */
617 void PrintGroup(std::ostream& os, const std::string& group) const;
618 /**
619 * Handler for \c \--PrintTypeIds: print all TypeId names.
620 *
621 * @param [in,out] os The output stream.
622 */
623 void PrintTypeIds(std::ostream& os) const;
624 /**
625 * Handler for \c \--PrintGroups: print all TypeId group names
626 *
627 * @param [in,out] os The output stream.
628 */
629 void PrintGroups(std::ostream& os) const;
630 /**
631 * Copy constructor implementation
632 *
633 * @param [in] cmd CommandLine to copy
634 */
635 void Copy(const CommandLine& cmd);
636 /** Remove all arguments, Usage(), name */
637 void Clear();
638 /**
639 * Append usage message in Doxygen format to the file indicated
640 * by the NS_COMMANDLINE_INTROSPECTION environment variable.
641 * This is typically only called once, by Parse().
642 */
643 void PrintDoxygenUsage() const;
644
645 /** Argument list container */
646 using Items = std::vector<std::shared_ptr<Item>>;
647
648 /** The list of option arguments */
650 /** The list of non-option arguments */
652
653 /** The expected number of non-option arguments */
654 std::size_t m_NNonOptions;
655 /** The number of actual non-option arguments seen so far. */
656 std::size_t m_nonOptionCount;
657 /** The Usage string */
658 std::string m_usage;
659 /** The source file name (without `.cc`), as would be given to `ns3 run` */
660 std::string m_shortName;
661
662}; // class CommandLine
663
664/**
665 * @ingroup commandline
666 * @defgroup commandlinehelper Helpers to specialize UserItem
667 */
668/**
669 * @ingroup commandlinehelper
670 * @brief Helpers for CommandLine to specialize UserItem
671 */
672namespace CommandLineHelper
673{
674
675/**
676 * @ingroup commandlinehelper
677 * @brief Helpers to specialize CommandLine::UserItem::Parse()
678 *
679 * @param [in] value The argument name
680 * @param [out] dest The argument location
681 * @tparam T \deduced The type being specialized
682 * @return \c true if parsing was successful
683 */
684template <typename T>
685bool UserItemParse(const std::string& value, T& dest);
686/**
687 * @brief Specialization of CommandLine::UserItem::Parse() to \c bool
688 *
689 * @param [in] value The argument name
690 * @param [out] dest The boolean variable to set
691 * @return \c true if parsing was successful
692 */
693template <>
694bool UserItemParse<bool>(const std::string& value, bool& dest);
695/**
696 * @brief Specialization of CommandLine::UserItem::Parse() to \c uint8_t
697 * to distinguish from \c char
698 *
699 * @param [in] value The argument name
700 * @param [out] dest The \c uint8_t variable to set
701 * @return \c true if parsing was successful
702 */
703template <>
704bool UserItemParse<uint8_t>(const std::string& value, uint8_t& dest);
705
706/**
707 * @ingroup commandlinehelper
708 * @brief Helper to specialize CommandLine::UserItem::GetDefault() on types
709 * needing special handling.
710 *
711 * @param [in] defaultValue The default value from the UserItem.
712 * @return The string representation of value.
713 * @{
714 */
715template <typename T>
716std::string GetDefault(const std::string& defaultValue);
717template <>
718std::string GetDefault<bool>(const std::string& defaultValue);
719template <>
720std::string GetDefault<Time>(const std::string& defaultValue);
721/**@}*/
722
723} // namespace CommandLineHelper
724
725} // namespace ns3
726
727/********************************************************************
728 * Implementation of the templates declared above.
729 ********************************************************************/
730
731namespace ns3
732{
733
734template <typename T>
735void
736CommandLine::AddValue(const std::string& name, const std::string& help, T& value)
737{
738 auto item = std::make_shared<UserItem<T>>();
739 item->m_name = name;
740 item->m_help = help;
741 item->m_valuePtr = &value;
742
743 std::stringstream ss;
744 ss << value;
745 ss >> item->m_default;
746
747 m_options.push_back(item);
748}
749
750template <typename T>
751void
752CommandLine::AddNonOption(const std::string& name, const std::string& help, T& value)
753{
754 auto item = std::make_shared<UserItem<T>>();
755 item->m_name = name;
756 item->m_help = help;
757 item->m_valuePtr = &value;
758
759 std::stringstream ss;
760 ss << value;
761 ss >> item->m_default;
762 m_nonOptions.push_back(item);
764}
765
766template <typename T>
767bool
769{
770 return !m_default.empty();
771}
772
773template <typename T>
774std::string
779
780template <typename T>
781std::string
782CommandLineHelper::GetDefault(const std::string& defaultValue)
783{
784 return defaultValue;
785}
786
787template <typename T>
788bool
789CommandLine::UserItem<T>::Parse(const std::string& value) const
790{
791 return CommandLineHelper::UserItemParse<T>(value, *m_valuePtr);
792}
793
794template <typename T>
795bool
796CommandLineHelper::UserItemParse(const std::string& value, T& dest)
797{
798 std::istringstream iss;
799 iss.str(value);
800 iss >> dest;
801 return !iss.bad() && !iss.fail();
802}
803
804/**
805 * Overloaded operator << to print program usage
806 * (shortcut for CommandLine::PrintHelper)
807 *
808 * @see CommandLine::PrintHelper
809 *
810 * Example usage:
811 * @code
812 * CommandLine cmd (__FILE__);
813 * cmd.Parse (argc, argv);
814 * ...
815 *
816 * std::cerr << cmd;
817 * @endcode
818 *
819 * @param [in,out] os The stream to print on.
820 * @param [in] cmd The CommandLine describing the program.
821 * @returns The stream.
822 */
823std::ostream& operator<<(std::ostream& os, const CommandLine& cmd);
824
825} // namespace ns3
826
827#endif /* COMMAND_LINE_H */
Declaration of the various callback functions.
Callback template class.
Definition callback.h:422
An argument Item using a Callback to parse the input.
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.
ns3::Callback< bool, const std::string & > m_callback
The Callback.
Extension of Item for char*.
char * m_buffer
The buffer to write in to.
std::string m_default
The default value.
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.
bool HasDefault() const override
The argument abstract base class.
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=...
std::string m_help
Argument help string.
Extension of Item for extra non-options, stored as strings.
bool Parse(const std::string &value) const override
Parse from a string.
std::string GetDefault() const override
std::string m_value
The argument value.
bool HasDefault() const override
An argument Item assigning to POD.
bool HasDefault() const override
std::string m_default
String representation of default value.
std::string GetDefault() const override
T * m_valuePtr
Pointer to the POD location.
bool Parse(const std::string &value) const override
Parse from a string.
Parse command-line arguments.
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().
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::size_t m_nonOptionCount
The number of actual non-option arguments seen so far.
std::size_t GetNExtraNonOptions() const
Get the total number of non-option arguments found, including those configured with AddNonOption() an...
std::vector< std::shared_ptr< Item > > Items
Argument list container.
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.
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.
void PrintGlobals(std::ostream &os) const
Handler for --PrintGlobals: print all global variables and values.
Items m_nonOptions
The list of non-option arguments.
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
bool HandleOption(const std::string &param) const
Handle an option in the form param=value.
std::string m_usage
The Usage string.
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.
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.
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:48
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()
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.
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:148
Declaration of classes ns3::Time and ns3::TimeWithUnit, and the TimeValue implementation classes.
ns3::TypeId declaration; inline and template implementations.