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
455 // end of class Item
456 };
457
458 /**
459 * @ingroup commandline
460 * @brief An argument Item assigning to POD
461 */
462 template <typename T>
463 class UserItem : public Item
464 {
465 public:
466 // Inherited
467 bool Parse(const std::string& value) const override;
468 bool HasDefault() const override;
469 std::string GetDefault() const override;
470
471 T* m_valuePtr; /**< Pointer to the POD location */
472 std::string m_default; /**< String representation of default value */
473
474 // end of class UserItem
475 };
476
477 /**
478 * @ingroup commandline
479 * @brief Extension of Item for extra non-options, stored as strings.
480 */
481 class StringItem : public Item
482 {
483 public:
484 // Inherited
485 bool Parse(const std::string& value) const override;
486 bool HasDefault() const override;
487 std::string GetDefault() const override;
488
489 /**
490 * The argument value.
491 * @internal This has to be \c mutable because the Parse()
492 * function is \c const in the base class Item.
493 */
494 mutable std::string m_value;
495
496 // end of class StringItem
497 };
498
499 /**
500 * @ingroup commandline
501 * @brief Extension of Item for \c char*.
502 */
503 class CharStarItem : public Item
504 {
505 public:
506 // Inherited
507 bool Parse(const std::string& value) const override;
508 bool HasDefault() const override;
509 std::string GetDefault() const override;
510
511 /** The buffer to write in to. */
512 char* m_buffer;
513 /** The size of the buffer, including terminating null. */
514 std::size_t m_size;
515 /** The default value. */
516 std::string m_default;
517
518 // end of class CharStarItem
519 };
520
521 /**
522 * @ingroup commandline
523 * @brief An argument Item using a Callback to parse the input
524 */
525 class CallbackItem : public Item
526 {
527 public:
528 // Inherited
529 bool Parse(const std::string& value) const override;
530 bool HasDefault() const override;
531 std::string GetDefault() const override;
532
534 std::string m_default; /**< The default value, as a string, if it exists. */
535
536 // end of class CallbackItem
537 };
538
539 /**
540 * Tuple type returned by GetOptionName().
541 *
542 * | Field | Meaning
543 * |----------|--------------------------------------------
544 * | `get<0>` | Is this an option (beginning with `-`)?
545 * | `get<1>` | The option name (after any `-`, before `=`)
546 * | `get<2>` | The value (after any `=`)
547 */
548 using HasOptionName = std::tuple<bool, std::string, std::string>;
549
550 /**
551 * Strip leading `--` or `-` from options.
552 *
553 * @param [in] param Option name to search
554 * @returns \c false if none found, indicating this is a non-option.
555 */
556 HasOptionName GetOptionName(const std::string& param) const;
557 /**
558 * Handle hard-coded options.
559 *
560 * \note: if any hard-coded options are found this function exits.
561 *
562 * @param [in] args Vector of hard-coded options to handle.
563 */
564 void HandleHardOptions(const std::vector<std::string>& args) const;
565
566 /**
567 * Handle an option in the form \c param=value.
568 *
569 * @param [in] param The option string.
570 * @returns \c true if this was really an option.
571 */
572 bool HandleOption(const std::string& param) const;
573
574 /**
575 * Handle a non-option
576 *
577 * @param [in] value The command line non-option value.
578 * @return \c true if \c value could be parsed correctly.
579 */
580 bool HandleNonOption(const std::string& value);
581
582 /**
583 * Match name against the program or general arguments,
584 * and dispatch to the appropriate handler.
585 *
586 * @param [in] name The argument name
587 * @param [in] value The command line value
588 * @returns \c true if the argument was handled successfully
589 */
590 bool HandleArgument(const std::string& name, const std::string& value) const;
591 /**
592 * Callback function to handle attributes.
593 *
594 * @param [in] name The full name of the Attribute.
595 * @param [in] value The value to assign to \pname{name}.
596 * @return \c true if the value was set successfully, false otherwise.
597 */
598 static bool HandleAttribute(const std::string& name, const std::string& value);
599
600 /**
601 * Handler for \c \--PrintGlobals: print all global variables and values
602 * @param [in,out] os The output stream to print on.
603 */
604 void PrintGlobals(std::ostream& os) const;
605 /**
606 * Handler for \c \--PrintAttributes: print the attributes for a given type
607 * as well as its parents.
608 *
609 * @param [in,out] os the output stream.
610 * @param [in] type The type name whose Attributes should be displayed,
611 */
612 void PrintAttributes(std::ostream& os, const std::string& type) const;
613 /**
614 * Print the Attributes for a single type.
615 *
616 * @param [in,out] os the output stream.
617 * @param [in] tid The TypeId whose Attributes should be displayed,
618 * @param [in] header A header line to print if \c tid has Attributes
619 */
620 void PrintAttributeList(std::ostream& os, const TypeId tid, std::stringstream& header) const;
621 /**
622 * Handler for \c \--PrintGroup: print all types belonging to a given group.
623 *
624 * @param [in,out] os The output stream.
625 * @param [in] group The name of the TypeId group to display
626 */
627 void PrintGroup(std::ostream& os, const std::string& group) const;
628 /**
629 * Handler for \c \--PrintTypeIds: print all TypeId names.
630 *
631 * @param [in,out] os The output stream.
632 */
633 void PrintTypeIds(std::ostream& os) const;
634 /**
635 * Handler for \c \--PrintGroups: print all TypeId group names
636 *
637 * @param [in,out] os The output stream.
638 */
639 void PrintGroups(std::ostream& os) const;
640 /**
641 * Copy constructor implementation
642 *
643 * @param [in] cmd CommandLine to copy
644 */
645 void Copy(const CommandLine& cmd);
646 /** Remove all arguments, Usage(), name */
647 void Clear();
648 /**
649 * Append usage message in Doxygen format to the file indicated
650 * by the NS_COMMANDLINE_INTROSPECTION environment variable.
651 * This is typically only called once, by Parse().
652 */
653 void PrintDoxygenUsage() const;
654
655 /** Argument list container */
656 using Items = std::vector<std::shared_ptr<Item>>;
657
658 /**
659 * @brief Checks for duplicate item names in a list of items.
660 *
661 * This function checks for the existence of an item with the same name as the one provided in
662 * the list of items. If a duplicate is found, an error message is generated and the program is
663 * aborted.
664 *
665 * @param[in] name The name of the item to check.
666 * @param[in] optType The type of option that the item represents (e.g. "option", "non-option").
667 * @param[in] items A list of Items to check for duplicate names.
668 */
669 void CheckForDuplicateItemNameError(std::string name, std::string optType, const Items& items);
670 /**
671 * @brief Test for duplicate option arguments.
672 *
673 * @param name The name of the option argument to test for.
674 */
675 void TestForDuplicateOption(std::string name);
676 /**
677 * Tests for duplicate non-option arguments.
678 *
679 * @param name The name of the non-option argument to test for.
680 */
681 void TestForDuplicateNonOption(std::string name);
682
683 /** The list of option arguments */
685 /** The list of non-option arguments */
687
688 /** The expected number of non-option arguments */
689 std::size_t m_NNonOptions;
690 /** The number of actual non-option arguments seen so far. */
691 std::size_t m_nonOptionCount;
692 /** The Usage string */
693 std::string m_usage;
694 /** The source file name (without `.cc`), as would be given to `ns3 run` */
695 std::string m_shortName;
696
697 // end of class CommandLine
698};
699
700/**
701 * @ingroup commandline
702 * @defgroup commandlinehelper Helpers to specialize UserItem
703 */
704/**
705 * @ingroup commandlinehelper
706 * @brief Helpers for CommandLine to specialize UserItem
707 */
709{
710
711/**
712 * @ingroup commandlinehelper
713 * @brief Helpers to specialize CommandLine::UserItem::Parse()
714 *
715 * @param [in] value The argument name
716 * @param [out] dest The argument location
717 * @tparam T \deduced The type being specialized
718 * @return \c true if parsing was successful
719 */
720template <typename T>
721bool UserItemParse(const std::string& value, T& dest);
722/**
723 * @brief Specialization of CommandLine::UserItem::Parse() to \c bool
724 *
725 * @param [in] value The argument name
726 * @param [out] dest The boolean variable to set
727 * @return \c true if parsing was successful
728 */
729template <>
730bool UserItemParse<bool>(const std::string& value, bool& dest);
731/**
732 * @brief Specialization of CommandLine::UserItem::Parse() to \c uint8_t
733 * to distinguish from \c char
734 *
735 * @param [in] value The argument name
736 * @param [out] dest The \c uint8_t variable to set
737 * @return \c true if parsing was successful
738 */
739template <>
740bool UserItemParse<uint8_t>(const std::string& value, uint8_t& dest);
741
742/**
743 * @ingroup commandlinehelper
744 * @brief Helper to specialize CommandLine::UserItem::GetDefault() on types
745 * needing special handling.
746 *
747 * @param [in] defaultValue The default value from the UserItem.
748 * @return The string representation of value.
749 * @{
750 */
751template <typename T>
752std::string GetDefault(const std::string& defaultValue);
753template <>
754std::string GetDefault<bool>(const std::string& defaultValue);
755template <>
756std::string GetDefault<Time>(const std::string& defaultValue);
757/**@}*/
758
759} // namespace CommandLineHelper
760
761} // namespace ns3
762
763/********************************************************************
764 * Implementation of the templates declared above.
765 ********************************************************************/
766
767namespace ns3
768{
769
770template <typename T>
771void
772CommandLine::AddValue(const std::string& name, const std::string& help, T& value)
773{
775 auto item = std::make_shared<UserItem<T>>();
776 item->m_name = name;
777 item->m_help = help;
778 item->m_valuePtr = &value;
779
780 std::stringstream ss;
781 ss << value;
782 item->m_default = ss.str(); // Including white spaces
783
784 m_options.push_back(item);
785}
786
787template <typename T>
788void
789CommandLine::AddNonOption(const std::string& name, const std::string& help, T& value)
790{
792 auto item = std::make_shared<UserItem<T>>();
793 item->m_name = name;
794 item->m_help = help;
795 item->m_valuePtr = &value;
796
797 std::stringstream ss;
798 ss << value;
799 ss >> item->m_default;
800 m_nonOptions.push_back(item);
802}
803
804template <typename T>
805bool
807{
808 return !m_default.empty();
809}
810
811template <typename T>
812std::string
817
818template <typename T>
819std::string
820CommandLineHelper::GetDefault(const std::string& defaultValue)
821{
822 return defaultValue;
823}
824
825template <typename T>
826bool
827CommandLine::UserItem<T>::Parse(const std::string& value) const
828{
830}
831
832template <typename T>
833bool
834CommandLineHelper::UserItemParse(const std::string& value, T& dest)
835{
836 std::istringstream iss;
837 iss.str(value);
838 iss >> dest;
839 return !iss.bad() && !iss.fail();
840}
841
842/**
843 * Overloaded operator << to print program usage
844 * (shortcut for CommandLine::PrintHelper)
845 *
846 * @see CommandLine::PrintHelper
847 *
848 * Example usage:
849 * @code
850 * CommandLine cmd (__FILE__);
851 * cmd.Parse (argc, argv);
852 * ...
853 *
854 * std::cerr << cmd;
855 * @endcode
856 *
857 * @param [in,out] os The stream to print on.
858 * @param [in] cmd The CommandLine describing the program.
859 * @returns The stream.
860 */
861std::ostream& operator<<(std::ostream& os, const CommandLine& cmd);
862
863} // namespace ns3
864
865#endif /* COMMAND_LINE_H */
Declaration of the various callback functions.
Callback template class.
Definition callback.h:428
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.
bool(* Callback)(const std::string &value)
Callback function signature for AddValue(const std::string&,const std::string&,Callback<bool,...
void TestForDuplicateOption(std::string name)
Test for duplicate 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 TestForDuplicateNonOption(std::string name)
Tests for duplicate non-option arguments.
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.
void CheckForDuplicateItemNameError(std::string name, std::string optType, const Items &items)
Checks for duplicate item names in a list of items.
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:50
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.
Helpers for CommandLine to specialize UserItem.
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.