8#if !defined(ALIB_C20_MODULES) || ((ALIB_C20_MODULES != 0) && (ALIB_C20_MODULES != 1))
9# error "Configuration MACRO ALIB_C20_MODULES has to be given to the compiler as either 0 or 1"
20 import ALib.Characters.Functions;
22 import ALib.Strings.Tokenizer;
36 if ( ( identString.
Length() == 1
38 || ( identString.
Length() >= decl->MinimumRecognitionLength()
47 if ( identString.
Length() >= decl->MinimumRecognitionLength()
69 if( param->IsOptional() ) result <<
'[';
71 result << param->Name();
72 if ( param->ValueListSeparator() !=
'\0' ) {
73 result <<
'[' << param->ValueListSeparator() << param->Name() <<
"...]";
76 if( param->IsOptional() ) result <<
']';
92 return GetHelp(cmdLine, topics, text);
97 if( helpOpt->
Args.IsNotEmpty() )
98 topics= helpOpt->
Args.front();
103 helpOpt->
Args.push_back( topics );
106 return GetHelp(cmdLine, topics, text);
116 int cntArgsRecognized= 0;
124 int cntArgsRecognizedSoFar= cntArgsRecognized;
145 text.
Add(
"* ", param->Name() )
147 .AddMarked( param->GetHelpTextShort() )
156 if( cntArgsRecognizedSoFar == cntArgsRecognized ) {
173 if( cntArgsRecognizedSoFar == cntArgsRecognized ) {
185 if( cntArgsRecognizedSoFar == cntArgsRecognized ) {
186 auto& additionalHelpTopics= cmdLine.
TryResource(
"HlpAddnlTopics");
187 if( additionalHelpTopics.IsNotEmpty() ) {
188 Tokenizer topicsTknzr(additionalHelpTopics,
',');
201 if ( ( cntArgsRecognized == 0 )
202 || ( cntTokens > 1 && cntArgsRecognized < cntTokens ) ) {
208 if( cntArgsRecognized > 0 )
222 sortedExitCodes.emplace_back(declIt);
223 std::sort( sortedExitCodes.begin(), sortedExitCodes.end(),
224 []( std::pair<Enum, ExitCodeDecl *>& lhs,
225 std::pair<Enum, ExitCodeDecl *>& rhs)
227 return lhs.first.Integral() < rhs.first.Integral();
231 for(
auto& declIt : sortedExitCodes )
232 text.
Add(
" {:>3}: {}\n {}", declIt.first.Integral(),
233 declIt.second->Name(),
234 declIt.second->FormatString() );
246 text.
Add( decl->HelpUsageLine() );
255 .Add( decl->HelpTextShort(),
NEW_LINE )
269 bool argWasPeeked=
false;
270 if( dryOpt.
Args.IsNotEmpty() )
271 arg= dryOpt.
Args.front();
280 cmdLine.
DryRun= dryRunMode;
288 else if ( !argWasPeeked ) {
298 dump.
Add(
"COMMANDS:")
301 dump.
Add(
"- ({}) {}", decl->Element(), decl->Identifier())
304 for(
auto& param : decl->Parameters )
305 paramIDs << param->Name() <<
", ";
310 dump.
Add(
"Associated parameters: ", paramIDs )
311 .Add( decl->HelpTextShort())
322 dump.
Add( decl->HelpUsageLine() )
323 .Add( decl->HelpText() )
334 dump.
Add(
"- ({}) {} Optional: {} Multi-Separator: {}" ,
338 (decl->ValueListSeparator() ?
Box(decl->ValueListSeparator()) :
Box(
"-/-") ))
339 .Add( decl->GetHelpTextShort())
348 dump.
Add(
"{:>5} : {}", declIt.first, declIt.second->FormatString() );
361 std::vector<Option*> options;
362 std::vector<Option*> optionsOfActType;
363 auto overallSize= optionsOriginal.
size();
364 options .reserve(
size_t(overallSize) );
365 optionsOfActType.reserve(
size_t(overallSize) );
366 for(
auto* optionOrig : optionsOriginal )
367 options.push_back( optionOrig );
370 while( options.size() ) {
372 auto* decl= options.front()->Declaration;
373 optionsOfActType.clear();
375 while( actIdx < options.size() ) {
377 optionsOfActType.push_back( options[actIdx] );
378 options.erase( options.begin() +
integer(actIdx) );
385 dump.
Add(
"- \"-{},--{}\" ({}x)",
386 decl->IdentifierChar(), decl->Identifier(), optionsOfActType.size() )
389 for( actIdx= 0; actIdx < optionsOfActType.size() ; ++actIdx ) {
390 Option* actOption= optionsOfActType[actIdx];
391 dump.
Add(
"{}/{}: ArgStrings{!Q[]}= {!Q}, #arguments parsed: {}",
392 actIdx + 1, optionsOfActType.size(),
394 app.
GetArg(actOption->Position),
395 actOption->Args.
size() )
399 for(
auto& arg : actOption->Args )
400 dump.
Add(
"Argument {}: {!Q}", ++argNo, arg );
415 dumpParsedOptions( cmdLine, cmdLine.
Options, dump );
418 .Add(
"OPTION ARGUMENTS IGNORED (Usable with other libs):")
422 dump.
Add(
"{}: {!Q}", cnt++ + 1, it );
428 .Add(
"COMMANDS PARSED:")
433 dump.
Add(
"- {:8}with argument #{}", cmd->Declaration->Identifier(), cmd->Position )
435 for(
auto* param : cmd->ParametersMandatory ) {
436 dump.
Add(
"Parameter: {}", param->Declaration->Name() )
439 for(
auto& arg : param->Args )
440 dump.
Add(
"Parameter argument {}: {!Q}", ++argNo, arg );
444 for(
auto* param : cmd->ParametersOptional ) {
445 dump.
Add(
"Parameter: {}", param->Declaration->Name() )
448 for(
auto& arg : param->Args )
449 dump.
Add(
"Parameter argument {}: {!Q}", ++argNo, arg );
463 .Add(
"UNRECOGNIZED CLI ARGUMENTS:")
466 dump.
Add(
"{}: {!Q}", it, cmdLine.
GetArg(it));
#define ALIB_LOCK_RECURSIVE_WITH(lock)
static ParameterDecl * GetParameterDecl(CommandLine &cmdLine, const String &identString)
static bool GetDryOpt(CommandLine &cmdLine, Option &dryOpt)
static OptionDecl * GetOptionDecl(CommandLine &cmdLine, const String &identString)
static AString & DumpParseResults(CommandLine &cmdLine, Paragraphs &text)
static AString & DumpDeclarations(CommandLine &cmdLine, Paragraphs &text)
static bool GetHelp(CommandLine &cmdLine, const String &topics, Paragraphs &text)
static CommandDecl * GetCommandDecl(CommandLine &cmdLine, const String &identString)
static AString GetCommandUsageFormat(CommandLine &cmdLine, CommandDecl &commandDecl)
ListMA< ParameterDecl * > Parameters
Command parameters.
const String & Identifier()
const String & HelpTextLong()
void RemoveArg(integer argNo)
const String & TryResource(const NString &name)
StdVectorMA< integer > ArgsLeft
ListMA< String, Recycling::Shared > OptionArgsIgnored
ListMA< CommandDecl * > CommandDecls
Commands defined.
ListMA< Command * > CommandsParsed
A list of commands actually parsed. Filled with method #"ReadNextCommands".
const String & GetResource(const NString &name)
ListMA< Option * > Options
The options parsed in the order of their appearance.
ListMA< ParameterDecl * > ParameterDecls
Possible Parameters.
ListMA< OptionDecl * > OptionDecls
Possible Options.
HashMap< MonoAllocator, Enum, ExitCodeDecl * > ExitCodeDecls
Possible Errors.
virtual String GetArg(integer idx)
const String & HelpUsageLine()
const String & HelpText()
const String & Identifier()
const String & GetHelpTextLong()
void Reset(Snapshot snapshot=Snapshot())
TAString & DeleteEnd(integer regionLength)
TAString & ShortenTo(integer newLength)
void EnsureRemainingCapacity(integer spaceNeeded)
constexpr integer Length() const
constexpr bool IsEmpty() const
TChar CharAtStart() const
constexpr bool IsNotEmpty() const
bool StartsWith(const TString &needle) const
TSubstring< TChar > Actual
TSubstring< TChar > & Next(lang::Whitespaces trimming=lang::Whitespaces::Trim, TChar newDelim='\0')
DryRunModes
Dry run modes.
bool Parse(strings::TSubstring< TChar > &input, TEnum &result)
constexpr String NULL_STRING
A nulled string of the default character type.
strings::util::TTokenizer< character > Tokenizer
Type alias in namespace alib.
containers::List< T, MonoAllocator, TRecycling > ListMA
Type alias in namespace alib.
constexpr CString NEW_LINE
A zero-terminated string containing the new-line character sequence.
lang::integer integer
Type alias in namespace alib.
boxing::Box Box
Type alias in namespace alib.
strings::TString< character > String
Type alias in namespace alib.
strings::TSubstring< character > Substring
Type alias in namespace alib.
LocalString< 256 > String256
Type alias name for #"TLocalString;TLocalString<character,256>".
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace alib.
format::Paragraphs Paragraphs
Type alias in namespace alib.
lang::uinteger uinteger
Type alias in namespace alib.
variables::Declaration Declaration
Type alias in namespace alib.
std::vector< T, StdMA< T > > StdVectorMA
Type alias in namespace alib.
NLocalString< 64 > NString64
Type alias name for #"TLocalString;TLocalString<nchar,64>".
A command of a ALib CLI command-line.
ListMA< Parameter *, Recycling::Shared > ParametersOptional
Optional parameters parsed.
ListMA< String, Recycling::Shared > Args
Arguments belonging to this option.
integer ConsumedArguments