@@ -984,9 +984,7 @@ void ArgumentParser::readArgs(int argc, const char *const *argv)
984984 // check for further arguments
985985 if (!--argc) {
986986 // no arguments specified -> flag default argument as present if one is assigned
987- if (m_defaultArg) {
988- m_defaultArg->m_occurrences .emplace_back (0 );
989- }
987+ assumeDefaultArgument ();
990988 return ;
991989 }
992990
@@ -1041,6 +1039,43 @@ void ArgumentParser::resetArgs()
10411039 m_actualArgc = 0 ;
10421040}
10431041
1042+ /* !
1043+ * \brief Assumes the default argument is present (if not already present).
1044+ * \remarks
1045+ * - Adds an occurrence to the default argument so this argument is assumed to be present (even though
1046+ * it is not actually present).
1047+ * - This function is automatically called if there are no arguments present at all.
1048+ * - This function may be called manually if no actually relevant arguments are present, e.g. if only
1049+ * global flags are present but no main operation argument. Note that for this concrete example there
1050+ * is also ensureDefaultOperation() (which will call assumeDefaultArgument() internally).
1051+ */
1052+ void ArgumentParser::assumeDefaultArgument ()
1053+ {
1054+ if (m_defaultArg && m_defaultArg->m_occurrences .empty ()) {
1055+ m_defaultArg->m_occurrences .emplace_back (0 );
1056+ }
1057+ }
1058+
1059+ /* !
1060+ * \brief Ensures a main operation argument is present.
1061+ * \remarks
1062+ * - Assumes the default argument is present if no other main operation argument or the help argument
1063+ * is present.
1064+ * - Requires the default argument to be an operation argument.
1065+ */
1066+ void ArgumentParser::ensureDefaultOperation ()
1067+ {
1068+ if (!m_defaultArg || m_defaultArg->isPresent () || !m_defaultArg->denotesOperation ()) {
1069+ return ;
1070+ }
1071+ for (auto *const arg : m_mainArgs) {
1072+ if (arg->isPresent () && (arg->denotesOperation () || arg == &m_helpArg)) {
1073+ return ;
1074+ }
1075+ }
1076+ assumeDefaultArgument ();
1077+ }
1078+
10441079/* !
10451080 * \brief Returns the first operation argument specified by the user or nullptr if no operation has been specified.
10461081 * \remarks Only main arguments are considered. See Argument::specifiedOperation() to check sub arguments of a specific
0 commit comments