10#if !defined (HPP_ALIB_CLI_CLIUTIL)
14#if !defined (HPP_ALIB_STRINGS_UTIL_TOKENIZER)
17#if !defined(HPP_ALIB_ENUMS_SERIALIZATION)
23#if !defined (_GLIBCXX_VECTOR) && !defined(_VECTOR_)
34 if ( ( identString.
Length() == 1
35 && identString.
CharAtStart<
false>() == decl->IdentifierChar() )
36 || ( identString.
Length() >= decl->MinimumRecognitionLength()
46 if ( identString.
Length() >= decl->MinimumRecognitionLength()
71 if( param->IsOptional() ) result <<
'[';
73 result << param->Name();
74 if ( param->ValueListSeparator() !=
'\0' )
76 result <<
'[' << param->ValueListSeparator() << param->Name() <<
"...]";
79 if( param->IsOptional() ) result <<
']';
90 bool argWasPeeked=
false;
103 if( helpOpt->
Args.IsNotEmpty() )
104 argList= helpOpt->
Args.Front();
114 int cntArgsRecognized= 0;
123 int cntArgsRecognizedSoFar= cntArgsRecognized;
147 text.
Add(
"* ", param->Name() )
149 .AddMarked( param->GetHelpTextShort() )
160 if( cntArgsRecognizedSoFar == cntArgsRecognized )
182 if( cntArgsRecognizedSoFar == cntArgsRecognized )
199 if( cntArgsRecognizedSoFar == cntArgsRecognized )
201 auto& additionalHelpTopics= cmdLine.
TryResource(
"HlpAddnlTopics");
202 if( additionalHelpTopics.IsNotEmpty() )
204 Tokenizer topics(additionalHelpTopics,
',');
205 while(topics.
Next().IsNotEmpty())
222 if( cntArgsRecognized > 0 && argWasPeeked )
233 helpOpt->
Args.PushBack( argList );
239 if ( ( !argWasPeeked && cntArgsRecognized == 0 )
240 || ( cntTokens > 1 && cntArgsRecognized < cntTokens ) )
244 if( cntArgsRecognized > 0 )
257 std::vector<std::pair<Enum, ExitCodeDecl *>,
260 sortedExitCodes.emplace_back(declIt);
261 std::sort( sortedExitCodes.begin(), sortedExitCodes.end(),
262 []( std::pair<Enum, ExitCodeDecl *>& lhs,
263 std::pair<Enum, ExitCodeDecl *>& rhs)
265 return lhs.first.Integral() < rhs.first.Integral();
269 for(
auto declIt : sortedExitCodes )
270 text.
Add(
" {:>3}: {}\n {}", declIt.first.Integral(),
271 declIt.second->Name(),
272 declIt.second->FormatString() );
284 text.
Add( decl->HelpUsageLine() );
294 .Add( decl->HelpTextShort(),
NewLine() )
309 bool argWasPeeked=
false;
310 if( dryOpt.
Args.IsNotEmpty() )
311 arg= dryOpt.
Args.Front();
323 cmdLine.
DryRun= dryRunMode;
333 else if ( !argWasPeeked )
343 dump.
Add(
"COMMANDS:")
347 dump.
Add(
"- ({}) {}", decl->Element(), decl->Identifier())
350 for(
auto& param : decl->Parameters )
351 paramIDs << param->Name() <<
", ";
356 dump.
Add(
"Associated parameters: ", paramIDs )
357 .Add( decl->HelpTextShort())
369 dump.
Add( decl->HelpUsageLine() )
370 .Add( decl->HelpText() )
382 dump.
Add(
"- ({}) {} Optional: {} Multi-Separator: {}" ,
386 (decl->ValueListSeparator() ?
Box(decl->ValueListSeparator()) :
Box(
"-/-") ))
387 .Add( decl->GetHelpTextShort())
396 dump.
Add(
"{:>5} : {}", declIt.first, declIt.second->FormatString() );
409 std::vector<Option*> options;
410 std::vector<Option*> optionsOfActType;
411 auto overallSize= optionsOriginal.
Size();
412 options .reserve(
static_cast<size_t>(overallSize) );
413 optionsOfActType.reserve(
static_cast<size_t>(overallSize) );
414 for(
auto* optionOrig : optionsOriginal )
415 options.push_back( optionOrig );
418 while( options.size() )
421 auto* decl= options.front()->Declaration;
422 optionsOfActType.clear();
424 while( actIdx < options.size() )
426 if( options[actIdx]->Declaration == decl )
428 optionsOfActType.push_back( options[actIdx] );
429 options.erase( options.begin() +
static_cast<integer>(actIdx) );
436 dump.
Add(
"- \"-{},--{}\" ({}x)",
437 decl->IdentifierChar(), decl->Identifier(), optionsOfActType.size() )
440 for( actIdx= 0; actIdx < optionsOfActType.size() ; ++actIdx )
442 Option* actOption= optionsOfActType[actIdx];
443 dump.
Add(
"{}/{}: ArgStrings{!Q[]}= {!Q}, #arguments parsed: {}",
444 actIdx + 1, optionsOfActType.size(),
446 app.
GetArg(actOption->Position),
447 actOption->Args.Size() )
451 for(
auto& arg : actOption->Args )
452 dump.Add(
"Argument {}: {!Q}", ++argNo, arg );
468 dumpParsedOptions( cmdLine, cmdLine.
Options, dump );
471 .Add(
"OPTION ARGUMENTS IGNORED (Usable with other libs):")
475 dump.
Add(
"{}: {!Q}", cnt++ + 1, it );
481 .Add(
"COMMANDS PARSED:")
487 dump.
Add(
"- {:8}with argument #{}", cmd->Declaration->Identifier(), cmd->Position )
489 for(
auto* param : cmd->ParametersMandatory )
491 dump.
Add(
"Parameter: {}", param->Declaration->Name() )
494 for(
auto& arg : param->Args )
495 dump.
Add(
"Parameter argument {}: {!Q}", ++argNo, arg );
499 for(
auto* param : cmd->ParametersOptional )
501 dump.
Add(
"Parameter: {}", param->Declaration->Name() )
504 for(
auto& arg : param->Args )
505 dump.
Add(
"Parameter argument {}: {!Q}", ++argNo, arg );
519 .Add(
"UNRECOGNIZED CLI ARGUMENTS:")
522 dump.
Add(
"{}: {!Q}", it, cmdLine.
GetArg(it));
static ALIB_API CommandDecl * GetCommandDecl(CommandLine &cmdLine, const String &identString)
static ALIB_API ParameterDecl * GetParameterDecl(CommandLine &cmdLine, const String &identString)
static ALIB_API OptionDecl * GetOptionDecl(CommandLine &cmdLine, const String &identString)
static ALIB_API AString & DumpDeclarations(CommandLine &cmdLine, Paragraphs &text)
static ALIB_API bool GetHelp(CommandLine &cmdLine, Command *helpCmd, Option *helpOpt, Paragraphs &text)
static ALIB_API AString GetCommandUsageFormat(CommandLine &cmdLine, CommandDecl &commandDecl)
static ALIB_API AString & DumpParseResults(CommandLine &cmdLine, Paragraphs &text)
static ALIB_API bool GetDryOpt(CommandLine &cmdLine, Option &dryOpt)
const String & Identifier()
const String & HelpTextLong()
List< ParameterDecl * > Parameters
List< OptionDecl * > OptionDecls
List< ParameterDecl * > ParameterDecls
virtual String GetArg(integer idx)
HashMap< Enum, ExitCodeDecl * > ExitCodeDecls
List< Command * > CommandsParsed
const String & TryResource(const NString &name)
virtual integer ArgCount()
ALIB_API void RemoveArg(integer argNo)
const String & GetResource(const NString &name)
List< String, Recycling::Shared > OptionArgsIgnored
List< CommandDecl * > CommandDecls
std::vector< integer, StdContMA< integer > > ArgsLeft
const String & Identifier()
const String & HelpText()
const String & HelpUsageLine()
const String & GetHelpTextLong()
ALIB_API void Reset(const Snapshot &snapshot=Snapshot())
TAString & DeleteEnd(integer regionLength)
void EnsureRemainingCapacity(integer spaceNeeded)
constexpr bool IsEmpty() const
constexpr bool IsNotEmpty() const
constexpr integer Length() const
TChar CharAtStart() const
ALIB_API TSubstring< TChar > & Next(lang::Whitespaces trimming=lang::Whitespaces::Trim, TChar newDelim='\0')
TSubstring< TChar > Actual
bool Parse(strings::TSubstring< TChar > &input, TEnum &result)
lang::uinteger uinteger
Type alias in namespace alib.
constexpr CString NewLine()
constexpr String NullString()
boxing::Box Box
Type alias in namespace alib.
lang::integer integer
Type alias in namespace alib.
List< Parameter *, Recycling::Shared > ParametersOptional
List< String, Recycling::Shared > Args