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"
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 <<
']';
88 bool argWasPeeked=
false;
97 if( helpOpt->
Args.IsNotEmpty() )
98 argList= helpOpt->
Args.front();
105 int cntArgsRecognized= 0;
113 int cntArgsRecognizedSoFar= cntArgsRecognized;
134 text.
Add(
"* ", param->Name() )
136 .AddMarked( param->GetHelpTextShort() )
145 if( cntArgsRecognizedSoFar == cntArgsRecognized ) {
162 if( cntArgsRecognizedSoFar == cntArgsRecognized ) {
174 if( cntArgsRecognizedSoFar == cntArgsRecognized ) {
175 auto& additionalHelpTopics= cmdLine.
TryResource(
"HlpAddnlTopics");
176 if( additionalHelpTopics.IsNotEmpty() ) {
177 Tokenizer topics(additionalHelpTopics,
',');
189 if( cntArgsRecognized > 0 && argWasPeeked ) {
196 helpOpt->
Args.push_back( argList );
201 if ( ( !argWasPeeked && cntArgsRecognized == 0 )
202 || ( cntTokens > 1 && cntArgsRecognized < cntTokens ) )
206 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 )
297 dump.
Add(
"COMMANDS:")
300 dump.
Add(
"- ({}) {}", decl->Element(), decl->Identifier())
303 for(
auto& param : decl->Parameters )
304 paramIDs << param->Name() <<
", ";
309 dump.
Add(
"Associated parameters: ", paramIDs )
310 .Add( decl->HelpTextShort())
321 dump.
Add( decl->HelpUsageLine() )
322 .Add( decl->HelpText() )
333 dump.
Add(
"- ({}) {} Optional: {} Multi-Separator: {}" ,
337 (decl->ValueListSeparator() ?
Box(decl->ValueListSeparator()) :
Box(
"-/-") ))
338 .Add( decl->GetHelpTextShort())
347 dump.
Add(
"{:>5} : {}", declIt.first, declIt.second->FormatString() );
360 std::vector<Option*> options;
361 std::vector<Option*> optionsOfActType;
362 auto overallSize= optionsOriginal.
size();
363 options .reserve(
size_t(overallSize) );
364 optionsOfActType.reserve(
size_t(overallSize) );
365 for(
auto* optionOrig : optionsOriginal )
366 options.push_back( optionOrig );
369 while( options.size() ) {
371 auto* decl= options.front()->Declaration;
372 optionsOfActType.clear();
374 while( actIdx < options.size() ) {
376 optionsOfActType.push_back( options[actIdx] );
377 options.erase( options.begin() +
integer(actIdx) );
384 dump.
Add(
"- \"-{},--{}\" ({}x)",
385 decl->IdentifierChar(), decl->Identifier(), optionsOfActType.size() )
388 for( actIdx= 0; actIdx < optionsOfActType.size() ; ++actIdx ) {
389 Option* actOption= optionsOfActType[actIdx];
390 dump.
Add(
"{}/{}: ArgStrings{!Q[]}= {!Q}, #arguments parsed: {}",
391 actIdx + 1, optionsOfActType.size(),
393 app.
GetArg(actOption->Position),
394 actOption->Args.
size() )
398 for(
auto& arg : actOption->Args )
399 dump.
Add(
"Argument {}: {!Q}", ++argNo, arg );
414 dumpParsedOptions( cmdLine, cmdLine.
Options, dump );
417 .Add(
"OPTION ARGUMENTS IGNORED (Usable with other libs):")
421 dump.
Add(
"{}: {!Q}", cnt++ + 1, it );
427 .Add(
"COMMANDS PARSED:")
432 dump.
Add(
"- {:8}with argument #{}", cmd->Declaration->Identifier(), cmd->Position )
434 for(
auto* param : cmd->ParametersMandatory ) {
435 dump.
Add(
"Parameter: {}", param->Declaration->Name() )
438 for(
auto& arg : param->Args )
439 dump.
Add(
"Parameter argument {}: {!Q}", ++argNo, arg );
443 for(
auto* param : cmd->ParametersOptional ) {
444 dump.
Add(
"Parameter: {}", param->Declaration->Name() )
447 for(
auto& arg : param->Args )
448 dump.
Add(
"Parameter argument {}: {!Q}", ++argNo, arg );
462 .Add(
"UNRECOGNIZED CLI ARGUMENTS:")
465 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)
ListMA< ParameterDecl * > Parameters
Command parameters.
const String & Identifier()
const String & HelpTextLong()
ALIB_DLL 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)
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, StdMA< T > > StdVectorMA
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.
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.
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