8#if !defined(ALIB_C20_MODULES) || ((ALIB_C20_MODULES != 0) && (ALIB_C20_MODULES != 1))
9# error "Symbol ALIB_C20_MODULES has to be given to the compiler as either 0 or 1"
37 if ( ( identString.
Length() == 1
39 || ( identString.
Length() >= decl->MinimumRecognitionLength()
49 if ( identString.
Length() >= decl->MinimumRecognitionLength()
74 if( param->IsOptional() ) result <<
'[';
76 result << param->Name();
77 if ( param->ValueListSeparator() !=
'\0' )
79 result <<
'[' << param->ValueListSeparator() << param->Name() <<
"...]";
82 if( param->IsOptional() ) result <<
']';
94 bool argWasPeeked=
false;
107 if( helpOpt->
Args.IsNotEmpty() )
108 argList= helpOpt->
Args.front();
118 int cntArgsRecognized= 0;
127 int cntArgsRecognizedSoFar= cntArgsRecognized;
151 text.
Add(
"* ", param->Name() )
153 .AddMarked( param->GetHelpTextShort() )
164 if( cntArgsRecognizedSoFar == cntArgsRecognized )
186 if( cntArgsRecognizedSoFar == cntArgsRecognized )
203 if( cntArgsRecognizedSoFar == cntArgsRecognized )
205 auto& additionalHelpTopics= cmdLine.
TryResource(
"HlpAddnlTopics");
206 if( additionalHelpTopics.IsNotEmpty() )
208 Tokenizer topics(additionalHelpTopics,
',');
226 if( cntArgsRecognized > 0 && argWasPeeked )
237 helpOpt->
Args.push_back( argList );
243 if ( ( !argWasPeeked && cntArgsRecognized == 0 )
244 || ( cntTokens > 1 && cntArgsRecognized < cntTokens ) )
248 if( cntArgsRecognized > 0 )
264 sortedExitCodes.emplace_back(declIt);
265 std::sort( sortedExitCodes.begin(), sortedExitCodes.end(),
266 []( std::pair<Enum, ExitCodeDecl *>& lhs,
267 std::pair<Enum, ExitCodeDecl *>& rhs)
269 return lhs.first.Integral() < rhs.first.Integral();
273 for(
auto& declIt : sortedExitCodes )
274 text.
Add(
" {:>3}: {}\n {}", declIt.first.Integral(),
275 declIt.second->Name(),
276 declIt.second->FormatString() );
288 text.
Add( decl->HelpUsageLine() );
298 .Add( decl->HelpTextShort(),
NEW_LINE )
313 bool argWasPeeked=
false;
314 if( dryOpt.
Args.IsNotEmpty() )
315 arg= dryOpt.
Args.front();
327 cmdLine.
DryRun= dryRunMode;
337 else if ( !argWasPeeked )
347 dump.
Add(
"COMMANDS:")
351 dump.
Add(
"- ({}) {}", decl->Element(), decl->Identifier())
354 for(
auto& param : decl->Parameters )
355 paramIDs << param->Name() <<
", ";
360 dump.
Add(
"Associated parameters: ", paramIDs )
361 .Add( decl->HelpTextShort())
373 dump.
Add( decl->HelpUsageLine() )
374 .Add( decl->HelpText() )
386 dump.
Add(
"- ({}) {} Optional: {} Multi-Separator: {}" ,
390 (decl->ValueListSeparator() ?
Box(decl->ValueListSeparator()) :
Box(
"-/-") ))
391 .Add( decl->GetHelpTextShort())
400 dump.
Add(
"{:>5} : {}", declIt.first, declIt.second->FormatString() );
413 std::vector<Option*> options;
414 std::vector<Option*> optionsOfActType;
415 auto overallSize= optionsOriginal.
size();
416 options .reserve(
size_t(overallSize) );
417 optionsOfActType.reserve(
size_t(overallSize) );
418 for(
auto* optionOrig : optionsOriginal )
419 options.push_back( optionOrig );
422 while( options.size() )
425 auto* decl= options.front()->Declaration;
426 optionsOfActType.clear();
428 while( actIdx < options.size() )
432 optionsOfActType.push_back( options[actIdx] );
433 options.erase( options.begin() +
integer(actIdx) );
440 dump.
Add(
"- \"-{},--{}\" ({}x)",
441 decl->IdentifierChar(), decl->Identifier(), optionsOfActType.size() )
444 for( actIdx= 0; actIdx < optionsOfActType.size() ; ++actIdx )
446 Option* actOption= optionsOfActType[actIdx];
447 dump.
Add(
"{}/{}: ArgStrings{!Q[]}= {!Q}, #arguments parsed: {}",
448 actIdx + 1, optionsOfActType.size(),
450 app.
GetArg(actOption->Position),
451 actOption->Args.
size() )
455 for(
auto& arg : actOption->Args )
456 dump.
Add(
"Argument {}: {!Q}", ++argNo, arg );
472 dumpParsedOptions( cmdLine, cmdLine.
Options, dump );
475 .Add(
"OPTION ARGUMENTS IGNORED (Usable with other libs):")
479 dump.
Add(
"{}: {!Q}", cnt++ + 1, it );
485 .Add(
"COMMANDS PARSED:")
491 dump.
Add(
"- {:8}with argument #{}", cmd->Declaration->Identifier(), cmd->Position )
493 for(
auto* param : cmd->ParametersMandatory )
495 dump.
Add(
"Parameter: {}", param->Declaration->Name() )
498 for(
auto& arg : param->Args )
499 dump.
Add(
"Parameter argument {}: {!Q}", ++argNo, arg );
503 for(
auto* param : cmd->ParametersOptional )
505 dump.
Add(
"Parameter: {}", param->Declaration->Name() )
508 for(
auto& arg : param->Args )
509 dump.
Add(
"Parameter argument {}: {!Q}", ++argNo, arg );
523 .Add(
"UNRECOGNIZED CLI ARGUMENTS:")
526 dump.
Add(
"{}: {!Q}", it, cmdLine.
GetArg(it));
static ALIB_DLL ParameterDecl * GetParameterDecl(CommandLine &cmdLine, const String &identString)
static ALIB_DLL bool GetDryOpt(CommandLine &cmdLine, Option &dryOpt)
static ALIB_DLL OptionDecl * GetOptionDecl(CommandLine &cmdLine, const String &identString)
static ALIB_DLL AString & DumpParseResults(CommandLine &cmdLine, Paragraphs &text)
static ALIB_DLL AString & DumpDeclarations(CommandLine &cmdLine, Paragraphs &text)
static ALIB_DLL CommandDecl * GetCommandDecl(CommandLine &cmdLine, const String &identString)
static ALIB_DLL bool GetHelp(CommandLine &cmdLine, Command *helpCmd, Option *helpOpt, Paragraphs &text)
static ALIB_DLL AString GetCommandUsageFormat(CommandLine &cmdLine, CommandDecl &commandDecl)
const String & Identifier()
List< MonoAllocator, ParameterDecl * > Parameters
Command parameters.
const String & HelpTextLong()
ALIB_DLL void RemoveArg(integer argNo)
List< MonoAllocator, Option * > Options
The options parsed in the order of their appearance.
const String & TryResource(const NString &name)
List< MonoAllocator, ParameterDecl * > ParameterDecls
Possible Parameters.
List< MonoAllocator, String, Recycling::Shared > OptionArgsIgnored
List< MonoAllocator, OptionDecl * > OptionDecls
Possible Options.
const String & GetResource(const NString &name)
StdVectorMono< integer > ArgsLeft
List< MonoAllocator, CommandDecl * > CommandDecls
Commands defined.
List< MonoAllocator, Command * > CommandsParsed
A list of commands actually parsed. Filled with method ReadNextCommands.
HashMap< MonoAllocator, Enum, ExitCodeDecl * > ExitCodeDecls
Possible Errors.
virtual String GetArg(integer idx)
virtual integer ArgCount()
const String & HelpUsageLine()
const String & HelpText()
const String & Identifier()
const String & GetHelpTextLong()
ALIB_DLL void Reset(Snapshot snapshot=Snapshot())
TAString & DeleteEnd(integer regionLength)
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
ALIB_DLL TSubstring< TChar > & Next(lang::Whitespaces trimming=lang::Whitespaces::Trim, TChar newDelim='\0')
TSubstring< TChar > Actual
#define ALIB_LOCK_RECURSIVE_WITH(lock)
DryRunModes
Dry run modes.
bool Parse(strings::TSubstring< TChar > &input, TEnum &result)
variables::Declaration Declaration
Type alias in namespace alib.
constexpr String NULL_STRING
A nulled string of the default character type.
NLocalString< 64 > NString64
Type alias name for TLocalString<nchar,64>.
LocalString< 256 > String256
Type alias name for TLocalString<character,256>.
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace alib.
strings::util::TTokenizer< character > Tokenizer
Type alias in namespace alib.
std::vector< T, SCAMono< T > > StdVectorMono
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.
format::Paragraphs Paragraphs
Type alias in namespace alib.
boxing::Box Box
Type alias in namespace alib.
strings::TString< character > String
Type alias in namespace alib.
lang::uinteger uinteger
Type alias in namespace alib.
strings::TSubstring< character > Substring
Type alias in namespace alib.
containers::List< TAllocator, T, TRecycling > List
Type alias in namespace alib.
A command of a ALib CLI command line.
List< MonoAllocator, Parameter *, Recycling::Shared > ParametersOptional
Optional parameters parsed.
List< MonoAllocator, String, Recycling::Shared > Args
Arguments belonging to this option.
integer ConsumedArguments